Java 类org.apache.http.nio.ContentEncoder 实例源码

项目:WebQQCore    文件:ApacheHttpService.java   
@Override
public synchronized void produceContent(ContentEncoder encoder,
        IOControl ioctrl) throws IOException {
    checkCanceled(isCanceled);

    byte[] tmp = new byte[4096];
    int len = httpInStream.read(tmp);
    ByteBuffer buffer = ByteBuffer.wrap(tmp, 0, len);
    encoder.write(buffer);
    writeLength += len;

    if (httpListener != null) {
        httpListener.onHttpWrite(writeLength, contentLength);
    }
    checkCanceled(isCanceled);
}
项目:OpsDev    文件:NHttpReverseProxy.java   
public void produceContent(
        final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
    synchronized (this.httpExchange) {
        this.httpExchange.setOriginIOControl(ioctrl);
        // Send data to the origin server
        ByteBuffer buf = this.httpExchange.getInBuffer();
        buf.flip();
        int n = encoder.write(buf);
        buf.compact();
        System.out.println("[proxy->origin] " + this.httpExchange.getId() + " " + n + " bytes written");
        ConsoleFactory.printToConsole("[proxy->origin] " + this.httpExchange.getId() + " " + n + " bytes written",true);
        // If there is space in the buffer and the message has not been
        // transferred, make sure the client is sending more data
        if (buf.hasRemaining() && !this.httpExchange.isRequestReceived()) {
            if (this.httpExchange.getClientIOControl() != null) {
                this.httpExchange.getClientIOControl().requestInput();
                System.out.println("[proxy->origin] " + this.httpExchange.getId() + " request client input");
                ConsoleFactory.printToConsole("[proxy->origin] " + this.httpExchange.getId() + " request client input",true);
            }
        }
        if (buf.position() == 0) {
            if (this.httpExchange.isRequestReceived()) {
                encoder.complete();
                System.out.println("[proxy->origin] " + this.httpExchange.getId() + " content fully written");
                ConsoleFactory.printToConsole("[proxy->origin] " + this.httpExchange.getId() + " content fully written",true);
            } else {
                // Input buffer is empty. Wait until the client fills up
                // the buffer
                ioctrl.suspendOutput();
                System.out.println("[proxy->origin] " + this.httpExchange.getId() + " suspend origin output");
                ConsoleFactory.printToConsole("[proxy->origin] " + this.httpExchange.getId() + " suspend origin output",true);
            }
        }
    }
}
项目:OpsDev    文件:NHttpReverseProxy.java   
public void produceContent(
        final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
    synchronized (this.httpExchange) {
        this.httpExchange.setClientIOControl(ioctrl);
        // Send data to the client
        ByteBuffer buf = this.httpExchange.getOutBuffer();
        buf.flip();
        int n = encoder.write(buf);
        buf.compact();
        System.out.println("[client<-proxy] " + this.httpExchange.getId() + " " + n + " bytes written");
        ConsoleFactory.printToConsole("[client<-proxy] " + this.httpExchange.getId() + " " + n + " bytes written",true);
        // If there is space in the buffer and the message has not been
        // transferred, make sure the origin is sending more data
        if (buf.hasRemaining() && !this.httpExchange.isResponseReceived()) {
            if (this.httpExchange.getOriginIOControl() != null) {
                this.httpExchange.getOriginIOControl().requestInput();
                System.out.println("[client<-proxy] " + this.httpExchange.getId() + " request origin input");
                ConsoleFactory.printToConsole("[client<-proxy] " + this.httpExchange.getId() + " request origin input",true);
            }
        }
        if (buf.position() == 0) {
            if (this.httpExchange.isResponseReceived()) {
                encoder.complete();
                System.out.println("[client<-proxy] " + this.httpExchange.getId() + " content fully written");
                ConsoleFactory.printToConsole("[client<-proxy] " + this.httpExchange.getId() + " content fully written",true);
            } else {
                // Input buffer is empty. Wait until the origin fills up
                // the buffer
                ioctrl.suspendOutput();
                System.out.println("[client<-proxy] " + this.httpExchange.getId() + " suspend client output");
                ConsoleFactory.printToConsole("[client<-proxy] " + this.httpExchange.getId() + " suspend client output",true);
            }
        }
    }
}
项目:PhET    文件:NHttpReverseProxy.java   
public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {
    System.out.println(conn + " [client<-proxy] output ready");

    HttpContext context = conn.getContext();
    ProxyTask proxyTask = (ProxyTask) context.getAttribute(ProxyTask.ATTRIB);

    synchronized (proxyTask) {
        ConnState connState = proxyTask.getClientState();
        if (connState != ConnState.RESPONSE_SENT
                && connState != ConnState.RESPONSE_BODY_STREAM) {
            throw new IllegalStateException("Illegal client connection state: " + connState);
        }

        HttpResponse response = proxyTask.getResponse();
        if (response == null) {
            throw new IllegalStateException("HTTP request is null");
        }

        try {

            ByteBuffer src = proxyTask.getOutBuffer();
            src.flip();
            int bytesWritten = encoder.write(src);
            System.out.println(conn + " [client<-proxy] " + bytesWritten + " bytes written");
            System.out.println(conn + " [client<-proxy] " + encoder);
            src.compact();

            if (src.position() == 0) {

                if (proxyTask.getOriginState() == ConnState.RESPONSE_BODY_DONE) {
                    encoder.complete();
                } else {
                    // Input output is empty. Wait until the origin handler 
                    // fills up the buffer
                    conn.suspendOutput();
                }
            }

            // Update connection state
            if (encoder.isCompleted()) {
                System.out.println(conn + " [proxy] response body sent");
                proxyTask.setClientState(ConnState.RESPONSE_BODY_DONE);
                if (!this.connStrategy.keepAlive(response, context)) {
                    System.out.println(conn + " [client<-proxy] close connection");
                    proxyTask.setClientState(ConnState.CLOSING);
                    conn.close();
                } else {
                    // Reset connection state
                    proxyTask.reset();
                    conn.requestInput();
                    // Ready to deal with a new request
                }
            } else {
                proxyTask.setClientState(ConnState.RESPONSE_BODY_STREAM);
                // Make sure origin input is active
                proxyTask.getOriginIOControl().requestInput();
            }

        } catch (IOException ex) {
            shutdownConnection(conn);
        } 
    }
}
项目:PhET    文件:NHttpReverseProxy.java   
public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) {
    System.out.println(conn + " [proxy->origin] output ready");

    HttpContext context = conn.getContext();
    ProxyTask proxyTask = (ProxyTask) context.getAttribute(ProxyTask.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);
        }

        try {

            ByteBuffer src = proxyTask.getInBuffer();
            src.flip();
            int bytesWritten = encoder.write(src);
            System.out.println(conn + " [proxy->origin] " + bytesWritten + " bytes written");
            System.out.println(conn + " [proxy->origin] " + encoder);
            src.compact();

            if (src.position() == 0) {
                if (proxyTask.getClientState() == ConnState.REQUEST_BODY_DONE) {
                    encoder.complete();
                } else {
                    // Input buffer is empty. Wait until the client fills up 
                    // the buffer
                    conn.suspendOutput();
                }
            }
            // 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);
                // Make sure client input is active
                proxyTask.getClientIOControl().requestInput();
            }

        } catch (IOException ex) {
            shutdownConnection(conn);
        }
    }
}
项目:PhET    文件:NHttpClientConnManagement.java   
public void outputReady(NHttpClientConnection conn, ContentEncoder encoder) {
    this.handler.outputReady(conn, encoder);
}
项目:epigraph    文件:FormatBasedServerProtocol.java   
MyHttpAsyncContentProducer(final Function<ContentEncoder, ContentWriter> producerFunc) {
  this.producerFunc = producerFunc;
  writer = null;
}
项目:epigraph    文件:FormatBasedServerProtocol.java   
@Override
public void produceContent(final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
  writer = producerFunc.apply(encoder);
  writer.write();
}
项目:relution-jenkins-plugin    文件:ZeroCopyFileRequestProducer.java   
@Override
public synchronized void produceContent(final ContentEncoder encoder, final IOControl ioctrl)
        throws IOException {
    final boolean first;

    if (this.mItemIterator == null) {
        this.mItemIterator = this.mItems.iterator();
        first = true;
    } else {
        first = false;
    }

    if (this.mItem == null && this.mItemIterator.hasNext()) {
        this.mItem = this.mItemIterator.next();
        this.mMultipartHeaderIndex = 0;
    }

    if (this.mItem != null) {
        if (!this.writeHeader(encoder, ioctrl, this.mItem, first)) {
            return;
        }

        if (this.mFileChannel == null) {
            this.mFile = new RandomAccessFile(this.mItem.getFile(), "r");
            this.mFileChannel = this.mFile.getChannel();
            this.mFilePosition = 0;
        }

        final long transferred;

        if (encoder instanceof FileContentEncoder) {
            transferred = ((FileContentEncoder) encoder).transfer(this.mFileChannel, this.mFilePosition, Integer.MAX_VALUE);

        } else {
            transferred = this.mFileChannel.transferTo(this.mFilePosition, Integer.MAX_VALUE, new ContentEncoderChannel(encoder));

        }

        if (transferred > 0) {
            this.mFilePosition += transferred;
        }

        if (this.mFilePosition >= this.mFileChannel.size()) {
            IOUtils.closeQuietly(this.mFileChannel);
            IOUtils.closeQuietly(this.mFile);
            this.mFileChannel = null;
            this.mFile = null;
            this.mItem = null;
        }
    }

    if (this.mItem == null && !this.mItemIterator.hasNext() && this.writeFooter(encoder, ioctrl)) {
        encoder.complete();
    }
}
项目:epigraph    文件:ContentEncodingOutputStream.java   
public ContentEncodingOutputStream(final @NotNull ContentEncoder encoder) {this.encoder = encoder;}