@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) { } else { fail("percentDone should be within 0 - 100"); } } } }); ByteArrayOutputStream output = new ByteArrayOutputStream(); body.writeTo(output); assertArrayEquals(getData().getBytes(), output.toByteArray()); assertTrue(didReportIntermediateProgress.tryAcquire(5, TimeUnit.SECONDS)); assertTrue(finish.tryAcquire(5, TimeUnit.SECONDS)); }
@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); } }
ParseCountingFileHttpBody extends ParseFileHttpBody { @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); } } }
ParseCountingFileHttpBody extends ParseFileHttpBody { @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); } } ParseCountingFileHttpBody(File file, ProgressCallback progressCallback);  ParseCountingFileHttpBody(
            File file, String contentType, ProgressCallback progressCallback); }
ParseCountingFileHttpBody extends ParseFileHttpBody { @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); } } ParseCountingFileHttpBody(File file, ProgressCallback progressCallback);  ParseCountingFileHttpBody(
            File file, String contentType, ProgressCallback progressCallback); @Override void writeTo(OutputStream output); }
ParseCountingFileHttpBody extends ParseFileHttpBody { @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); } } ParseCountingFileHttpBody(File file, ProgressCallback progressCallback);  ParseCountingFileHttpBody(
            File file, String contentType, ProgressCallback progressCallback); @Override void writeTo(OutputStream output);  }