Created
May 17, 2012 13:20
-
-
Save boxmein/2718870 to your computer and use it in GitHub Desktop.
Connects to Pastebin and crops out the paste content via looking for HTML tags. Use as add-on class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Connects to Pastebin and receives a paste's content through cutting the received string with some HTML tags that | |
* Pastebin uses to store its content. | |
* | |
* | |
* | |
* | |
*/ | |
import java.io.*; // Used to read the content of the URL stream | |
import java.net.*; // Used to connect to the Internet and create a stream | |
public class URLtest { | |
static String url_as_string; | |
public URLtest(String url) | |
{ | |
url_as_string = url; | |
fetchURL(); | |
} | |
public static void fetchURL() | |
{ | |
URL url1 = null; // Declares the URL so it could be used, gives it a value to not get a compiler error | |
try { | |
url1 = new URL(url_as_string); // Gives the URL its real meaning | |
} catch(MalformedURLException ex) | |
{ | |
System.out.println(ex.getMessage()); // Catches the exception and prints the output | |
ex.printStackTrace(); | |
} | |
InputStream stream = null; // A stream to read from the URL | |
try { | |
stream = url1.openStream();// The stream now reads from the URL | |
} | |
catch(IOException ex) | |
{ | |
System.out.println(ex.getMessage()); // Another exception handle | |
ex.printStackTrace(); | |
} | |
catch(NullPointerException ex) | |
{ | |
ex.printStackTrace(); | |
} | |
String str = null; // The string used for output | |
try { | |
str = convertStreamToString(stream); // Makes the InputStream into a string | |
} catch(Exception exc) { | |
System.out.println(exc.getMessage());// Handles the exception | |
exc.printStackTrace(); | |
} | |
// Gets the paste content | |
String content = str.substring(str.indexOf("<textarea cols=\"5\" rows=\"5\" id=\"paste_code\" class=\"paste_code\" name=\"paste_code\" onkeydown=\"return catchTab(this,event)\">"),str.indexOf("</textarea>")); | |
// Removes the textarea tag from the beginning of the content | |
String content2 = null; | |
content2 = content.substring("<textarea cols=\"5\" rows=\"5\" id=\"paste_code\" class=\"paste_code\" name=\"paste_code\" onkeydown=\"return catchTab(this,event)\">".length()); | |
// Output | |
System.out.println("The content of the Stream: \n" + str); // The output | |
System.out.println("The content of the paste: \n" + content2); | |
System.out.println("Run successful."); | |
} | |
public static String convertStreamToString(InputStream is) throws Exception { // A method that makes an InputStream into a String, copied from the web | |
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); | |
StringBuilder sb = new StringBuilder(); | |
String line = null; | |
while ((line = reader.readLine()) != null) { | |
sb.append(line + "\n"); | |
} | |
is.close(); | |
return sb.toString(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment