List of usage examples for org.apache.http.nio ContentEncoder write
int write(ByteBuffer src) throws IOException;
From source file:NGzipCompressingEntity.java
public void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException { if (baos == null) { baos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(baos); InputStream in = wrappedEntity.getContent(); byte[] tmp = new byte[2048]; int l;/*from www. j a v a 2 s .c o m*/ while ((l = in.read(tmp)) != -1) { gzip.write(tmp, 0, l); } gzip.close(); buffer = ByteBuffer.wrap(baos.toByteArray()); } encoder.write(buffer); if (!buffer.hasRemaining()) encoder.complete(); }
From source file:org.jenkinsci.plugins.relution_publisher.net.requests.ZeroCopyFileRequestProducer.java
private boolean writeHeader(final ContentEncoder encoder, final IOControl ioctrl) throws IOException { final byte[] array = this.getHeader(); if (this.mMultipartHeaderIndex >= array.length) { return true; }//from ww w . j ava 2s . c om final int length = array.length - this.mMultipartHeaderIndex; final ByteBuffer buffer = ByteBuffer.wrap(array, this.mMultipartHeaderIndex, length); this.mMultipartHeaderIndex += encoder.write(buffer); return false; }
From source file:org.jenkinsci.plugins.relution_publisher.net.requests.ZeroCopyFileRequestProducer.java
private boolean writeFooter(final ContentEncoder encoder, final IOControl ioctrl) throws IOException { final byte[] array = this.getFooter(); if (this.mMultipartFooterIndex >= array.length) { return true; }//from ww w . ja va 2 s .c om final int length = array.length - this.mMultipartFooterIndex; final ByteBuffer buffer = ByteBuffer.wrap(array, this.mMultipartFooterIndex, length); this.mMultipartFooterIndex += encoder.write(buffer); return false; }
From source file:org.apache.synapse.transport.nhttp.NhttpSharedOutputBuffer.java
public int produceContent(final ContentEncoder encoder, final IOControl ioctrl) throws IOException { if (this.shutdown) { return -1; }//from w ww .j a v a 2 s.c o m this.lock.lock(); try { if (ioctrl != null) { this.ioctrl = ioctrl; } setOutputMode(); int bytesWritten = 0; if (super.hasData()) { bytesWritten = encoder.write(this.buffer); if (encoder.isCompleted()) { this.endOfStream = true; } } if (!super.hasData()) { // No more buffered content // If at the end of the stream, terminate if (this.endOfStream && !encoder.isCompleted()) { encoder.complete(); } if (!this.endOfStream) { // suspend output events if (this.ioctrl != null) { this.ioctrl.suspendOutput(); } } } this.condition.signalAll(); return bytesWritten; } finally { this.lock.unlock(); } }
From source file:org.apache.cxf.transport.http.asyncclient.SharedOutputBuffer.java
public int produceContent(final ContentEncoder encoder, final IOControl ioc) throws IOException { if (this.shutdown) { return -1; }/*from www . j a v a 2 s. co m*/ this.lock.lock(); try { this.ioctrl = ioc; setOutputMode(); int bytesWritten = 0; if (largeWrapper != null || super.hasData()) { if (!this.buffer.hasRemaining() && largeWrapper != null) { bytesWritten = encoder.write(largeWrapper); } else { bytesWritten = encoder.write(this.buffer); } if (encoder.isCompleted()) { this.endOfStream = true; } } if ((largeWrapper == null || !largeWrapper.hasRemaining()) && !super.hasData()) { // No more buffered content // If at the end of the stream, terminate if (this.endOfStream && !encoder.isCompleted()) { encoder.complete(); } if (!this.endOfStream && this.ioctrl != null) { // suspend output events this.ioctrl.suspendOutput(); } } // no need to signal if the large wrapper is present and has data remaining if (largeWrapper == null || !largeWrapper.hasRemaining()) { this.condition.signalAll(); } return bytesWritten; } finally { this.lock.unlock(); } }
From source file:org.apache.cxf.transport.http.asyncclient.CXFHttpAsyncRequestProducer.java
public void produceContent(final ContentEncoder enc, final IOControl ioc) throws IOException { if (content != null) { if (buffer == null) { if (content.getTempFile() == null) { buffer = ByteBuffer.wrap(content.getBytes()); } else { fis = content.getInputStream(); chan = (fis instanceof FileInputStream) ? ((FileInputStream) fis).getChannel() : Channels.newChannel(fis); buffer = ByteBuffer.allocate(8 * 1024); }/*from w ww . ja v a 2s.c o m*/ } int i = -1; buffer.rewind(); if (buffer.hasRemaining() && chan != null) { i = chan.read(buffer); buffer.flip(); } enc.write(buffer); if (!buffer.hasRemaining() && i == -1) { enc.complete(); } } else { buf.produceContent(enc, ioc); } }
From source file:org.apache.synapse.transport.passthru.Pipe.java
/** * Consume the data from the buffer. Before calling this method attachConsumer * method must be called with a valid IOControl. * * @param encoder encoder used to write the data means there will not be any data * written in to this buffer/*from ww w . j av a2s. co m*/ * @return number of bytes written (consumed) * @throws IOException if an error occurred while consuming data */ public int consume(final ContentEncoder encoder) throws IOException { if (consumerIoControl == null) { throw new IllegalStateException("Consumer cannot be null when calling consume"); } if (hasHttpProducer && producerIoControl == null) { throw new IllegalStateException("Producer cannot be null when calling consume"); } lock.lock(); ControlledByteBuffer consumerBuffer; if (outputBuffer != null) { consumerBuffer = outputBuffer; } else { consumerBuffer = buffer; } try { // if producer at error we have to stop the encoding and return immediately if (producerError) { encoder.complete(); return -1; } setOutputMode(consumerBuffer); int bytesWritten = encoder.write(consumerBuffer.getByteBuffer()); setInputMode(consumerBuffer); if (consumerBuffer.position() == 0) { if (outputBuffer == null) { if (producerCompleted) { encoder.complete(); } else { // buffer is empty. Wait until the producer fills up // the buffer consumerIoControl.suspendOutput(); } } else if (serializationComplete || rawSerializationComplete) { encoder.complete(); } } if (bytesWritten > 0) { if (!encoder.isCompleted() && !producerCompleted && hasHttpProducer) { producerIoControl.requestInput(); } } writeCondition.signalAll(); return bytesWritten; } finally { lock.unlock(); } }
From source file:net.kungfoo.grizzly.proxy.impl.ConnectingHandler.java
public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) { System.out.println(conn + " [proxy->origin] output ready"); HttpContext context = conn.getContext(); ProxyProcessingInfo proxyTask = (ProxyProcessingInfo) context.getAttribute(ProxyProcessingInfo.ATTRIB); synchronized (proxyTask) { ConnState connState = proxyTask.getOriginState(); if (connState != ConnState.REQUEST_SENT && connState != ConnState.REQUEST_BODY_STREAM) { throw new IllegalStateException("Illegal target connection state: " + connState); }/*from www . j a v a2 s .com*/ try { // TODO: propper handling of POST ByteBuffer src = proxyTask.getInBuffer(); final int srcSize = src.limit(); if (src.position() != 0) { System.out.println(conn + " [proxy->origin] buff not consumed yet"); return; } ByteChunk chunk = new ByteChunk(srcSize); Request originalRequest = proxyTask.getOriginalRequest(); int read; int encRead = 0; long bytesWritten = 0; while ((read = originalRequest.doRead(chunk)) != -1) { System.out.println(conn + " [proxy->origin] " + read + " bytes read"); if (read > srcSize) { src = ByteBuffer.wrap(chunk.getBytes(), chunk.getOffset(), read); } else { src.put(chunk.getBytes(), chunk.getOffset(), read); } src.flip(); encRead = encoder.write(src); bytesWritten += encRead; src.compact(); chunk.reset(); if (encRead == 0) { System.out.println(conn + " [proxy->origin] encoder refused to consume more"); break; } else { System.out.println(conn + " [proxy->origin] " + encRead + " consumed by encoder"); } } System.out.println(conn + " [proxy->origin] " + bytesWritten + " bytes written"); System.out.println(conn + " [proxy->origin] " + encoder); src.compact(); if (src.position() == 0 && encRead != 0) { encoder.complete(); } // Update connection state if (encoder.isCompleted()) { System.out.println(conn + " [proxy->origin] request body sent"); proxyTask.setOriginState(ConnState.REQUEST_BODY_DONE); } else { proxyTask.setOriginState(ConnState.REQUEST_BODY_STREAM); } } catch (IOException ex) { shutdownConnection(conn); } } }