Exemplo n.º 1
0
 /**
  * Write bytes form the given {@link ByteBuf} to the underlying {@link java.nio.channels.Channel}.
  *
  * @param buf the {@link ByteBuf} from which the bytes should be written
  * @return amount the amount of written bytes
  */
 private int doWriteBytes(ByteBuf buf, int readable) throws Exception {
   int readerIndex = buf.readerIndex();
   int localFlushedAmount;
   if (buf.nioBufferCount() == 1) {
     if (buf.hasMemoryAddress()) {
       localFlushedAmount =
           Native.writeAddress(fd, buf.memoryAddress(), readerIndex, buf.writerIndex());
     } else {
       ByteBuffer nioBuf = buf.internalNioBuffer(readerIndex, readable);
       localFlushedAmount = Native.write(fd, nioBuf, nioBuf.position(), nioBuf.limit());
     }
   } else {
     // backed by more then one buffer, do a gathering write...
     ByteBuffer[] nioBufs = buf.nioBuffers();
     localFlushedAmount = (int) Native.writev(fd, nioBufs, 0, nioBufs.length);
   }
   if (localFlushedAmount > 0) {
     buf.readerIndex(readerIndex + localFlushedAmount);
   }
   return localFlushedAmount;
 }