Java Code Examples for io.netty.channel.FileRegion#transfered()

The following examples show how to use io.netty.channel.FileRegion#transfered() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileRegionEncoder.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 2
Source File: FileRegionEncoder.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: FileRegionEncoder.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 4
Source File: FileRegionEncoder.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 5
Source File: FileRegionEncoder.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 6
Source File: AbstractOioByteChannel.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@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)));
        }
    }
}
 
Example 7
Source File: NioSocketChannel.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
protected long doWriteFileRegion(FileRegion region) throws Exception {
    final long position = region.transfered();
    return region.transferTo(javaChannel(), position);
}
 
Example 8
Source File: OioByteStreamChannel.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
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());
    }
}