下面列出了怎么用org.apache.http.nio.ContentDecoder的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void consumeContent(final ContentDecoder dec, final IOControl ioc) throws IOException {
// Only consume content when the work was accepted by the work queue
if (outstream.retrySetHttpResponse(response)) {
buf.consumeContent(dec, ioc);
}
}
protected void onContentReceived(ContentDecoder decoder, IOControl ioControl) throws IOException {
this.buf.consumeContent(decoder);
}
@Override
protected void onContentReceived(
final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
this.buffer.getSource().consumeContent(decoder);
}
@Override
protected void onContentReceived(final ContentDecoder decoder,
final IOControl ioctrl) throws IOException {
Preconditions.checkNotNull(this.buf, "Content buffer should not be null.");
this.buf.consumeContent(decoder);
}
@Override
public void consumeContent(final ContentDecoder decoder, final IOControl control) throws IOException {
delegate().consumeContent(decoder, control);
}
@Override
public void consumeContent(ContentDecoder decoder, IOControl ioctrl) throws IOException {
consumer.consumeContent(decoder, ioctrl);
}
public int consumeContent(final ContentDecoder decoder, final IOControl ioc) throws IOException {
if (this.shutdown) {
//something bad happened, we need to shutdown the connection
//as we're not going to read the data at all and we
//don't want to keep getting read notices and such
ioc.shutdown();
return -1;
}
this.lock.lock();
try {
this.ioctrl = ioc;
setInputMode();
int totalRead = 0;
int bytesRead;
if (waitingBuffer != null && this.buffer.position() == 0) {
while ((bytesRead = decoder.read(this.waitingBuffer)) > 0) {
totalRead += bytesRead;
}
}
//read more
while ((bytesRead = decoder.read(this.buffer)) > 0) {
totalRead += bytesRead;
}
if (bytesRead == -1 || decoder.isCompleted()) {
this.endOfStream = true;
}
if (!this.buffer.hasRemaining() && this.ioctrl != null && !this.endOfStream) {
this.ioctrl.suspendInput();
}
this.condition.signalAll();
if (totalRead > 0) {
return totalRead;
}
if (this.endOfStream) {
return -1;
}
return 0;
} finally {
this.lock.unlock();
}
}
@Override public void consumeContent(ContentDecoder decoder, IOControl ioctrl)
throws IOException {
responseConsumer.consumeContent(decoder, ioctrl);
}
@Test
public void onContentReceivedPassedDecoderToBuffer() throws IOException, PoolResourceException {
// given
ItemSourcePool<SimpleInputBuffer> itemSourcePool = mock(ItemSourcePool.class);
ItemSource<SimpleInputBuffer> itemSource = mock(ItemSource.class);
when(itemSourcePool.getPooled()).thenReturn(itemSource);
SimpleInputBuffer buffer = mock(SimpleInputBuffer.class);
when(itemSource.getSource()).thenReturn(buffer);
PoolingAsyncResponseConsumer consumer = createDefaultTestObject(itemSourcePool);
consumer.onResponseReceived(mock(HttpResponse.class));
HttpEntity httpEntity = mock(HttpEntity.class);
consumer.onEntityEnclosed(httpEntity, ContentType.create("application/json"));
ContentDecoder contentDecoder = mock(ContentDecoder.class);
// when
consumer.onContentReceived(contentDecoder, mock(IOControl.class));
// then
verify(buffer, times(1)).consumeContent(eq(contentDecoder));
}