* increase read-buffer
This commit is contained in:
parent
611a6bf637
commit
bd45c798d4
|
@ -86,7 +86,7 @@ public final class VerificationUtilities {
|
||||||
InputStream in = new FileInputStream(file);
|
InputStream in = new FileInputStream(file);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
byte[] buffer = new byte[32 * 1024];
|
byte[] buffer = new byte[BUFFER_SIZE];
|
||||||
int len = 0;
|
int len = 0;
|
||||||
|
|
||||||
while ((len = in.read(buffer)) >= 0) {
|
while ((len = in.read(buffer)) >= 0) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
package net.filebot.ui.sfv;
|
package net.filebot.ui.sfv;
|
||||||
|
|
||||||
|
import static net.filebot.util.FileUtilities.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
@ -14,46 +14,41 @@ import javax.swing.SwingWorker;
|
||||||
import net.filebot.hash.Hash;
|
import net.filebot.hash.Hash;
|
||||||
import net.filebot.hash.HashType;
|
import net.filebot.hash.HashType;
|
||||||
|
|
||||||
|
|
||||||
class ChecksumComputationTask extends SwingWorker<Map<HashType, String>, Void> {
|
class ChecksumComputationTask extends SwingWorker<Map<HashType, String>, Void> {
|
||||||
|
|
||||||
private static final int BUFFER_SIZE = 32 * 1024;
|
|
||||||
|
|
||||||
private final File file;
|
private final File file;
|
||||||
private final HashType hashType;
|
private final HashType hashType;
|
||||||
|
|
||||||
|
|
||||||
public ChecksumComputationTask(File file, HashType hashType) {
|
public ChecksumComputationTask(File file, HashType hashType) {
|
||||||
this.file = file;
|
this.file = file;
|
||||||
this.hashType = hashType;
|
this.hashType = hashType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Map<HashType, String> doInBackground() throws Exception {
|
protected Map<HashType, String> doInBackground() throws Exception {
|
||||||
// create hash instance
|
// create hash instance
|
||||||
Hash hash = hashType.newHash();
|
Hash hash = hashType.newHash();
|
||||||
|
|
||||||
// cache length for speed
|
// cache length for speed
|
||||||
long length = file.length();
|
long length = file.length();
|
||||||
|
|
||||||
// open file
|
// open file
|
||||||
InputStream in = new FileInputStream(file);
|
InputStream in = new FileInputStream(file);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
byte[] buffer = new byte[BUFFER_SIZE];
|
byte[] buffer = new byte[BUFFER_SIZE];
|
||||||
|
|
||||||
long position = 0;
|
long position = 0;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
|
|
||||||
while ((len = in.read(buffer)) >= 0) {
|
while ((len = in.read(buffer)) >= 0) {
|
||||||
position += len;
|
position += len;
|
||||||
|
|
||||||
hash.update(buffer, 0, len);
|
hash.update(buffer, 0, len);
|
||||||
|
|
||||||
// update progress
|
// update progress
|
||||||
setProgress((int) ((position * 100) / length));
|
setProgress((int) ((position * 100) / length));
|
||||||
|
|
||||||
// check abort status
|
// check abort status
|
||||||
if (isCancelled() || Thread.interrupted()) {
|
if (isCancelled() || Thread.interrupted()) {
|
||||||
throw new CancellationException();
|
throw new CancellationException();
|
||||||
|
@ -62,8 +57,8 @@ class ChecksumComputationTask extends SwingWorker<Map<HashType, String>, Void> {
|
||||||
} finally {
|
} finally {
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Collections.singletonMap(hashType, hash.digest());
|
return Collections.singletonMap(hashType, hash.digest());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
|
|
||||||
package net.filebot.util;
|
package net.filebot.util;
|
||||||
|
|
||||||
|
import static net.filebot.util.FileUtilities.*;
|
||||||
import static net.filebot.web.WebRequest.*;
|
import static net.filebot.web.WebRequest.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -19,150 +18,135 @@ import java.util.Map.Entry;
|
||||||
|
|
||||||
import javax.swing.SwingWorker;
|
import javax.swing.SwingWorker;
|
||||||
|
|
||||||
|
|
||||||
public class DownloadTask extends SwingWorker<ByteBuffer, Void> {
|
public class DownloadTask extends SwingWorker<ByteBuffer, Void> {
|
||||||
|
|
||||||
public static final String DOWNLOAD_STATE = "download state";
|
public static final String DOWNLOAD_STATE = "download state";
|
||||||
public static final String DOWNLOAD_PROGRESS = "download progress";
|
public static final String DOWNLOAD_PROGRESS = "download progress";
|
||||||
|
|
||||||
|
|
||||||
public static enum DownloadState {
|
public static enum DownloadState {
|
||||||
PENDING, CONNECTING, DOWNLOADING, DONE
|
PENDING, CONNECTING, DOWNLOADING, DONE
|
||||||
}
|
}
|
||||||
|
|
||||||
private URL url;
|
private URL url;
|
||||||
|
|
||||||
private long contentLength = -1;
|
private long contentLength = -1;
|
||||||
private DownloadState state = DownloadState.PENDING;
|
private DownloadState state = DownloadState.PENDING;
|
||||||
|
|
||||||
private Map<String, String> postParameters;
|
private Map<String, String> postParameters;
|
||||||
private Map<String, String> requestHeaders;
|
private Map<String, String> requestHeaders;
|
||||||
private Map<String, List<String>> responseHeaders;
|
private Map<String, List<String>> responseHeaders;
|
||||||
|
|
||||||
|
|
||||||
public DownloadTask(URL url) {
|
public DownloadTask(URL url) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected HttpURLConnection createConnection() throws Exception {
|
protected HttpURLConnection createConnection() throws Exception {
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
if (requestHeaders != null) {
|
if (requestHeaders != null) {
|
||||||
for (Entry<String, String> entry : requestHeaders.entrySet()) {
|
for (Entry<String, String> entry : requestHeaders.entrySet()) {
|
||||||
connection.addRequestProperty(entry.getKey(), entry.getValue());
|
connection.addRequestProperty(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ByteBuffer doInBackground() throws Exception {
|
protected ByteBuffer doInBackground() throws Exception {
|
||||||
setDownloadState(DownloadState.CONNECTING);
|
setDownloadState(DownloadState.CONNECTING);
|
||||||
|
|
||||||
HttpURLConnection connection = createConnection();
|
HttpURLConnection connection = createConnection();
|
||||||
|
|
||||||
if (postParameters != null) {
|
if (postParameters != null) {
|
||||||
ByteBuffer postData = Charset.forName("UTF-8").encode(encodeParameters(postParameters, true));
|
ByteBuffer postData = Charset.forName("UTF-8").encode(encodeParameters(postParameters, true));
|
||||||
|
|
||||||
// add content type and content length headers
|
// add content type and content length headers
|
||||||
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||||
connection.addRequestProperty("Content-Length", String.valueOf(postData.remaining()));
|
connection.addRequestProperty("Content-Length", String.valueOf(postData.remaining()));
|
||||||
|
|
||||||
connection.setRequestMethod("POST");
|
connection.setRequestMethod("POST");
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
// write post data
|
// write post data
|
||||||
WritableByteChannel out = Channels.newChannel(connection.getOutputStream());
|
WritableByteChannel out = Channels.newChannel(connection.getOutputStream());
|
||||||
out.write(postData);
|
out.write(postData);
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
contentLength = connection.getContentLength();
|
contentLength = connection.getContentLength();
|
||||||
|
|
||||||
responseHeaders = connection.getHeaderFields();
|
responseHeaders = connection.getHeaderFields();
|
||||||
|
|
||||||
setDownloadState(DownloadState.DOWNLOADING);
|
setDownloadState(DownloadState.DOWNLOADING);
|
||||||
|
|
||||||
ReadableByteChannel in = Channels.newChannel(connection.getInputStream());
|
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 {
|
try {
|
||||||
while (!isCancelled() && ((buffer.transferFrom(in)) >= 0)) {
|
while (!isCancelled() && ((buffer.transferFrom(in)) >= 0)) {
|
||||||
|
|
||||||
firePropertyChange(DOWNLOAD_PROGRESS, null, buffer.position());
|
firePropertyChange(DOWNLOAD_PROGRESS, null, buffer.position());
|
||||||
|
|
||||||
if (isContentLengthKnown()) {
|
if (isContentLengthKnown()) {
|
||||||
setProgress((int) ((buffer.position() * 100) / contentLength));
|
setProgress((int) ((buffer.position() * 100) / contentLength));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} 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
|
// is always thrown after all the data has been read
|
||||||
if (isContentLengthKnown())
|
if (isContentLengthKnown())
|
||||||
throw e;
|
throw e;
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
in.close();
|
in.close();
|
||||||
|
|
||||||
// download either finished or an exception is thrown
|
// download either finished or an exception is thrown
|
||||||
setDownloadState(DownloadState.DONE);
|
setDownloadState(DownloadState.DONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer.getByteBuffer();
|
return buffer.getByteBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void setDownloadState(DownloadState state) {
|
protected void setDownloadState(DownloadState state) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
firePropertyChange(DOWNLOAD_STATE, null, state);
|
firePropertyChange(DOWNLOAD_STATE, null, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public DownloadState getDownloadState() {
|
public DownloadState getDownloadState() {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public URL getUrl() {
|
public URL getUrl() {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isContentLengthKnown() {
|
public boolean isContentLengthKnown() {
|
||||||
return contentLength >= 0;
|
return contentLength >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public long getContentLength() {
|
public long getContentLength() {
|
||||||
return contentLength;
|
return contentLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setRequestHeaders(Map<String, String> requestHeaders) {
|
public void setRequestHeaders(Map<String, String> requestHeaders) {
|
||||||
this.requestHeaders = new HashMap<String, String>(requestHeaders);
|
this.requestHeaders = new HashMap<String, String>(requestHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setPostParameters(Map<String, String> postParameters) {
|
public void setPostParameters(Map<String, String> postParameters) {
|
||||||
this.postParameters = new HashMap<String, String>(postParameters);
|
this.postParameters = new HashMap<String, String>(postParameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Map<String, List<String>> getResponseHeaders() {
|
public Map<String, List<String>> getResponseHeaders() {
|
||||||
return responseHeaders;
|
return responseHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Map<String, String> getPostParameters() {
|
public Map<String, String> getPostParameters() {
|
||||||
return postParameters;
|
return postParameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Map<String, String> getRequestHeaders() {
|
public Map<String, String> getRequestHeaders() {
|
||||||
return requestHeaders;
|
return requestHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -642,6 +642,8 @@ public final class FileUtilities {
|
||||||
return buffer.toString();
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final int BUFFER_SIZE = 64 * 1024;
|
||||||
|
|
||||||
public static final long KILO = 1024;
|
public static final long KILO = 1024;
|
||||||
public static final long MEGA = 1024 * KILO;
|
public static final long MEGA = 1024 * KILO;
|
||||||
public static final long GIGA = 1024 * MEGA;
|
public static final long GIGA = 1024 * MEGA;
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package net.filebot.web;
|
package net.filebot.web;
|
||||||
|
|
||||||
|
import static net.filebot.util.FileUtilities.*;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
@ -185,7 +187,7 @@ public final class WebRequest {
|
||||||
in = new InflaterInputStream(in, new Inflater(true));
|
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 {
|
try {
|
||||||
// read all
|
// read all
|
||||||
buffer.transferFully(in);
|
buffer.transferFully(in);
|
||||||
|
|
Loading…
Reference in New Issue