List of usage examples for org.apache.http.nio ContentDecoder isCompleted
boolean isCompleted();
true
if the entity has been received in its entirety. From source file:com.zotoh.maedr.device.apache.StreamingNHttpEntity.java
@Override public void consumeContent(ContentDecoder decoder, IOControl iocontrol) throws IOException { tlog().debug("StreamingNHttpEntity: consumeContent()"); if (!_finished) { sockItDown(decoder);/*w ww.j a v a2 s .c o m*/ } if (decoder.isCompleted()) { finish(); } }
From source file:org.apache.synapse.transport.passthru.SourceRequest.java
/** * Produce the content in to the pipe.//from w w w. j a va 2 s.co m * @param conn the connection * @param decoder content decoder * * @throws java.io.IOException if an error occurs * @return number of bytes read */ public int read(NHttpServerConnection conn, ContentDecoder decoder) throws IOException { if (pipe == null) { throw new IllegalStateException("A Pipe must be connected before calling read"); } if (entityEnclosing) { int bytes = pipe.produce(decoder); if (decoder.isCompleted()) { conn.getContext().setAttribute(PassThroughConstants.REQ_FROM_CLIENT_READ_END_TIME, System.currentTimeMillis()); sourceConfiguration.getMetrics() .notifyReceivedMessageSize(conn.getMetrics().getReceivedBytesCount()); // Update connection state SourceContext.updateState(conn, ProtocolState.REQUEST_DONE); // Suspend client input conn.suspendInput(); } return bytes; } else { throw new IllegalStateException("Only Entity Enclosing Requests " + "can read content in to the pipe"); } }
From source file:org.apache.synapse.transport.passthru.TargetResponseTest.java
/** * Testing reading from a pipe//from w w w.j a v a2 s . com * @throws Exception */ @Test public void testRead() throws Exception { ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration()); WorkerPool workerPool = new NativeWorkerPool(3, 4, 5, 5, "name", "id"); PassThroughTransportMetricsCollector metrics = new PassThroughTransportMetricsCollector(true, "testScheme"); TargetConfiguration targetConfiguration = new TargetConfiguration(configurationContext, null, workerPool, metrics, null); targetConfiguration.build(); HttpResponse response = PowerMockito.mock(HttpResponse.class, Mockito.RETURNS_DEEP_STUBS); NHttpClientConnection conn = PowerMockito.mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS); ContentDecoder decoder = PowerMockito.mock(ContentDecoder.class); TargetConnections connections = PowerMockito.mock(TargetConnections.class); targetConfiguration.setConnections(connections); PowerMockito.mockStatic(TargetContext.class); TargetContext cntxt = new TargetContext(targetConfiguration); PowerMockito.when(TargetContext.get(any(NHttpClientConnection.class))).thenReturn(cntxt); PowerMockito.when(decoder.read(any(ByteBuffer.class))).thenReturn(12); PowerMockito.when(decoder.isCompleted()).thenReturn(true); TargetResponse targetResponse = new TargetResponse(targetConfiguration, response, conn, true, false); targetResponse.start(conn); int result = targetResponse.read(conn, decoder); Assert.assertEquals(12, result); }
From source file:org.apache.synapse.transport.passthru.Pipe.java
/** * Helper method to mark as producer completed. The normal behavior only set the boolean param to true only when * decoder is completed. However in some cases it is needed to intentionally close chunk stream and mark as * producer completed in order to prevent consumer further waiting on end of stream condition. * * @param decoder decoder instance to consume input *//*from w ww. ja va2s . c o m*/ public void forceProducerComplete(final ContentDecoder decoder) { //no need to mark EoS if decoder is completed if (!decoder.isCompleted()) { lock.lock(); try { producerCompleted = true; //there can be edge cases where consumer already waiting on read condition ( buffer is empty ) //in that case marking producer complete is not enough need to clear the read condition //consumer is waiting on readCondition.signalAll(); //let consumer complete = clear consumerIoControl.suspendOutput(); if (consumerIoControl != null) { consumerIoControl.requestOutput(); } } finally { lock.unlock(); } } }
From source file:rx.apache.http.consumers.ResponseConsumerChunked.java
@Override protected final void onContentReceived(final ContentDecoder decoder, final IOControl ioctrl) throws IOException { if (parentSubscription.isUnsubscribed()) { ioctrl.shutdown();/*w ww. jav a2s .c o m*/ } byte[] data; data = new byte[BUFFER_SIZE]; final int bytesRead; bytesRead = decoder.read(ByteBuffer.wrap(data)); if (bytesRead > 0) { if (bytesRead == data.length) { contentSubject.onNext(data); } else { byte[] subset; subset = new byte[bytesRead]; System.arraycopy(data, 0, subset, 0, bytesRead); contentSubject.onNext(subset); } } if (decoder.isCompleted()) { contentSubject.onCompleted(); } }
From source file:org.apache.cxf.transport.http.asyncclient.SharedInputBuffer.java
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();// w ww . j av a 2 s . co m 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.ioctrl.suspendInput(); } this.condition.signalAll(); if (totalRead > 0) { return totalRead; } else { if (this.endOfStream) { return -1; } else { return 0; } } } finally { this.lock.unlock(); } }