Skip to content

Commit b0dbb87

Browse files
committed
Add ParseFileHttpBody and ParseCountingFileHttpBody
1 parent 44d3133 commit b0dbb87

File tree

6 files changed

+291
-5
lines changed

6 files changed

+291
-5
lines changed

Parse/src/main/java/com/parse/ParseApacheHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public ParseApacheHttpClient(int socketOperationTimeout, SSLSessionCache sslSess
212212
private static class ParseApacheHttpEntity extends InputStreamEntity {
213213
private ParseHttpBody parseBody;
214214

215-
public ParseApacheHttpEntity(ParseHttpBody parseBody) {
215+
public ParseApacheHttpEntity(ParseHttpBody parseBody) throws IOException {
216216
super(parseBody.getContent(), parseBody.getContentLength());
217217
super.setContentType(parseBody.getContentType());
218218
this.parseBody = parseBody;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
package com.parse;
10+
11+
import java.io.File;
12+
import java.io.FileInputStream;
13+
import java.io.IOException;
14+
import java.io.OutputStream;
15+
16+
/** package */ class ParseCountingFileHttpBody extends ParseFileHttpBody {
17+
18+
private static final int DEFAULT_CHUNK_SIZE = 4096;
19+
private static final int EOF = -1;
20+
21+
private final ProgressCallback progressCallback;
22+
23+
public ParseCountingFileHttpBody(File file, ProgressCallback progressCallback) {
24+
this(file, null, progressCallback);
25+
}
26+
27+
public ParseCountingFileHttpBody(
28+
File file, String contentType, ProgressCallback progressCallback) {
29+
super(file, contentType);
30+
this.progressCallback = progressCallback;
31+
}
32+
33+
@Override
34+
public void writeTo(OutputStream output) throws IOException {
35+
if (output == null) {
36+
throw new IllegalArgumentException("Output stream may not be null");
37+
}
38+
39+
final FileInputStream fileInput = new FileInputStream(file);;
40+
try {
41+
byte[] buffer = new byte[DEFAULT_CHUNK_SIZE];
42+
int n;
43+
long totalLength = file.length();
44+
long position = 0;
45+
while (EOF != (n = fileInput.read(buffer))) {
46+
output.write(buffer, 0, n);
47+
position += n;
48+
49+
if (progressCallback != null) {
50+
int progress = (int) (100 * position / totalLength);
51+
progressCallback.done(progress);
52+
}
53+
}
54+
} finally {
55+
ParseIOUtils.closeQuietly(fileInput);
56+
}
57+
}
58+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
package com.parse;
10+
11+
import java.io.File;
12+
import java.io.FileInputStream;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.io.OutputStream;
16+
17+
/** package */ class ParseFileHttpBody extends ParseHttpBody {
18+
19+
/* package */ final File file;
20+
21+
public ParseFileHttpBody(File file) {
22+
this(file, null);
23+
}
24+
25+
public ParseFileHttpBody(File file, String contentType) {
26+
super(contentType, file.length());
27+
this.file = file;
28+
}
29+
30+
@Override
31+
public InputStream getContent() throws IOException {
32+
return new FileInputStream(file);
33+
}
34+
35+
@Override
36+
public void writeTo(OutputStream out) throws IOException {
37+
if (out == null) {
38+
throw new IllegalArgumentException("Output stream can not be null");
39+
}
40+
41+
final FileInputStream fileInput = new FileInputStream(file);
42+
try {
43+
ParseIOUtils.copy(fileInput, out);
44+
} finally {
45+
ParseIOUtils.closeQuietly(fileInput);
46+
}
47+
}
48+
}

Parse/src/main/java/com/parse/ParseHttpBody.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
*/
1919
/** package */ abstract class ParseHttpBody {
2020
protected final String contentType;
21-
protected final int contentLength;
21+
protected final long contentLength;
2222

23-
public abstract InputStream getContent();
23+
public abstract InputStream getContent() throws IOException;
2424
public abstract void writeTo(OutputStream out) throws IOException;
2525

26-
public ParseHttpBody(String contentType, int contentLength) {
26+
public ParseHttpBody(String contentType, long contentLength) {
2727
this.contentType = contentType;
2828
this.contentLength = contentLength;
2929
}
3030

31-
public int getContentLength() {
31+
public long getContentLength() {
3232
return contentLength;
3333
}
3434

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
package com.parse;
10+
11+
import org.junit.Rule;
12+
import org.junit.Test;
13+
import org.junit.rules.TemporaryFolder;
14+
15+
import java.io.ByteArrayOutputStream;
16+
import java.io.File;
17+
import java.io.FileWriter;
18+
import java.io.IOException;
19+
import java.util.Arrays;
20+
import java.util.concurrent.Semaphore;
21+
import java.util.concurrent.TimeUnit;
22+
23+
import static org.junit.Assert.assertArrayEquals;
24+
import static org.junit.Assert.assertTrue;
25+
import static org.junit.Assert.fail;
26+
27+
public class ParseCountingFileHttpBodyTest {
28+
29+
@Rule
30+
public TemporaryFolder temporaryFolder = new TemporaryFolder();
31+
32+
@Test
33+
public void testWriteTo() throws Exception {
34+
final Semaphore didReportIntermediateProgress = new Semaphore(0);
35+
final Semaphore finish = new Semaphore(0);
36+
37+
ParseCountingFileHttpBody body = new ParseCountingFileHttpBody(
38+
makeTestFile(temporaryFolder.getRoot()), new ProgressCallback() {
39+
Integer maxProgressSoFar = 0;
40+
@Override
41+
public void done(Integer percentDone) {
42+
if (percentDone > maxProgressSoFar) {
43+
maxProgressSoFar = percentDone;
44+
assertTrue(percentDone >= 0 && percentDone <= 100);
45+
46+
if (percentDone < 100 && percentDone > 0) {
47+
didReportIntermediateProgress.release();
48+
} else if (percentDone == 100) {
49+
finish.release();
50+
} else if (percentDone == 0) {
51+
// do nothing
52+
} else {
53+
fail("percentDone should be within 0 - 100");
54+
}
55+
}
56+
}
57+
});
58+
59+
// Check content
60+
ByteArrayOutputStream output = new ByteArrayOutputStream();
61+
body.writeTo(output);
62+
assertArrayEquals(getData().getBytes(), output.toByteArray());
63+
// Check progress callback
64+
assertTrue(didReportIntermediateProgress.tryAcquire(5, TimeUnit.SECONDS));
65+
assertTrue(finish.tryAcquire(5, TimeUnit.SECONDS));
66+
}
67+
68+
@Test(expected = IllegalArgumentException.class)
69+
public void testWriteToWithNullOutput() throws Exception {
70+
ParseCountingFileHttpBody body = new ParseCountingFileHttpBody(
71+
makeTestFile(temporaryFolder.getRoot()), null);
72+
body.writeTo(null);
73+
}
74+
75+
private static String getData() {
76+
char[] chars = new char[64 << 14]; // 1MB
77+
Arrays.fill(chars, '1');
78+
return new String(chars);
79+
}
80+
81+
private static File makeTestFile(File root) throws IOException {
82+
File file = new File(root, "test");
83+
FileWriter writer = new FileWriter(file);
84+
writer.write(getData());
85+
writer.close();
86+
return file;
87+
}
88+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
package com.parse;
10+
11+
import org.junit.Rule;
12+
import org.junit.Test;
13+
import org.junit.rules.TemporaryFolder;
14+
15+
import java.io.ByteArrayOutputStream;
16+
import java.io.File;
17+
import java.io.FileWriter;
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
21+
import static org.junit.Assert.assertArrayEquals;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertNull;
24+
25+
public class ParseFileHttpBodyTest {
26+
27+
@Rule
28+
public TemporaryFolder temporaryFolder = new TemporaryFolder();
29+
30+
@Test
31+
public void testInitializeWithFileAndContentType() throws IOException {
32+
String contentType = "text/plain";
33+
File file = makeTestFile(temporaryFolder.getRoot());
34+
35+
ParseFileHttpBody body = new ParseFileHttpBody(file, contentType);
36+
37+
assertEquals(file.length(), body.getContentLength());
38+
assertEquals(contentType, body.getContentType());
39+
// Verify file content
40+
InputStream content = body.getContent();
41+
byte[] contentBytes = ParseIOUtils.toByteArray(content);
42+
ParseIOUtils.closeQuietly(content);
43+
verifyTestFileContent(contentBytes);
44+
}
45+
46+
@Test
47+
public void testInitializeWithFile() throws IOException {
48+
File file = makeTestFile(temporaryFolder.getRoot());
49+
50+
ParseFileHttpBody body = new ParseFileHttpBody(file);
51+
52+
assertEquals(file.length(), body.getContentLength());
53+
assertNull(body.getContentType());
54+
// Verify file content
55+
InputStream content = body.getContent();
56+
byte[] contentBytes = ParseIOUtils.toByteArray(content);
57+
ParseIOUtils.closeQuietly(content);
58+
verifyTestFileContent(contentBytes);
59+
}
60+
61+
@Test
62+
public void testWriteTo() throws IOException {
63+
File file = makeTestFile(temporaryFolder.getRoot());
64+
ParseFileHttpBody body = new ParseFileHttpBody(file);
65+
66+
// Check content
67+
ByteArrayOutputStream output = new ByteArrayOutputStream();
68+
body.writeTo(output);
69+
verifyTestFileContent(output.toByteArray());
70+
}
71+
72+
@Test(expected = IllegalArgumentException.class)
73+
public void testWriteToWithNullOutput() throws Exception {
74+
ParseFileHttpBody body = new ParseFileHttpBody(makeTestFile(temporaryFolder.getRoot()));
75+
body.writeTo(null);
76+
}
77+
78+
// Generate a test file used for create ParseFileHttpBody, if you change file's content, make sure
79+
// you also change the test file content in verifyTestFileContent().
80+
private static File makeTestFile(File root) throws IOException {
81+
File file = new File(root, "test");
82+
String content = "content";
83+
FileWriter writer = new FileWriter(file);
84+
writer.write(content);
85+
writer.close();
86+
return file;
87+
}
88+
89+
private static void verifyTestFileContent(byte[] bytes) throws IOException {
90+
assertArrayEquals("content".getBytes(), bytes);
91+
}
92+
}

0 commit comments

Comments
 (0)