1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-08-13 17:03:45 -04:00

* increase read-buffer

This commit is contained in:
Reinhard Pointner 2014-08-17 06:56:48 +00:00
parent 611a6bf637
commit bd45c798d4
5 changed files with 57 additions and 74 deletions

View File

@ -86,7 +86,7 @@ public final class VerificationUtilities {
InputStream in = new FileInputStream(file);
try {
byte[] buffer = new byte[32 * 1024];
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) >= 0) {

View File

@ -1,6 +1,6 @@
package net.filebot.ui.sfv;
import static net.filebot.util.FileUtilities.*;
import java.io.File;
import java.io.FileInputStream;
@ -14,46 +14,41 @@ import javax.swing.SwingWorker;
import net.filebot.hash.Hash;
import net.filebot.hash.HashType;
class ChecksumComputationTask extends SwingWorker<Map<HashType, String>, Void> {
private static final int BUFFER_SIZE = 32 * 1024;
private final File file;
private final HashType hashType;
public ChecksumComputationTask(File file, HashType hashType) {
this.file = file;
this.hashType = hashType;
}
@Override
protected Map<HashType, String> doInBackground() throws Exception {
// create hash instance
Hash hash = hashType.newHash();
// cache length for speed
long length = file.length();
// open file
InputStream in = new FileInputStream(file);
try {
byte[] buffer = new byte[BUFFER_SIZE];
long position = 0;
int len = 0;
while ((len = in.read(buffer)) >= 0) {
position += len;
hash.update(buffer, 0, len);
// update progress
setProgress((int) ((position * 100) / length));
// check abort status
if (isCancelled() || Thread.interrupted()) {
throw new CancellationException();
@ -62,8 +57,8 @@ class ChecksumComputationTask extends SwingWorker<Map<HashType, String>, Void> {
} finally {
in.close();
}
return Collections.singletonMap(hashType, hash.digest());
}
}

View File

@ -1,7 +1,6 @@
package net.filebot.util;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.web.WebRequest.*;
import java.io.IOException;
@ -19,150 +18,135 @@ import java.util.Map.Entry;
import javax.swing.SwingWorker;
public class DownloadTask extends SwingWorker<ByteBuffer, Void> {
public static final String DOWNLOAD_STATE = "download state";
public static final String DOWNLOAD_PROGRESS = "download progress";
public static enum DownloadState {
PENDING, CONNECTING, DOWNLOADING, DONE
}
private URL url;
private long contentLength = -1;
private DownloadState state = DownloadState.PENDING;
private Map<String, String> postParameters;
private Map<String, String> requestHeaders;
private Map<String, List<String>> responseHeaders;
public DownloadTask(URL url) {
this.url = url;
}
protected HttpURLConnection createConnection() throws Exception {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (requestHeaders != null) {
for (Entry<String, String> entry : requestHeaders.entrySet()) {
connection.addRequestProperty(entry.getKey(), entry.getValue());
}
}
return connection;
}
@Override
protected ByteBuffer doInBackground() throws Exception {
setDownloadState(DownloadState.CONNECTING);
HttpURLConnection connection = createConnection();
if (postParameters != null) {
ByteBuffer postData = Charset.forName("UTF-8").encode(encodeParameters(postParameters, true));
// add content type and content length headers
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.addRequestProperty("Content-Length", String.valueOf(postData.remaining()));
connection.setRequestMethod("POST");
connection.setDoOutput(true);
// write post data
WritableByteChannel out = Channels.newChannel(connection.getOutputStream());
out.write(postData);
out.close();
}
contentLength = connection.getContentLength();
responseHeaders = connection.getHeaderFields();
setDownloadState(DownloadState.DOWNLOADING);
ReadableByteChannel in = Channels.newChannel(connection.getInputStream());
ByteBufferOutputStream buffer = new ByteBufferOutputStream((int) (contentLength > 0 ? contentLength : 32 * 1024));
ByteBufferOutputStream buffer = new ByteBufferOutputStream((int) (contentLength > 0 ? contentLength : BUFFER_SIZE));
try {
while (!isCancelled() && ((buffer.transferFrom(in)) >= 0)) {
firePropertyChange(DOWNLOAD_PROGRESS, null, buffer.position());
if (isContentLengthKnown()) {
setProgress((int) ((buffer.position() * 100) / contentLength));
}
}
} catch (IOException e) {
// if the content length is not known in advance an IOException (Premature EOF)
// if the content length is not known in advance an IOException (Premature EOF)
// is always thrown after all the data has been read
if (isContentLengthKnown())
throw e;
} finally {
in.close();
// download either finished or an exception is thrown
setDownloadState(DownloadState.DONE);
}
return buffer.getByteBuffer();
}
protected void setDownloadState(DownloadState state) {
this.state = state;
firePropertyChange(DOWNLOAD_STATE, null, state);
}
public DownloadState getDownloadState() {
return state;
}
public URL getUrl() {
return url;
}
public boolean isContentLengthKnown() {
return contentLength >= 0;
}
public long getContentLength() {
return contentLength;
}
public void setRequestHeaders(Map<String, String> requestHeaders) {
this.requestHeaders = new HashMap<String, String>(requestHeaders);
}
public void setPostParameters(Map<String, String> postParameters) {
this.postParameters = new HashMap<String, String>(postParameters);
}
public Map<String, List<String>> getResponseHeaders() {
return responseHeaders;
}
public Map<String, String> getPostParameters() {
return postParameters;
}
public Map<String, String> getRequestHeaders() {
return requestHeaders;
}
}

View File

@ -642,6 +642,8 @@ public final class FileUtilities {
return buffer.toString();
}
public static final int BUFFER_SIZE = 64 * 1024;
public static final long KILO = 1024;
public static final long MEGA = 1024 * KILO;
public static final long GIGA = 1024 * MEGA;

View File

@ -1,5 +1,7 @@
package net.filebot.web;
import static net.filebot.util.FileUtilities.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -185,7 +187,7 @@ public final class WebRequest {
in = new InflaterInputStream(in, new Inflater(true));
}
ByteBufferOutputStream buffer = new ByteBufferOutputStream(contentLength >= 0 ? contentLength : 32 * 1024);
ByteBufferOutputStream buffer = new ByteBufferOutputStream(contentLength >= 0 ? contentLength : BUFFER_SIZE);
try {
// read all
buffer.transferFully(in);