private int addComponent0(int cIndex, ByteBuf buffer, boolean addedBySelf) {
    checkComponentIndex(cIndex);

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

    if (buffer instanceof Iterable) {
      @SuppressWarnings("unchecked")
      Iterable<ByteBuf> composite = (Iterable<ByteBuf>) buffer;
      return addComponents0(cIndex, composite);
    }

    int readableBytes = buffer.readableBytes();
    if (readableBytes == 0) {
      return cIndex;
    }

    // No need to consolidate - just add a component to the list.
    Component c = new Component(buffer.order(ByteOrder.BIG_ENDIAN).slice(), addedBySelf);
    if (cIndex == components.size()) {
      components.add(c);
      if (cIndex == 0) {
        c.endOffset = readableBytes;
      } else {
        Component prev = components.get(cIndex - 1);
        c.offset = prev.endOffset;
        c.endOffset = c.offset + readableBytes;
      }
    } else {
      components.add(cIndex, c);
      updateComponentOffsets(cIndex);
    }
    return cIndex;
  }
Esempio n. 2
0
 @Override
 public ByteBuf copy(int index, int length) {
   checkIndex(index, length);
   ByteBuf copy = alloc().heapBuffer(length, maxCapacity());
   copy.writeBytes(memory, idx(index), length);
   return copy;
 }
  @Override
  public CompositeByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
    if (index > capacity() - length || dstIndex > dst.length - length) {
      throw new IndexOutOfBoundsException(
          "Too many bytes to read - Needs "
              + (index + length)
              + ", maximum is "
              + capacity()
              + " or "
              + dst.length);
    }
    if (index < 0) {
      throw new IndexOutOfBoundsException("index must be >= 0");
    }
    if (length == 0) {
      return this;
    }
    int i = toComponentIndex(index);

    while (length > 0) {
      Component c = components.get(i);
      ByteBuf s = c.buf;
      int adjustment = c.offset;
      int localLength = Math.min(length, s.capacity() - (index - adjustment));
      s.getBytes(index - adjustment, dst, dstIndex, localLength);
      index += localLength;
      dstIndex += localLength;
      length -= localLength;
      i++;
    }
    return this;
  }
  @Override
  public CompositeByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
    if (index > capacity() - length) {
      throw new IndexOutOfBoundsException(
          "Too many bytes to be read - needs " + (index + length) + ", maximum of " + capacity());
    }
    if (index < 0) {
      throw new IndexOutOfBoundsException("index must be >= 0");
    }
    if (length == 0) {
      return this;
    }

    int i = toComponentIndex(index);
    while (length > 0) {
      Component c = components.get(i);
      ByteBuf s = c.buf;
      int adjustment = c.offset;
      int localLength = Math.min(length, s.capacity() - (index - adjustment));
      s.getBytes(index - adjustment, out, localLength);
      index += localLength;
      length -= localLength;
      i++;
    }
    return this;
  }
  @Override
  public void getBytes(int index, ByteBuf dst, int dstIndex, int length) {
    int componentId = toComponentIndex(index);
    if (index > capacity() - length || dstIndex > dst.capacity() - length) {
      throw new IndexOutOfBoundsException(
          "Too many bytes to be read - Needs "
              + (index + length)
              + " or "
              + (dstIndex + length)
              + ", maximum is "
              + capacity()
              + " or "
              + dst.capacity());
    }

    int i = componentId;
    while (length > 0) {
      Component c = components.get(i);
      ByteBuf s = c.buf;
      int adjustment = c.offset;
      int localLength = Math.min(length, s.capacity() - (index - adjustment));
      s.getBytes(index - adjustment, dst, dstIndex, localLength);
      index += localLength;
      dstIndex += localLength;
      length -= localLength;
      i++;
    }
  }
  @Override
  public ByteBuffer[] nioBuffers(int index, int length) {
    if (index + length > capacity()) {
      throw new IndexOutOfBoundsException(
          "Too many bytes to convert - Needs" + (index + length) + ", maximum is " + capacity());
    }
    if (index < 0) {
      throw new IndexOutOfBoundsException("index must be >= 0");
    }
    if (length == 0) {
      return EMPTY_NIOBUFFERS;
    }
    int componentId = toComponentIndex(index);

    List<ByteBuffer> buffers = new ArrayList<ByteBuffer>(components.size());

    int i = componentId;
    while (length > 0) {
      Component c = components.get(i);
      ByteBuf s = c.buf;
      int adjustment = c.offset;
      int localLength = Math.min(length, s.capacity() - (index - adjustment));
      buffers.add(s.nioBuffer(index - adjustment, localLength));
      index += localLength;
      length -= localLength;
      i++;
    }

    return buffers.toArray(new ByteBuffer[buffers.size()]);
  }
 private static ByteBuffer toNioBuffer(ByteBuf buf, int index, int length) {
   if (buf.nioBufferCount() == 1) {
     return buf.nioBuffer(index, length);
   } else {
     return buf.copy(index, length).nioBuffer(0, length);
   }
 }
  @Override
  public CompositeByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
    int componentId = toComponentIndex(index);
    if (index > capacity() - length || srcIndex > src.capacity() - length) {
      throw new IndexOutOfBoundsException(
          "Too many bytes to be written - Needs "
              + (index + length)
              + " or "
              + (srcIndex + length)
              + ", maximum is "
              + capacity()
              + " or "
              + src.capacity());
    }

    int i = componentId;
    while (length > 0) {
      Component c = components.get(i);
      ByteBuf s = c.buf;
      int adjustment = c.offset;
      int localLength = Math.min(length, s.capacity() - (index - adjustment));
      s.setBytes(index - adjustment, src, srcIndex, localLength);
      index += localLength;
      srcIndex += localLength;
      length -= localLength;
      i++;
    }
    return this;
  }
  @Override
  public CompositeByteBuf setBytes(int index, ByteBuffer src) {
    int componentId = toComponentIndex(index);
    int limit = src.limit();
    int length = src.remaining();
    if (index > capacity() - length) {
      throw new IndexOutOfBoundsException(
          "Too many bytes to be written - Needs "
              + (index + length)
              + ", maximum is "
              + capacity());
    }

    int i = componentId;
    try {
      while (length > 0) {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.capacity() - (index - adjustment));
        src.limit(src.position() + localLength);
        s.setBytes(index - adjustment, src);
        index += localLength;
        length -= localLength;
        i++;
      }
    } finally {
      src.limit(limit);
    }
    return this;
  }
