本文整理汇总了Java中io.netty.channel.FileRegion.count方法的典型用法代码示例。如果您正苦于以下问题:Java FileRegion.count方法的具体用法?Java FileRegion.count怎么用?Java FileRegion.count使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.channel.FileRegion
的用法示例。
在下文中一共展示了FileRegion.count方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import io.netty.channel.FileRegion; //导入方法依赖的package包/类
/**
* {@inheritDoc}
* @see io.netty.channel.ChannelOutboundInvoker#write(java.lang.Object)
*/
@Override
public ChannelFuture write(Object message) {
if(message!=null) {
if(message instanceof FileRegion) {
try {
Pipe pipe = Pipe.open();
FileRegion fr = (FileRegion)message;
long bytesToRead = fr.count();
fr.transferTo(pipe.sink(), 0L);
byte[] content = new byte[(int)bytesToRead];
pipe.source().read(ByteBuffer.wrap(content));
channelWrites.add(content);
} catch (Exception ex) {
log.error("Failed to read content from pipe", ex);
channelWrites.add(ex);
}
} else {
channelWrites.add(message);
}
log.info("Received Channel Write [{}] type:[{}]", message, message.getClass().getName());
}
return null;
}
示例2: doWriteFileRegion
import io.netty.channel.FileRegion; //导入方法依赖的package包/类
@Override
protected void doWriteFileRegion(FileRegion region) throws Exception {
OutputStream os = this.os;
if (os == null) {
throw new NotYetConnectedException();
}
if (outChannel == null) {
outChannel = Channels.newChannel(os);
}
long written = 0;
for (;;) {
long localWritten = region.transferTo(outChannel, written);
if (localWritten == -1) {
checkEOF(region);
return;
}
written += localWritten;
if (written >= region.count()) {
return;
}
}
}
示例3: encode
import io.netty.channel.FileRegion; //导入方法依赖的package包/类
/**
* Encode a message into a {@link io.netty.buffer.ByteBuf}. This method will be called for each written message that
* can be handled by this encoder.
*
* @param ctx the {@link io.netty.channel.ChannelHandlerContext} which this {@link
* io.netty.handler.codec.MessageToByteEncoder} belongs to
* @param msg the message to encode
* @param out the {@link io.netty.buffer.ByteBuf} into which the encoded message will be written
* @throws Exception is thrown if an error occurs
*/
@Override
protected void encode(ChannelHandlerContext ctx, FileRegion msg, final ByteBuf out) throws Exception {
WritableByteChannel writableByteChannel = new WritableByteChannel() {
@Override
public int write(ByteBuffer src) throws IOException {
out.writeBytes(src);
return out.capacity();
}
@Override
public boolean isOpen() {
return true;
}
@Override
public void close() throws IOException {
}
};
long toTransfer = msg.count();
while (true) {
long transferred = msg.transfered();
if (toTransfer - transferred <= 0) {
break;
}
msg.transferTo(writableByteChannel, transferred);
}
}
示例4: checkEOF
import io.netty.channel.FileRegion; //导入方法依赖的package包/类
private static void checkEOF(FileRegion region) throws IOException {
if (region.transfered() < region.count()) {
throw new EOFException("Expected to be able to write " + region.count() + " bytes, " +
"but only wrote " + region.transfered());
}
}