下面列出了怎么用org.apache.http.nio.ContentEncoder的API类实例代码及写法,或者点击链接到github查看源代码。
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);
}
}
int i = -1;
((Buffer)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);
}
}
public int produceContent(final ContentEncoder encoder, final IOControl ioc) throws IOException {
if (this.shutdown) {
return -1;
}
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();
}
}
@Override
public void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException {
delegate.produceContent(encoder, ioctrl);
}
@Override public void produceContent(ContentEncoder encoder, IOControl io) throws IOException {
requestProducer.produceContent(encoder, io);
}