当前位置: 首页>>代码示例>>Java>>正文


Java FileRegion.transferTo方法代码示例

本文整理汇总了Java中io.netty.channel.FileRegion.transferTo方法的典型用法代码示例。如果您正苦于以下问题:Java FileRegion.transferTo方法的具体用法?Java FileRegion.transferTo怎么用?Java FileRegion.transferTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.netty.channel.FileRegion的用法示例。


在下文中一共展示了FileRegion.transferTo方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
 
开发者ID:nickman,项目名称:HeliosStreams,代码行数:30,代码来源:InvocationChannel.java

示例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;
        }
    }
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:25,代码来源:OioByteStreamChannel.java

示例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);
    }
}
 
开发者ID:apache,项目名称:rocketmq,代码行数:40,代码来源:FileRegionEncoder.java

示例4: doWriteFileRegion

import io.netty.channel.FileRegion; //导入方法依赖的package包/类
@Override
protected long doWriteFileRegion(FileRegion region) throws Exception {
    final long position = region.transfered();
    return region.transferTo(javaChannel(), position);
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:6,代码来源:NioSocketChannel.java

示例5: doWriteFileRegion

import io.netty.channel.FileRegion; //导入方法依赖的package包/类
@Override
protected long doWriteFileRegion(FileRegion region) throws Exception {
    final long position = region.transfered();
    final long writtenBytes = region.transferTo(javaChannel(), position);
    return writtenBytes;
}
 
开发者ID:kyle-liu,项目名称:netty4study,代码行数:7,代码来源:NioSocketChannel.java


注:本文中的io.netty.channel.FileRegion.transferTo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。