private void fireReceived(HttpResponseBodyPart content) { int remaining = content.getBodyByteBuffer().remaining(); amount += remaining; if (listener != null) { listener.onBytesReceived(uri, amount, remaining, total); } }
@Override public State onBodyPartReceived(final HttpResponseBodyPart content) throws Exception { byteReceived.addAndGet(content.getBodyByteBuffer().capacity()); return super.onBodyPartReceived(content); }
/** * {@inheritDoc} */ public State onBodyPartReceived(final HttpResponseBodyPart content) throws Exception { fireReceived(content); if (omitBody) { return State.CONTINUE; } if (!accumulateBody && bodyConsumer != null) { bodyConsumer.consume(content.getBodyByteBuffer()); } else { return super.onBodyPartReceived(content); } return State.CONTINUE; }
@Test public void testOnBodyPartReceived() throws Exception { ResumableAsyncHandler handler = new ResumableAsyncHandler(); HttpResponseBodyPart bodyPart = mock(HttpResponseBodyPart.class); when(bodyPart.getBodyPartBytes()).thenReturn(new byte[0]); ByteBuffer buffer = ByteBuffer.allocate(0); when(bodyPart.getBodyByteBuffer()).thenReturn(buffer); State state = handler.onBodyPartReceived(bodyPart); assertEquals(state, AsyncHandler.State.CONTINUE, "State should be CONTINUE for a successful onBodyPartReceived"); }
@Override public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { if (accumulateBody) { responseBuilder.accumulate(bodyPart); } State state = State.CONTINUE; try { resumableListener.onBytesReceived(bodyPart.getBodyByteBuffer()); } catch (IOException ex) { return AsyncHandler.State.ABORT; } if (decoratedAsyncHandler != null) { state = decoratedAsyncHandler.onBodyPartReceived(bodyPart); } byteTransferred.addAndGet(bodyPart.getBodyPartBytes().length); resumableProcessor.put(url, byteTransferred.get()); return state; }
@Test public void testOnBodyPartReceivedWithDecoratedAsyncHandler() throws Exception { HttpResponseBodyPart bodyPart = mock(HttpResponseBodyPart.class); when(bodyPart.getBodyPartBytes()).thenReturn(new byte[0]); ByteBuffer buffer = ByteBuffer.allocate(0); when(bodyPart.getBodyByteBuffer()).thenReturn(buffer); @SuppressWarnings("unchecked") AsyncHandler<Response> decoratedAsyncHandler = mock(AsyncHandler.class); when(decoratedAsyncHandler.onBodyPartReceived(bodyPart)).thenReturn(State.CONTINUE); // following is needed to set the url variable HttpResponseStatus mockResponseStatus = mock(HttpResponseStatus.class); when(mockResponseStatus.getStatusCode()).thenReturn(200); Uri uri = Uri.create("http://non.null"); when(mockResponseStatus.getUri()).thenReturn(uri); ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler); handler.onStatusReceived(mockResponseStatus); State state = handler.onBodyPartReceived(bodyPart); assertEquals(state, State.CONTINUE, "State should be equal to the state returned from decoratedAsyncHandler"); }
private void fireReceived(HttpResponseBodyPart content) { int remaining = content.getBodyByteBuffer().remaining(); amount += remaining; if (listener != null) { listener.onBytesReceived(uri, amount, remaining, total); } }
@Override public State onBodyPartReceived(final HttpResponseBodyPart bodyPart) throws Exception { if (nonNull(bodyPart) && nonNull(bodyPart.getBodyByteBuffer())) { bytesSent += bodyPart.getBodyPartBytes().length; isLast = bodyPart.isLast(); context.getResponse().getOutputStream().write(bodyPart.getBodyPartBytes()); } return State.CONTINUE; }
/** * {@inheritDoc} */ public State onBodyPartReceived(final HttpResponseBodyPart content) throws Exception { fireReceived(content); if (omitBody) { return State.CONTINUE; } if (!accumulateBody && bodyConsumer != null) { bodyConsumer.consume(content.getBodyByteBuffer()); } else { return super.onBodyPartReceived(content); } return State.CONTINUE; }
/** * {@inheritDoc} */ @Override public AsyncHandler.State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { if (accumulateBody) { responseBuilder.accumulate(bodyPart); } State state = State.CONTINUE; try { resumableListener.onBytesReceived(bodyPart.getBodyByteBuffer()); } catch (IOException ex) { return AsyncHandler.State.ABORT; } if (decoratedAsyncHandler != null) { state = decoratedAsyncHandler.onBodyPartReceived(bodyPart); } byteTransferred.addAndGet(bodyPart.getBodyPartBytes().length); resumableProcessor.put(url, byteTransferred.get()); return state; }