@Test public void givenSameKeyAndIV_whenIWriteToBuffer_thenIExpectSameContents() throws Exception { DigestingRequestBody fromStart = getBody(0); DigestingRequestBody fromMiddle = getBody(CONTENT_LENGTH / 2); byte[] cipher1; try (Buffer buffer = new Buffer()) { fromStart.writeTo(buffer); cipher1 = buffer.readByteArray(); } byte[] cipher2; try (Buffer buffer = new Buffer()) { fromMiddle.writeTo(buffer); cipher2 = buffer.readByteArray(); } assertEquals(cipher1.length, TOTAL_LENGTH); assertEquals(cipher2.length, TOTAL_LENGTH - (CONTENT_LENGTH / 2)); for (int i = 0; i < cipher2.length; i++) { assertEquals(cipher2[i], cipher1[i + (CONTENT_LENGTH / 2)]); } }
@Override public void writeTo(BufferedSink sink) throws IOException { DigestingOutputStream outputStream = outputStreamFactory.createFor(new SkippingOutputStream(contentStart, sink.outputStream())); byte[] buffer = new byte[8192]; int read; long total = 0; while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) { if (cancelationSignal != null && cancelationSignal.isCanceled()) { throw new IOException("Canceled!"); } outputStream.write(buffer, 0, read); total += read; if (progressListener != null) { progressListener.onAttachmentProgress(contentLength, total); } } outputStream.flush(); digest = outputStream.getTransmittedDigest(); }
DigestingRequestBody extends RequestBody { @Override public void writeTo(BufferedSink sink) throws IOException { DigestingOutputStream outputStream = outputStreamFactory.createFor(new SkippingOutputStream(contentStart, sink.outputStream())); byte[] buffer = new byte[8192]; int read; long total = 0; while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) { if (cancelationSignal != null && cancelationSignal.isCanceled()) { throw new IOException("Canceled!"); } outputStream.write(buffer, 0, read); total += read; if (progressListener != null) { progressListener.onAttachmentProgress(contentLength, total); } } outputStream.flush(); digest = outputStream.getTransmittedDigest(); } }
DigestingRequestBody extends RequestBody { @Override public void writeTo(BufferedSink sink) throws IOException { DigestingOutputStream outputStream = outputStreamFactory.createFor(new SkippingOutputStream(contentStart, sink.outputStream())); byte[] buffer = new byte[8192]; int read; long total = 0; while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) { if (cancelationSignal != null && cancelationSignal.isCanceled()) { throw new IOException("Canceled!"); } outputStream.write(buffer, 0, read); total += read; if (progressListener != null) { progressListener.onAttachmentProgress(contentLength, total); } } outputStream.flush(); digest = outputStream.getTransmittedDigest(); } DigestingRequestBody(InputStream inputStream,
                              OutputStreamFactory outputStreamFactory,
                              String contentType, long contentLength,
                              ProgressListener progressListener,
                              CancelationSignal cancelationSignal,
                              long contentStart); }
DigestingRequestBody extends RequestBody { @Override public void writeTo(BufferedSink sink) throws IOException { DigestingOutputStream outputStream = outputStreamFactory.createFor(new SkippingOutputStream(contentStart, sink.outputStream())); byte[] buffer = new byte[8192]; int read; long total = 0; while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) { if (cancelationSignal != null && cancelationSignal.isCanceled()) { throw new IOException("Canceled!"); } outputStream.write(buffer, 0, read); total += read; if (progressListener != null) { progressListener.onAttachmentProgress(contentLength, total); } } outputStream.flush(); digest = outputStream.getTransmittedDigest(); } DigestingRequestBody(InputStream inputStream,
                              OutputStreamFactory outputStreamFactory,
                              String contentType, long contentLength,
                              ProgressListener progressListener,
                              CancelationSignal cancelationSignal,
                              long contentStart); @Override MediaType contentType(); @Override void writeTo(BufferedSink sink); @Override long contentLength(); byte[] getTransmittedDigest(); }
DigestingRequestBody extends RequestBody { @Override public void writeTo(BufferedSink sink) throws IOException { DigestingOutputStream outputStream = outputStreamFactory.createFor(new SkippingOutputStream(contentStart, sink.outputStream())); byte[] buffer = new byte[8192]; int read; long total = 0; while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) { if (cancelationSignal != null && cancelationSignal.isCanceled()) { throw new IOException("Canceled!"); } outputStream.write(buffer, 0, read); total += read; if (progressListener != null) { progressListener.onAttachmentProgress(contentLength, total); } } outputStream.flush(); digest = outputStream.getTransmittedDigest(); } DigestingRequestBody(InputStream inputStream,
                              OutputStreamFactory outputStreamFactory,
                              String contentType, long contentLength,
                              ProgressListener progressListener,
                              CancelationSignal cancelationSignal,
                              long contentStart); @Override MediaType contentType(); @Override void writeTo(BufferedSink sink); @Override long contentLength(); byte[] getTransmittedDigest();  }