Skip to content

Commit 16be4c9

Browse files
committed
Added option to append downloaded data to File if it already exists
1 parent f42c891 commit 16be4c9

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

library/src/main/java/com/loopj/android/http/FileAsyncHttpResponseHandler.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
public abstract class FileAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
3333

3434
protected final File mFile;
35+
protected final boolean append;
3536
private static final String LOG_TAG = "FileAsyncHttpResponseHandler";
3637

3738
/**
@@ -40,9 +41,20 @@ public abstract class FileAsyncHttpResponseHandler extends AsyncHttpResponseHand
4041
* @param file File to store response within, must not be null
4142
*/
4243
public FileAsyncHttpResponseHandler(File file) {
44+
this(file, false);
45+
}
46+
47+
/**
48+
* Obtains new FileAsyncHttpResponseHandler and stores response in passed file
49+
*
50+
* @param file File to store response within, must not be null
51+
* @param append whether data should be appended to existing file
52+
*/
53+
public FileAsyncHttpResponseHandler(File file, boolean append) {
4354
super();
4455
AssertUtils.asserts(file != null, "File passed into FileAsyncHttpResponseHandler constructor must not be null");
4556
this.mFile = file;
57+
this.append = append;
4658
}
4759

4860
/**
@@ -53,6 +65,7 @@ public FileAsyncHttpResponseHandler(File file) {
5365
public FileAsyncHttpResponseHandler(Context context) {
5466
super();
5567
this.mFile = getTemporaryFile(context);
68+
this.append = false;
5669
}
5770

5871
/**
@@ -127,7 +140,7 @@ protected byte[] getResponseData(HttpEntity entity) throws IOException {
127140
if (entity != null) {
128141
InputStream instream = entity.getContent();
129142
long contentLength = entity.getContentLength();
130-
FileOutputStream buffer = new FileOutputStream(getTargetFile());
143+
FileOutputStream buffer = new FileOutputStream(getTargetFile(), this.append);
131144
if (instream != null) {
132145
try {
133146
byte[] tmp = new byte[BUFFER_SIZE];

0 commit comments

Comments
 (0)