@Override protected void doWrite(ChannelOutboundBuffer in) throws Exception { ByteBuf chunk; while (!stream.isNotWritable() && (chunk = (ByteBuf) in.current()) != null) { bytesWritten += chunk.readableBytes(); stream.writeData(chunk.retain(), false); stream.handlerContext.flush(); in.remove(); } }
@Override protected void doWrite(ChannelOutboundBuffer in) throws Exception { for (;;) { Object msg = in.current(); if (msg == null) { break; } ReferenceCountUtil.retain(msg); handleOutboundMessage(msg); in.remove(); } }
ByteBuf buf = (ByteBuf) msg; if (!buf.isReadable()) { in.remove(); return 0; in.progress(localFlushedAmount); if (!buf.isReadable()) { in.remove(); FileRegion region = (FileRegion) msg; if (region.transferred() >= region.count()) { in.remove(); return 0; in.progress(localFlushedAmount); if (region.transferred() >= region.count()) { in.remove();
/** * Removes the fully written entries and update the reader index of the partially written entry. * This operation assumes all messages in this buffer is {@link ByteBuf}. */ public void removeBytes(long writtenBytes) { for (;;) { Object msg = current(); if (!(msg instanceof ByteBuf)) { assert writtenBytes == 0; break; } final ByteBuf buf = (ByteBuf) msg; final int readerIndex = buf.readerIndex(); final int readableBytes = buf.writerIndex() - readerIndex; if (readableBytes <= writtenBytes) { if (writtenBytes != 0) { progress(readableBytes); writtenBytes -= readableBytes; } remove(); } else { // readableBytes > writtenBytes if (writtenBytes != 0) { buf.readerIndex(readerIndex + (int) writtenBytes); progress(writtenBytes); } break; } } clearNioBuffers(); }
@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 transferred = region.transferred(); doWriteFileRegion(region); in.progress(region.transferred() - transferred); in.remove(); } else { in.remove(new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg))); } } }
@Override protected void doWrite(ChannelOutboundBuffer in) throws Exception { for (;;) { Object msg = in.current(); if (msg == null) { break; } ReferenceCountUtil.retain(msg); handleOutboundMessage(msg); in.remove(); } }
/** * Removes the fully written entries and update the reader index of the partially written entry. * This operation assumes all messages in this buffer is {@link ByteBuf}. */ public void removeBytes(long writtenBytes) { for (;;) { Object msg = current(); if (!(msg instanceof ByteBuf)) { assert writtenBytes == 0; break; } final ByteBuf buf = (ByteBuf) msg; final int readerIndex = buf.readerIndex(); final int readableBytes = buf.writerIndex() - readerIndex; if (readableBytes <= writtenBytes) { if (writtenBytes != 0) { progress(readableBytes); writtenBytes -= readableBytes; } remove(); } else { // readableBytes > writtenBytes if (writtenBytes != 0) { buf.readerIndex(readerIndex + (int) writtenBytes); progress(writtenBytes); } break; } } clearNioBuffers(); }
@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 transferred = region.transferred(); doWriteFileRegion(region); in.progress(region.transferred() - transferred); in.remove(); } else { in.remove(new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg))); } } }
@Override protected int doWriteSingle(ChannelOutboundBuffer in) throws Exception { Object msg = in.current(); if (msg instanceof FileDescriptor && socket.sendFd(((FileDescriptor) msg).intValue()) > 0) { // File descriptor was written, so remove it. in.remove(); return 1; } return super.doWriteSingle(in); }
@Override protected void doWrite(ChannelOutboundBuffer in) throws Exception { for (;;) { Object msg = in.current(); if (msg == null) { break; } ReferenceCountUtil.retain(msg); handleOutboundMessage(msg); in.remove(); } }
@Override protected int doWriteSingle(ChannelOutboundBuffer in) throws Exception { Object msg = in.current(); if (msg instanceof FileDescriptor && socket.sendFd(((FileDescriptor) msg).intValue()) > 0) { // File descriptor was written, so remove it. in.remove(); return 1; } return super.doWriteSingle(in); }
@Override protected int doWriteSingle(ChannelOutboundBuffer in) throws Exception { Object msg = in.current(); if (msg instanceof FileDescriptor && socket.sendFd(((FileDescriptor) msg).intValue()) > 0) { // File descriptor was written, so remove it. in.remove(); return 1; } return super.doWriteSingle(in); }
@Override protected int doWriteSingle(ChannelOutboundBuffer in) throws Exception { Object msg = in.current(); if (msg instanceof FileDescriptor && socket.sendFd(((FileDescriptor) msg).intValue()) > 0) { // File descriptor was written, so remove it. in.remove(); return 1; } return super.doWriteSingle(in); }
/** * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}. * @param in the collection which contains objects to write. * @param buf the {@link ByteBuf} from which the bytes should be written * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but no * data was accepted</li> * </ul> */ private int writeBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { int readableBytes = buf.readableBytes(); if (readableBytes == 0) { in.remove(); return 0; } if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) { return doWriteBytes(in, buf); } else { ByteBuffer[] nioBuffers = buf.nioBuffers(); return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes, config().getMaxBytesPerGatheringWrite()); } }
/** * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}. * @param in the collection which contains objects to write. * @param buf the {@link ByteBuf} from which the bytes should be written * @return The value that should be decremented from the write quantum which starts at * {@link ChannelConfig#getWriteSpinCount()}. The typical use cases are as follows: * <ul> * <li>0 - if no write was attempted. This is appropriate if an empty {@link ByteBuf} (or other empty content) * is encountered</li> * <li>1 - if a single call to write data was made to the OS</li> * <li>{@link ChannelUtils#WRITE_STATUS_SNDBUF_FULL} - if an attempt to write data was made to the OS, but * no data was accepted</li> * </ul> */ private int writeBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { int readableBytes = buf.readableBytes(); if (readableBytes == 0) { in.remove(); return 0; } if (buf.hasMemoryAddress() || buf.nioBufferCount() == 1) { return doWriteBytes(in, buf); } else { ByteBuffer[] nioBuffers = buf.nioBuffers(); return writeBytesMultiple(in, nioBuffers, nioBuffers.length, readableBytes, config().getMaxBytesPerGatheringWrite()); } }