Esempio n. 10
0
 @Override
 public ByteBuf copy(int index, int length) {
   checkIndex(index, length);
   ByteBuf copy = alloc().directBuffer(length, maxCapacity());
   copy.writeBytes(this, index, length);
   return copy;
 }
 @Override
 protected ByteBuf newBuffer(int length) {
   buffer = PooledByteBufAllocator.DEFAULT.directBuffer(length).order(ByteOrder.LITTLE_ENDIAN);
   assertSame(ByteOrder.LITTLE_ENDIAN, buffer.order());
   assertEquals(0, buffer.writerIndex());
   return buffer;
 }
Esempio n. 12
0
 @Test
 public void shouldHaveSameByteOrder() {
   ByteBuf buf = Unpooled.buffer(1);
   assertSame(Unpooled.BIG_ENDIAN, Unpooled.unmodifiableBuffer(buf).order());
   buf = buf.order(LITTLE_ENDIAN);
   assertSame(Unpooled.LITTLE_ENDIAN, Unpooled.unmodifiableBuffer(buf).order());
 }
Esempio n. 13
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;
   }
 }
  @Override
  public CompositeByteBuf getBytes(int index, ByteBuffer dst) {
    int limit = dst.limit();
    int length = dst.remaining();

    if (index > capacity() - length) {
      throw new IndexOutOfBoundsException(
          "Too many bytes to be read - Needs " + (index + length) + ", maximum is " + capacity());
    }
    if (index < 0) {
      throw new IndexOutOfBoundsException("index must be >= 0");
    }
    if (length == 0) {
      return this;
    }
    int i = toComponentIndex(index);
    try {
      while (length > 0) {
        Component c = components.get(i);
        ByteBuf s = c.buf;
        int adjustment = c.offset;
        int localLength = Math.min(length, s.capacity() - (index - adjustment));
        dst.limit(dst.position() + localLength);
        s.getBytes(index - adjustment, dst);
        index += localLength;
        length -= localLength;
        i++;
      }
    } finally {
      dst.limit(limit);
    }
    return this;
  }
  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;
  }
Esempio n. 16
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}:
Esempio n. 17
0
  /**
   * Creates a read-only buffer which disallows any modification operations on the specified {@code
   * buffer}. The new buffer has the same {@code readerIndex} and {@code writerIndex} with the
   * specified {@code buffer}.
   */
  public static ByteBuf unmodifiableBuffer(ByteBuf buffer) {
    ByteOrder endianness = buffer.order();
    if (endianness == BIG_ENDIAN) {
      return new ReadOnlyByteBuf(buffer);
    }

    return new ReadOnlyByteBuf(buffer.order(BIG_ENDIAN)).order(LITTLE_ENDIAN);
  }
