io.netty.channel.FileRegion#transfered ( )源码实例Demo

下面列出了io.netty.channel.FileRegion#transfered ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: DDMQ   文件: FileRegionEncoder.java
/**
 * 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);
    }
}
 
源代码2 项目: rocketmq-4.3.0   文件: FileRegionEncoder.java
/**
 * 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.将消息编码到ByteBuf中。对于可由此编码器处理的每个书面消息,将调用此方法。
 *
 * @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);
    }
}
 
源代码3 项目: rocketmq-read   文件: FileRegionEncoder.java
/**
 * 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 项目: DDMQ   文件: FileRegionEncoder.java
/**
 * 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);
    }
}
 
源代码5 项目: rocketmq   文件: FileRegionEncoder.java
/**
 * 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);
    }
}
 
源代码6 项目: netty4.0.27Learn   文件: AbstractOioByteChannel.java
@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
    for (;;) {
        Object msg = in.current();
        if (msg == null) {
            // nothing left to write
            break;
        }
        if (msg instanceof ByteBuf) {
            ByteBuf buf = (ByteBuf) msg;
            int readableBytes = buf.readableBytes();
            while (readableBytes > 0) {
                doWriteBytes(buf);
                int newReadableBytes = buf.readableBytes();
                in.progress(readableBytes - newReadableBytes);
                readableBytes = newReadableBytes;
            }
            in.remove();
        } else if (msg instanceof FileRegion) {
            FileRegion region = (FileRegion) msg;
            long transfered = region.transfered();
            doWriteFileRegion(region);
            in.progress(region.transfered() - transfered);
            in.remove();
        } else {
            in.remove(new UnsupportedOperationException(
                    "unsupported message type: " + StringUtil.simpleClassName(msg)));
        }
    }
}
 
源代码7 项目: netty4.0.27Learn   文件: NioSocketChannel.java
@Override
protected long doWriteFileRegion(FileRegion region) throws Exception {
    final long position = region.transfered();
    return region.transferTo(javaChannel(), position);
}
 
源代码8 项目: netty4.0.27Learn   文件: OioByteStreamChannel.java
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());
    }
}