Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support resuming of file transfers #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
Expand Down Expand Up @@ -196,6 +197,11 @@ public class HttpRequest {
* 'Referer' header name
*/
public static final String HEADER_REFERER = "Referer";

/**
* 'Range' header name
*/
public static final String HEADER_RANGE = "Range";

/**
* 'Server' header name
Expand Down Expand Up @@ -1960,6 +1966,33 @@ protected HttpRequest run() throws HttpRequestException, IOException {
}
}.call();
}

/**
* * Stream response body to file
*
* @param file
* @param pos
* @return this request
* @throws HttpRequestException
*/
public HttpRequest receive(final File file, long pos) throws HttpRequestException {
final RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "rw");
raf.seek(pos);
} catch (FileNotFoundException e) {
throw new HttpRequestException(e);
} catch (IOException e) {
throw new HttpRequestException(e);
}
return new CloseOperation<HttpRequest>(raf, ignoreCloseExceptions) {

@Override
protected HttpRequest run() throws HttpRequestException, IOException {
return receive(raf);
}
}.call();
}

/**
* Stream response to given output stream
Expand All @@ -1976,6 +2009,22 @@ public HttpRequest receive(final OutputStream output)
throw new HttpRequestException(e);
}
}

/**
* Stream response to given RandomAccessFile
*
* @param output
* @return this request
* @throws HttpRequestException
*/
public HttpRequest receive(final RandomAccessFile raf)
throws HttpRequestException {
try {
return copy(buffer(), raf);
} catch (IOException e) {
throw new HttpRequestException(e);
}
}

/**
* Stream response to given print stream
Expand Down Expand Up @@ -2379,6 +2428,28 @@ public HttpRequest acceptGzipEncoding() {
public HttpRequest acceptCharset(final String acceptCharset) {
return header(HEADER_ACCEPT_CHARSET, acceptCharset);
}

/**
* Set the 'Range' header to given value
*
* @param startBytes
* @param endBytes
* @return this request
*/
public HttpRequest range(final int startBytes, final int endBytes) {
return header(HEADER_RANGE, "bytes=" + startBytes + "-" + ((endBytes > 0) ? endBytes : ""));
}

/**
* Set the 'Range' header to given value
*
* @param startBytes
* @return this request
*/
public HttpRequest range(final int startBytes) {
return range(startBytes, -1);
}


/**
* Get the 'Content-Encoding' header from the response
Expand Down Expand Up @@ -2625,6 +2696,32 @@ public HttpRequest run() throws IOException {
}
}.call();
}

/**
* Copy from input stream to RandomAccessFile
*
* @param input
* @param output
* @return this request
* @throws IOException
*/
protected HttpRequest copy(final InputStream input, final RandomAccessFile raf)
throws IOException {
return new CloseOperation<HttpRequest>(input, ignoreCloseExceptions) {

@Override
public HttpRequest run() throws IOException {
final byte[] buffer = new byte[bufferSize];
int read;
while ((read = input.read(buffer)) != -1) {
raf.write(buffer, 0, read);
totalWritten += read;
progress.onUpload(totalWritten, totalSize);
}
return HttpRequest.this;
}
}.call();
}

/**
* Copy from reader to writer
Expand Down