-
-
Notifications
You must be signed in to change notification settings - Fork 735
Add ParseFileHttpBody and ParseCountingFileHttpBody #70
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
Merged
wangmengyan95
merged 1 commit into
master
from
wangmengyan.t8145272_add_ParseFileHttpBody
Aug 28, 2015
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Parse/src/main/java/com/parse/ParseCountingFileHttpBody.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2015-present, Parse, LLC. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
package com.parse; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
/** package */ class ParseCountingFileHttpBody extends ParseFileHttpBody { | ||
|
||
private static final int DEFAULT_CHUNK_SIZE = 4096; | ||
private static final int EOF = -1; | ||
|
||
private final ProgressCallback progressCallback; | ||
|
||
public ParseCountingFileHttpBody(File file, ProgressCallback progressCallback) { | ||
this(file, null, progressCallback); | ||
} | ||
|
||
public ParseCountingFileHttpBody( | ||
File file, String contentType, ProgressCallback progressCallback) { | ||
super(file, contentType); | ||
this.progressCallback = progressCallback; | ||
} | ||
|
||
@Override | ||
public void writeTo(OutputStream output) throws IOException { | ||
if (output == null) { | ||
throw new IllegalArgumentException("Output stream may not be null"); | ||
} | ||
|
||
final FileInputStream fileInput = new FileInputStream(file);; | ||
try { | ||
byte[] buffer = new byte[DEFAULT_CHUNK_SIZE]; | ||
int n; | ||
long totalLength = file.length(); | ||
long position = 0; | ||
while (EOF != (n = fileInput.read(buffer))) { | ||
output.write(buffer, 0, n); | ||
position += n; | ||
|
||
if (progressCallback != null) { | ||
int progress = (int) (100 * position / totalLength); | ||
progressCallback.done(progress); | ||
} | ||
} | ||
} finally { | ||
ParseIOUtils.closeQuietly(fileInput); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2015-present, Parse, LLC. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
package com.parse; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
/** package */ class ParseFileHttpBody extends ParseHttpBody { | ||
|
||
/* package */ final File file; | ||
|
||
public ParseFileHttpBody(File file) { | ||
this(file, null); | ||
} | ||
|
||
public ParseFileHttpBody(File file, String contentType) { | ||
super(contentType, file.length()); | ||
this.file = file; | ||
} | ||
|
||
@Override | ||
public InputStream getContent() throws IOException { | ||
return new FileInputStream(file); | ||
} | ||
|
||
@Override | ||
public void writeTo(OutputStream out) throws IOException { | ||
if (out == null) { | ||
throw new IllegalArgumentException("Output stream can not be null"); | ||
} | ||
|
||
final FileInputStream fileInput = new FileInputStream(file); | ||
try { | ||
ParseIOUtils.copy(fileInput, out); | ||
} finally { | ||
ParseIOUtils.closeQuietly(fileInput); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Parse/src/test/java/com/parse/ParseCountingFileHttpBodyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (c) 2015-present, Parse, LLC. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
package com.parse; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
public class ParseCountingFileHttpBodyTest { | ||
|
||
@Rule | ||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
@Test | ||
public void testWriteTo() throws Exception { | ||
final Semaphore didReportIntermediateProgress = new Semaphore(0); | ||
final Semaphore finish = new Semaphore(0); | ||
|
||
ParseCountingFileHttpBody body = new ParseCountingFileHttpBody( | ||
makeTestFile(temporaryFolder.getRoot()), new ProgressCallback() { | ||
Integer maxProgressSoFar = 0; | ||
@Override | ||
public void done(Integer percentDone) { | ||
if (percentDone > maxProgressSoFar) { | ||
maxProgressSoFar = percentDone; | ||
assertTrue(percentDone >= 0 && percentDone <= 100); | ||
|
||
if (percentDone < 100 && percentDone > 0) { | ||
didReportIntermediateProgress.release(); | ||
} else if (percentDone == 100) { | ||
finish.release(); | ||
} else if (percentDone == 0) { | ||
// do nothing | ||
} else { | ||
fail("percentDone should be within 0 - 100"); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// Check content | ||
ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
body.writeTo(output); | ||
assertArrayEquals(getData().getBytes(), output.toByteArray()); | ||
// Check progress callback | ||
assertTrue(didReportIntermediateProgress.tryAcquire(5, TimeUnit.SECONDS)); | ||
assertTrue(finish.tryAcquire(5, TimeUnit.SECONDS)); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testWriteToWithNullOutput() throws Exception { | ||
ParseCountingFileHttpBody body = new ParseCountingFileHttpBody( | ||
makeTestFile(temporaryFolder.getRoot()), null); | ||
body.writeTo(null); | ||
} | ||
|
||
private static String getData() { | ||
char[] chars = new char[64 << 14]; // 1MB | ||
Arrays.fill(chars, '1'); | ||
return new String(chars); | ||
} | ||
|
||
private static File makeTestFile(File root) throws IOException { | ||
File file = new File(root, "test"); | ||
FileWriter writer = new FileWriter(file); | ||
writer.write(getData()); | ||
writer.close(); | ||
return file; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since parseBody.getContent() will try to create a fileStream in some cases, so we need to throw IOException here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