@Test public void testWriteTo() throws IOException, InterruptedException { byte[] content = getData(); String contentType = "application/json"; final Semaphore didReportIntermediateProgress = new Semaphore(0); final Semaphore finish = new Semaphore(0); ParseCountingByteArrayHttpBody body = new ParseCountingByteArrayHttpBody( content, contentType, 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); assertTrue(Arrays.equals(content, output.toByteArray())); assertTrue(didReportIntermediateProgress.tryAcquire(5, TimeUnit.SECONDS)); assertTrue(finish.tryAcquire(5, TimeUnit.SECONDS)); }
@Override public void writeTo(OutputStream out) throws IOException { if (out == null) { throw new IllegalArgumentException("Output stream may not be null"); } int position = 0; int totalLength = content.length; while (position < totalLength) { int length = min(totalLength - position, DEFAULT_CHUNK_SIZE); out.write(content, position, length); out.flush(); if (progressCallback != null) { position += length; int progress = 100 * position / totalLength; progressCallback.done(progress); } } }
ParseCountingByteArrayHttpBody extends ParseByteArrayHttpBody { @Override public void writeTo(OutputStream out) throws IOException { if (out == null) { throw new IllegalArgumentException("Output stream may not be null"); } int position = 0; int totalLength = content.length; while (position < totalLength) { int length = min(totalLength - position, DEFAULT_CHUNK_SIZE); out.write(content, position, length); out.flush(); if (progressCallback != null) { position += length; int progress = 100 * position / totalLength; progressCallback.done(progress); } } } }
ParseCountingByteArrayHttpBody extends ParseByteArrayHttpBody { @Override public void writeTo(OutputStream out) throws IOException { if (out == null) { throw new IllegalArgumentException("Output stream may not be null"); } int position = 0; int totalLength = content.length; while (position < totalLength) { int length = min(totalLength - position, DEFAULT_CHUNK_SIZE); out.write(content, position, length); out.flush(); if (progressCallback != null) { position += length; int progress = 100 * position / totalLength; progressCallback.done(progress); } } } ParseCountingByteArrayHttpBody(byte[] content, String contentType,
                                          final ProgressCallback progressCallback); }
ParseCountingByteArrayHttpBody extends ParseByteArrayHttpBody { @Override public void writeTo(OutputStream out) throws IOException { if (out == null) { throw new IllegalArgumentException("Output stream may not be null"); } int position = 0; int totalLength = content.length; while (position < totalLength) { int length = min(totalLength - position, DEFAULT_CHUNK_SIZE); out.write(content, position, length); out.flush(); if (progressCallback != null) { position += length; int progress = 100 * position / totalLength; progressCallback.done(progress); } } } ParseCountingByteArrayHttpBody(byte[] content, String contentType,
                                          final ProgressCallback progressCallback); @Override void writeTo(OutputStream out); }
ParseCountingByteArrayHttpBody extends ParseByteArrayHttpBody { @Override public void writeTo(OutputStream out) throws IOException { if (out == null) { throw new IllegalArgumentException("Output stream may not be null"); } int position = 0; int totalLength = content.length; while (position < totalLength) { int length = min(totalLength - position, DEFAULT_CHUNK_SIZE); out.write(content, position, length); out.flush(); if (progressCallback != null) { position += length; int progress = 100 * position / totalLength; progressCallback.done(progress); } } } ParseCountingByteArrayHttpBody(byte[] content, String contentType,
                                          final ProgressCallback progressCallback); @Override void writeTo(OutputStream out);  }