本文整理汇总了Java中org.apache.http.nio.ContentEncoder.write方法的典型用法代码示例。如果您正苦于以下问题:Java ContentEncoder.write方法的具体用法?Java ContentEncoder.write怎么用?Java ContentEncoder.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.nio.ContentEncoder
的用法示例。
在下文中一共展示了ContentEncoder.write方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: produceContent
import org.apache.http.nio.ContentEncoder; //导入方法依赖的package包/类
@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);
}
示例2: produceContent
import org.apache.http.nio.ContentEncoder; //导入方法依赖的package包/类
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);
}
}
}
}
示例3: outputReady
import org.apache.http.nio.ContentEncoder; //导入方法依赖的package包/类
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);
}
}
}