mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-03 16:49:10 +00:00
v2.01 update
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package org.parabot.environment.api.utils;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
@@ -8,24 +11,29 @@ import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
import org.parabot.core.io.ProgressListener;
|
||||
import org.parabot.core.io.SizeInputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Clisprail
|
||||
*
|
||||
* @author Everel
|
||||
*
|
||||
*/
|
||||
public class WebUtil {
|
||||
private static String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
|
||||
|
||||
|
||||
/**
|
||||
* Agent to set at a URL connection
|
||||
*
|
||||
* @param userAgent
|
||||
*/
|
||||
public static void setUserAgent(final String userAgent) {
|
||||
agent = userAgent;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets useragent
|
||||
*
|
||||
* @return useragent
|
||||
*/
|
||||
public static String getUserAgent() {
|
||||
@@ -34,6 +42,7 @@ public class WebUtil {
|
||||
|
||||
/**
|
||||
* Fetches content of a page
|
||||
*
|
||||
* @param location
|
||||
* @return contents of page
|
||||
* @throws MalformedURLException
|
||||
@@ -45,12 +54,23 @@ public class WebUtil {
|
||||
|
||||
/**
|
||||
* Get contents from URL
|
||||
*
|
||||
* @param url
|
||||
* @return page contents
|
||||
*/
|
||||
public static String getContents(final URL url) {
|
||||
return getContents(getConnection(url));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets contents from URLConnection
|
||||
*
|
||||
* @param urlConnection
|
||||
* @return page contents
|
||||
*/
|
||||
public static String getContents(URLConnection urlConnection) {
|
||||
try {
|
||||
final BufferedReader in = getReader(url);
|
||||
final BufferedReader in = getReader(urlConnection);
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
@@ -64,22 +84,44 @@ public class WebUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets buffered reader from string url
|
||||
*
|
||||
* @param url
|
||||
* @return bufferedreader
|
||||
*/
|
||||
public static BufferedReader getReader(final String url) {
|
||||
try {
|
||||
return getReader(new URL(url));
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets BufferedReader from URL
|
||||
*
|
||||
* @param url
|
||||
* @return BufferedReader from URL
|
||||
*/
|
||||
public static BufferedReader getReader(final URL url) {
|
||||
return getReader(getConnection(url));
|
||||
}
|
||||
|
||||
public static BufferedReader getReader(final URLConnection urlConnection) {
|
||||
try {
|
||||
return new BufferedReader(new InputStreamReader(getInputStream(url)));
|
||||
return new BufferedReader(new InputStreamReader(
|
||||
urlConnection.getInputStream()));
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets inputstream from url
|
||||
*
|
||||
* @param url
|
||||
* @return inputstream from url
|
||||
*/
|
||||
@@ -92,9 +134,10 @@ public class WebUtil {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Opens a connection
|
||||
*
|
||||
* @param url
|
||||
* @return URLConnection to URL
|
||||
*/
|
||||
@@ -109,4 +152,36 @@ public class WebUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a file on the internet
|
||||
* @param url
|
||||
* @param destination
|
||||
* @param listener
|
||||
*/
|
||||
public static void downloadFile(final URL url, final File destination,
|
||||
final ProgressListener listener) {
|
||||
try {
|
||||
final URLConnection connection = getConnection(url);
|
||||
int size = connection.getContentLength();
|
||||
SizeInputStream sizeInputStream = new SizeInputStream(
|
||||
connection.getInputStream(), size, listener);
|
||||
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
|
||||
FileOutputStream fileOut = new FileOutputStream(destination);
|
||||
try {
|
||||
byte data[] = new byte[1024];
|
||||
int count;
|
||||
while ((count = in.read(data, 0, 1024)) != -1) {
|
||||
fileOut.write(data, 0, count);
|
||||
}
|
||||
} finally {
|
||||
if (in != null)
|
||||
in.close();
|
||||
if (fileOut != null)
|
||||
fileOut.close();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user