2008-02-13 20:08:06 +00:00
|
|
|
|
2008-03-23 22:41:25 +00:00
|
|
|
package net.sourceforge.tuned;
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.channels.Channels;
|
|
|
|
import java.nio.channels.WritableByteChannel;
|
|
|
|
import java.nio.charset.Charset;
|
2008-06-02 19:12:28 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
2008-02-13 20:08:06 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
import javax.swing.SwingWorker;
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
public class DownloadTask extends SwingWorker<ByteBuffer, Void> {
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
public static final String DOWNLOAD_STATE = "download state";
|
2008-05-03 20:43:15 +00:00
|
|
|
public static final String DOWNLOAD_PROGRESS = "download progress";
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
public static enum DownloadState {
|
2008-03-19 22:14:38 +00:00
|
|
|
PENDING,
|
|
|
|
CONNECTING,
|
|
|
|
DOWNLOADING,
|
2008-06-02 19:12:28 +00:00
|
|
|
DONE
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
private final int BUFFER_SIZE = 4 * 1024;
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
private URL url;
|
2008-06-02 19:12:28 +00:00
|
|
|
private ByteBuffer postdata;
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
private long size = -1;
|
|
|
|
private long bytesRead = 0;
|
|
|
|
private DownloadState state = DownloadState.PENDING;
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
private final Map<String, String> requestHeaders = new HashMap<String, String>();
|
|
|
|
private final Map<String, String> responseHeaders = new HashMap<String, String>();
|
|
|
|
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
public DownloadTask(URL url) {
|
2008-06-02 19:12:28 +00:00
|
|
|
this(url, null);
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
public DownloadTask(URL url, Map<String, String> postParameters) {
|
2008-02-13 20:08:06 +00:00
|
|
|
this.url = url;
|
2008-06-02 19:12:28 +00:00
|
|
|
|
|
|
|
if (postParameters != null) {
|
|
|
|
this.postdata = encodeParameters(postParameters);
|
|
|
|
}
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
protected HttpURLConnection createConnection() throws Exception {
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
|
|
for (String key : requestHeaders.keySet()) {
|
|
|
|
connection.addRequestProperty(key, requestHeaders.get(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
return connection;
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ByteBuffer doInBackground() throws Exception {
|
|
|
|
setDownloadState(DownloadState.CONNECTING);
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
HttpURLConnection connection = createConnection();
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
if (postdata != null) {
|
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
WritableByteChannel out = Channels.newChannel(connection.getOutputStream());
|
|
|
|
out.write(postdata);
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
size = connection.getContentLength();
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
for (String key : connection.getHeaderFields().keySet()) {
|
|
|
|
responseHeaders.put(key, connection.getHeaderField(key));
|
|
|
|
}
|
|
|
|
|
2008-02-13 20:08:06 +00:00
|
|
|
setDownloadState(DownloadState.DOWNLOADING);
|
|
|
|
|
|
|
|
InputStream in = connection.getInputStream();
|
2008-06-02 19:12:28 +00:00
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max((int) size, BUFFER_SIZE));
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
byte[] buffer = new byte[BUFFER_SIZE];
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
try {
|
2008-06-02 19:12:28 +00:00
|
|
|
while (((len = in.read(buffer)) >= 0) && !isCancelled()) {
|
2008-02-13 20:08:06 +00:00
|
|
|
out.write(buffer, 0, len);
|
|
|
|
|
|
|
|
bytesRead += len;
|
2008-06-02 19:12:28 +00:00
|
|
|
|
|
|
|
firePropertyChange(DOWNLOAD_PROGRESS, null, bytesRead);
|
|
|
|
|
|
|
|
if (isDownloadSizeKnown()) {
|
|
|
|
setProgress((int) ((bytesRead * 100) / size));
|
|
|
|
}
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
// IOException (Premature EOF) is always thrown when the size of
|
|
|
|
// the response body is not known in advance, so we ignore it
|
2008-06-02 19:12:28 +00:00
|
|
|
if (isDownloadSizeKnown())
|
2008-02-13 20:08:06 +00:00
|
|
|
throw e;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
in.close();
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
setDownloadState(DownloadState.DONE);
|
|
|
|
return ByteBuffer.wrap(out.toByteArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
protected void setDownloadState(DownloadState state) {
|
2008-02-13 20:08:06 +00:00
|
|
|
this.state = state;
|
2008-06-02 19:12:28 +00:00
|
|
|
firePropertyChange(DOWNLOAD_STATE, null, state);
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DownloadState getDownloadState() {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
public URL getUrl() {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-13 20:08:06 +00:00
|
|
|
public long getBytesRead() {
|
|
|
|
return bytesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
public boolean isDownloadSizeKnown() {
|
2008-02-13 20:08:06 +00:00
|
|
|
return size >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
public long getDownloadSize() {
|
2008-02-13 20:08:06 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
public void setRequestHeader(String name, String value) {
|
|
|
|
requestHeaders.put(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Map<String, String> getResponseHeaders() {
|
|
|
|
return Collections.unmodifiableMap(responseHeaders);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected static ByteBuffer encodeParameters(Map<String, String> parameters) {
|
2008-03-19 22:14:38 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (String key : parameters.keySet()) {
|
|
|
|
if (i > 0)
|
|
|
|
sb.append("&");
|
|
|
|
|
|
|
|
sb.append(key);
|
|
|
|
sb.append("=");
|
|
|
|
|
|
|
|
try {
|
|
|
|
sb.append(URLEncoder.encode(parameters.get(key), "UTF-8"));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
// will never happen
|
2008-03-16 01:05:06 +00:00
|
|
|
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString(), e);
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Charset.forName("UTF-8").encode(sb.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|