Esempio n. 18
0
 @Test
 public void shouldReturnReadOnlyDerivedBuffer() {
   ByteBuf buf = Unpooled.unmodifiableBuffer(Unpooled.buffer(1));
   assertTrue(buf.duplicate() instanceof ReadOnlyByteBuf);
   assertTrue(buf.slice() instanceof ReadOnlyByteBuf);
   assertTrue(buf.slice(0, 1) instanceof ReadOnlyByteBuf);
   assertTrue(buf.duplicate() instanceof ReadOnlyByteBuf);
 }
 // Test for https://github.com/netty/netty/issues/1060
 @Test
 public void testReadWithEmptyCompositeBuffer() {
   ByteBuf buf = freeLater(compositeBuffer());
   int n = 65;
   for (int i = 0; i < n; i++) {
     buf.writeByte(1);
     assertEquals(1, buf.readByte());
   }
 }
Esempio n. 20
0
 /**
  * Creates a new buffer whose content is a copy of the specified {@code buffer}'s readable bytes.
  * The new buffer's {@code readerIndex} and {@code writerIndex} are {@code 0} and {@code
  * buffer.readableBytes} respectively.
  */
 public static ByteBuf copiedBuffer(ByteBuf buffer) {
   int readable = buffer.readableBytes();
   if (readable > 0) {
     ByteBuf copy = buffer(readable);
     copy.writeBytes(buffer, buffer.readerIndex(), readable);
     return copy;
   } else {
     return EMPTY_BUFFER;
   }
 }
Esempio n. 21
0
  @Test
  public void testWriteUtf8() {
    String usAscii = "Some UTF-8 like äÄ∏ŒŒ";
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    buf.writeBytes(usAscii.getBytes(CharsetUtil.UTF_8));
    ByteBuf buf2 = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    ByteBufUtil.writeUtf8(buf2, usAscii);

    Assert.assertEquals(buf, buf2);
  }
  @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());
  }
Esempio n. 23
0
 /** Create a new big-endian buffer that holds a sequence of the specified 24-bit integers. */
 public static ByteBuf copyMedium(int... values) {
   if (values == null || values.length == 0) {
     return EMPTY_BUFFER;
   }
   ByteBuf buffer = buffer(values.length * 3);
   for (int v : values) {
     buffer.writeMedium(v);
   }
   return buffer;
 }
Esempio n. 24
0
 /** Create a new big-endian buffer that holds a sequence of the specified 16-bit integers. */
 public static ByteBuf copyShort(short... values) {
   if (values == null || values.length == 0) {
     return EMPTY_BUFFER;
   }
   ByteBuf buffer = buffer(values.length * 2);
   for (int v : values) {
     buffer.writeShort(v);
   }
   return buffer;
 }
Esempio n. 25
0
 /** Create a new big-endian buffer that holds a sequence of the specified 64-bit integers. */
 public static ByteBuf copyLong(long... values) {
   if (values == null || values.length == 0) {
     return EMPTY_BUFFER;
   }
   ByteBuf buffer = buffer(values.length * 8);
   for (long v : values) {
     buffer.writeLong(v);
   }
   return buffer;
 }
Esempio n. 26
0
 /** Create a new big-endian buffer that holds a sequence of the specified boolean values. */
 public static ByteBuf copyBoolean(boolean... values) {
   if (values == null || values.length == 0) {
     return EMPTY_BUFFER;
   }
   ByteBuf buffer = buffer(values.length);
   for (boolean v : values) {
     buffer.writeBoolean(v);
   }
   return buffer;
 }
Esempio n. 27
0
 /**
  * Create a new big-endian buffer that holds a sequence of the specified 32-bit floating point
  * numbers.
  */
 public static ByteBuf copyFloat(float... values) {
   if (values == null || values.length == 0) {
     return EMPTY_BUFFER;
   }
   ByteBuf buffer = buffer(values.length * 4);
   for (float v : values) {
     buffer.writeFloat(v);
   }
   return buffer;
 }
Esempio n. 28
0
 /**
  * Create a new big-endian buffer that holds a sequence of the specified 64-bit floating point
  * numbers.
  */
 public static ByteBuf copyDouble(double... values) {
   if (values == null || values.length == 0) {
     return EMPTY_BUFFER;
   }
   ByteBuf buffer = buffer(values.length * 8);
   for (double v : values) {
     buffer.writeDouble(v);
   }
   return buffer;
 }
Esempio n. 29
0
  @Test
  public void testWriteUsAsciiString() {
    AsciiString usAscii = new AsciiString("NettyRocks");
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    buf.writeBytes(usAscii.toString().getBytes(CharsetUtil.US_ASCII));
    ByteBuf buf2 = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    ByteBufUtil.writeAscii(buf2, usAscii);

    Assert.assertEquals(buf, buf2);
  }
  @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());
  }