private int addComponents0(int cIndex, ByteBuf... buffers) {
    checkComponentIndex(cIndex);

    if (buffers == null) {
      throw new NullPointerException("buffers");
    }

    int readableBytes = 0;
    for (ByteBuf b : buffers) {
      if (b == null) {
        break;
      }
      readableBytes += b.readableBytes();
    }

    if (readableBytes == 0) {
      return cIndex;
    }

    // No need for consolidation
    for (ByteBuf b : buffers) {
      if (b == null) {
        break;
      }
      if (b.isReadable()) {
        cIndex = addComponent0(cIndex, b, false) + 1;
        int size = components.size();
        if (cIndex > size) {
          cIndex = size;
        }
      }
    }
    return cIndex;
  }
Beispiel #2
0
 /**
  * Creates a new buffer which wraps the specified buffer's readable bytes. A modification on the
  * specified buffer's content will be visible to the returned buffer.
  */
 public static ByteBuf wrappedBuffer(ByteBuf buffer) {
   if (buffer.isReadable()) {
     return buffer.slice();
   } else {
     return EMPTY_BUFFER;
   }
 }
Beispiel #3
0
 public OFMessage readFrom(ByteBuf bb) throws OFParseError {
     if(!bb.isReadable())
         return null;
     short wireVersion = U8.f(bb.getByte(bb.readerIndex()));
     OFFactory factory;
     switch (wireVersion) {
     //:: for v in versions:
     case ${v.int_version}:
  @Test
  public void testGetReadInt() {
    ByteBuf buf = buffer(((ByteBuffer) allocate(8).putInt(1).putInt(2).flip()).asReadOnlyBuffer());
    Assert.assertEquals(1, buf.getInt(0));
    Assert.assertEquals(2, buf.getInt(4));

    Assert.assertEquals(1, buf.readInt());
    Assert.assertEquals(2, buf.readInt());
    Assert.assertFalse(buf.isReadable());
  }
  @Test
  public void testGetReadLong() {
    ByteBuf buf =
        buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
    Assert.assertEquals(1, buf.getLong(0));
    Assert.assertEquals(2, buf.getLong(8));

    Assert.assertEquals(1, buf.readLong());
    Assert.assertEquals(2, buf.readLong());
    Assert.assertFalse(buf.isReadable());
  }
  @Test
  public void testGetReadByte() {
    ByteBuf buf =
        buffer(
            ((ByteBuffer) allocate(2).put(new byte[] {(byte) 1, (byte) 2}).flip())
                .asReadOnlyBuffer());
    Assert.assertEquals(1, buf.getByte(0));
    Assert.assertEquals(2, buf.getByte(1));

    Assert.assertEquals(1, buf.readByte());
    Assert.assertEquals(2, buf.readByte());
    Assert.assertFalse(buf.isReadable());
  }
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
    ByteBuf in = (ByteBuf) msg;
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      while (in.isReadable()) { // (1)
        byte readByte = in.readByte();
        baos.write(readByte);
        // System.out.flush();

        ctx.flush();
      }
      System.out.println("Msg Read: " + baos.toString());
    } finally {
      // System.out.println("finish!");
      ReferenceCountUtil.release(msg); // (2)
    }
  }
Beispiel #8
0
 /**
  * Creates a new big-endian composite buffer which wraps the readable bytes of the specified
  * buffers without copying them. A modification on the content of the specified buffers will be
  * visible to the returned buffer.
  */
 public static ByteBuf wrappedBuffer(int maxNumComponents, ByteBuf... buffers) {
   switch (buffers.length) {
     case 0:
       break;
     case 1:
       if (buffers[0].isReadable()) {
         return wrappedBuffer(buffers[0].order(BIG_ENDIAN));
       }
       break;
     default:
       for (ByteBuf b : buffers) {
         if (b.isReadable()) {
           return new CompositeByteBuf(ALLOC, false, maxNumComponents, buffers);
         }
       }
   }
   return EMPTY_BUFFER;
 }
Beispiel #9
0
 @Override
 public boolean isReadable(int size) {
   return buf.isReadable(size);
 }
Beispiel #10
0
 @Override
 public boolean isReadable() {
   return buf.isReadable();
 }