List of usage examples for org.apache.http.nio ContentDecoder read
int read(ByteBuffer dst) throws IOException;
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();/*from w w w . j a v a 2 s.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:com.zotoh.maedr.device.apache.StreamingNHttpEntity.java
private void sockItDown(ContentDecoder decoder) throws IOException { tlog().debug("StreamingNHttpEntity: sockItDown()"); ByteBuffer buffer;/*from w ww . j a va 2 s .c om*/ int cnt; buffer = _alloctor.allocate(4096); do { buffer.clear(); if ((cnt = decoder.read(buffer)) == -1) break; if (cnt == 0) { if (buffer.hasRemaining()) break; else continue; } // buffer.flip(); byte[] bits = new byte[4096]; int len; while (buffer.hasRemaining()) { len = Math.min(4096, buffer.remaining()); buffer.get(bits, 0, len); storeBytes(bits, len); } } while (true); }
From source file:org.apache.synapse.transport.passthru.TargetResponseTest.java
/** * Testing reading from a pipe/*from w w w . j a v a 2 s . c om*/ * @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.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();//from ww w . j av a 2 s . com 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(); } }
From source file:org.apache.synapse.transport.passthru.Pipe.java
/** * Produce data in to the buffer.//from www . jav a 2 s .c o m * * @param decoder decoder to read bytes from the underlying stream * @return bytes read (consumed) * @throws IOException if an error occurs while reading data */ public int produce(final ContentDecoder decoder) throws IOException { if (producerIoControl == null) { throw new IllegalStateException("Producer cannot be null when calling produce"); } lock.lock(); try { setInputMode(buffer); int bytesRead = 0; try { bytesRead = decoder.read(buffer.getByteBuffer()); } catch (MalformedChunkCodingException ignore) { // we assume that this is a truncated chunk, hence simply ignore the exception // https://issues.apache.org/jira/browse/HTTPCORE-195 // we should add the EoF character buffer.putInt(-1); // now the buffer's position should give us the bytes read. bytesRead = buffer.position(); } // if consumer is at error we have to let the producer complete if (consumerError) { buffer.clear(); } if (!buffer.hasRemaining()) { // Input buffer is full. Suspend client input // until the origin handler frees up some space in the buffer producerIoControl.suspendInput(); } // If there is some content in the input buffer make sure consumer output is active if (buffer.position() > 0 || decoder.isCompleted()) { if (consumerIoControl != null) { consumerIoControl.requestOutput(); } readCondition.signalAll(); } if (decoder.isCompleted()) { producerCompleted = true; } return bytesRead; } finally { lock.unlock(); } }
From source file:net.kungfoo.grizzly.proxy.impl.ConnectingHandler.java
public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) { System.out.println(conn + " [proxy<-origin] input ready"); HttpContext context = conn.getContext(); ProxyProcessingInfo proxyTask = (ProxyProcessingInfo) context.getAttribute(ProxyProcessingInfo.ATTRIB); synchronized (proxyTask) { ConnState connState = proxyTask.getOriginState(); if (connState != ConnState.RESPONSE_RECEIVED && connState != ConnState.RESPONSE_BODY_STREAM) { throw new IllegalStateException("Illegal target connection state: " + connState); }//from ww w. ja v a 2 s . c o m final Response response = proxyTask.getResponse(); try { ByteBuffer dst = proxyTask.getOutBuffer(); int bytesRead = decoder.read(dst); if (bytesRead > 0) { dst.flip(); final ByteChunk chunk = new ByteChunk(bytesRead); final byte[] buf = new byte[bytesRead]; dst.get(buf); chunk.setBytes(buf, 0, bytesRead); dst.compact(); try { response.doWrite(chunk); } catch (ClassCastException e) { System.err.println("gone bad: " + e.getMessage()); e.printStackTrace(System.err); } response.flush(); System.out.println(conn + " [proxy<-origin] " + bytesRead + " bytes read"); System.out.println(conn + " [proxy<-origin] " + decoder); } if (!dst.hasRemaining()) { // Output buffer is full. Suspend origin input until // the client handler frees up some space in the buffer conn.suspendInput(); } /* // If there is some content in the buffer make sure client output // is active if (dst.position() > 0) { proxyTask.getClientIOControl().requestOutput(); } */ if (decoder.isCompleted()) { System.out.println(conn + " [proxy<-origin] response body received"); proxyTask.setOriginState(ConnState.RESPONSE_BODY_DONE); if (!this.connStrategy.keepAlive(conn.getHttpResponse(), context)) { System.out.println(conn + " [proxy<-origin] close connection"); proxyTask.setOriginState(ConnState.CLOSING); conn.close(); } proxyTask.getCompletion().run(); } else { proxyTask.setOriginState(ConnState.RESPONSE_BODY_STREAM); } } catch (IOException ex) { shutdownConnection(conn); } } }
From source file:org.apache.axis2.transport.nhttp.ClientHandler.java
/** * Process ready input (i.e. response from remote server) * @param conn connection being processed * @param decoder the content decoder in use *///from www.j a v a 2 s. c o m public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) { HttpContext context = conn.getContext(); HttpResponse response = conn.getHttpResponse(); Pipe.SinkChannel sink = (Pipe.SinkChannel) context.getAttribute(RESPONSE_SINK_CHANNEL); ByteBuffer inbuf = (ByteBuffer) context.getAttribute(REQUEST_BUFFER); try { while (decoder.read(inbuf) > 0) { inbuf.flip(); sink.write(inbuf); inbuf.compact(); } if (decoder.isCompleted()) { sink.close(); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { ConnectionPool.release(conn); } } } catch (IOException e) { handleException("I/O Error : " + e.getMessage(), e, conn); } }
From source file:org.apache.axis2.transport.nhttp.ServerHandler.java
/** * Process ready input by writing it into the Pipe * @param conn the connection being processed * @param decoder the content decoder in use *///from w w w. j av a2 s .c om public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) { HttpContext context = conn.getContext(); Pipe.SinkChannel sink = (Pipe.SinkChannel) context.getAttribute(REQUEST_SINK_CHANNEL); ByteBuffer inbuf = (ByteBuffer) context.getAttribute(REQUEST_BUFFER); try { while (decoder.read(inbuf) > 0) { inbuf.flip(); sink.write(inbuf); inbuf.compact(); } if (decoder.isCompleted()) { sink.close(); } } catch (IOException e) { handleException("I/O Error : " + e.getMessage(), e, conn); } }