@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;
  }
Ejemplo n.º 3
0
  public ByteBuf setBytes(int index, ByteBuffer src, int srcIndex, int length) {
    if (src.isDirect()) {
      checkIndex(index, length);
      PlatformDependent.copyMemory(
          PlatformDependent.directBufferAddress(src) + srcIndex,
          this.memoryAddress() + index,
          length);
    } else {
      if (srcIndex == 0 && src.capacity() == length) {
        b.setBytes(index + offset, src);
      } else {
        ByteBuffer newBuf = src.duplicate();
        newBuf.position(srcIndex);
        newBuf.limit(srcIndex + length);
        b.setBytes(index + offset, src);
      }
    }

    return this;
  }
Ejemplo n.º 4
0
 @Override
 public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
   checkDstIndex(index, length, dstIndex, dst.capacity());
   if (dst.hasMemoryAddress()) {
     PlatformDependent.copyMemory(memory, idx(index), dst.memoryAddress() + dstIndex, length);
   } else if (dst.hasArray()) {
     getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
   } else {
     dst.setBytes(dstIndex, memory, idx(index), length);
   }
   return this;
 }
Ejemplo n.º 5
0
 @Override
 public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
   checkDstIndex(index, length, dstIndex, dst.capacity());
   if (dst.hasArray()) {
     getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
   } else if (dst.nioBufferCount() > 0) {
     for (ByteBuffer bb : dst.nioBuffers(dstIndex, length)) {
       int bbLen = bb.remaining();
       getBytes(index, bb);
       index += bbLen;
     }
   } else {
     dst.setBytes(dstIndex, this, index, length);
   }
   return this;
 }
  @Override
  public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    int componentId = toComponentIndex(index);
    if (index > capacity() - length) {
      throw new IndexOutOfBoundsException(
          "Too many bytes to write - Needs " + (index + length) + ", maximum is " + capacity());
    }

    int i = componentId;
    int readBytes = 0;
    do {
      Component c = components.get(i);
      ByteBuf s = c.buf;
      int adjustment = c.offset;
      int localLength = Math.min(length, s.capacity() - (index - adjustment));
      int localReadBytes = s.setBytes(index - adjustment, in, localLength);

      if (localReadBytes == 0) {
        break;
      }

      if (localReadBytes < 0) {
        if (readBytes == 0) {
          return -1;
        } else {
          break;
        }
      }

      if (localReadBytes == localLength) {
        index += localLength;
        length -= localLength;
        readBytes += localLength;
        i++;
      } else {
        index += localReadBytes;
        length -= localReadBytes;
        readBytes += localReadBytes;
      }
    } while (length > 0);

    return readBytes;
  }
  @Override
  public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
    checkIndex(index, length);
    if (dst == null) {
      throw new NullPointerException("dst");
    }
    if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
      throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
    }

    if (length != 0) {
      if (dst.hasMemoryAddress()) {
        PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length);
      } else if (dst.hasArray()) {
        PlatformDependent.copyMemory(
            addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
      } else {
        dst.setBytes(dstIndex, this, index, length);
      }
    }
    return this;
  }
Ejemplo n.º 8
0
 @Override
 public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
   return buf.setBytes(index, in, length);
 }
Ejemplo n.º 9
0
 @Override
 public int setBytes(int index, InputStream in, int length) throws IOException {
   return buf.setBytes(index, in, length);
 }
Ejemplo n.º 10
0
 @Override
 public ByteBuf setBytes(int index, ByteBuffer src) {
   buf.setBytes(index, src);
   return this;
 }
Ejemplo n.º 11
0
 @Override
 public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
   buf.setBytes(index, src, srcIndex, length);
   return this;
 }
Ejemplo n.º 12
0
 @Override
 public ByteBuf setBytes(int index, ByteBuf src, int length) {
   buf.setBytes(index, src, length);
   return this;
 }
 @Test(expected = ReadOnlyBufferException.class)
 public void testSetBytesViaStream() throws IOException {
   ByteBuf buf = buffer(ByteBuffer.allocateDirect(8).asReadOnlyBuffer());
   buf.setBytes(0, new ByteArrayInputStream("test".getBytes()), 2);
 }
 @Test(expected = ReadOnlyBufferException.class)
 public void testSetBytesViaBuffer() {
   ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
   buf.setBytes(0, Unpooled.copyInt(1));
 }
 @Test(expected = ReadOnlyBufferException.class)
 public void testSetBytesViaArray() {
   ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
   buf.setBytes(0, "test".getBytes());
 }
Ejemplo n.º 16
0
 @Override
 public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
   b.setBytes(index + offset, src, srcIndex, length);
   return this;
 }