/** * Returns the {@link ChannelBuffer} portion of this {@link CompositeChannelBuffer} that * contains the specified {@code index}. <strong>This is an expert method!</strong> * * <p> * Please note that since a {@link CompositeChannelBuffer} is made up of * multiple {@link ChannelBuffer}s, this does <em>not</em> return the full buffer. * Instead, it only returns a portion of the composite buffer where the * index is located * </p> * * * @param index The {@code index} to search for and include in the returned {@link ChannelBuffer} * @return The {@link ChannelBuffer} that contains the specified {@code index} * @throws IndexOutOfBoundsException when the specified {@code index} is * less than zero, or larger than {@code capacity()} */ public ChannelBuffer getBuffer(int index) { if (index < 0 || index >= capacity()) { throw new IndexOutOfBoundsException("Invalid index: " + index + " - Bytes needed: " + index + ", maximum is " + capacity()); } //Return the component byte buffer return components[componentId(index)]; }
public ChannelBuffer slice(int index, int length) { if (index == 0) { if (length == 0) { return ChannelBuffers.EMPTY_BUFFER; } } else if (index < 0 || index > capacity() - length) { throw new IndexOutOfBoundsException("Invalid index: " + index + " - Bytes needed: " + (index + length) + ", maximum is " + capacity()); } else if (length == 0) { return ChannelBuffers.EMPTY_BUFFER; } List<ChannelBuffer> components = decompose(index, length); switch (components.size()) { case 0: return ChannelBuffers.EMPTY_BUFFER; case 1: return components.get(0); default: return new CompositeChannelBuffer(order(), components, gathering); } }
public void setBytes(int index, byte[] src, int srcIndex, int length) { int componentId = componentId(index); if (index > capacity() - length || srcIndex > src.length - length) { throw new IndexOutOfBoundsException("Too many bytes to read - needs " + (index + length) + " or " + (srcIndex + length) + ", maximum is " + capacity() + " or " + src.length); } int i = componentId; while (length > 0) { ChannelBuffer s = components[i]; int adjustment = indices[i]; int localLength = Math.min(length, s.capacity() - (index - adjustment)); s.setBytes(index - adjustment, src, srcIndex, localLength); index += localLength; srcIndex += localLength; length -= localLength; i ++; } }
public void 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; } int i = componentId(index); while (length > 0) { ChannelBuffer s = components[i]; int adjustment = indices[i]; int localLength = Math.min(length, s.capacity() - (index - adjustment)); s.getBytes(index - adjustment, out, localLength); index += localLength; length -= localLength; i ++; } }
public void setBytes(int index, ByteBuffer src) { int componentId = componentId(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) { ChannelBuffer s = components[i]; int adjustment = indices[i]; 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); } }
@Override public ByteBuffer[] toByteBuffers(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 new ByteBuffer[0]; } List<ByteBuffer> buffers = new ArrayList<ByteBuffer>(components.length); int i = componentId(index); while (length > 0) { ChannelBuffer s = components[i]; int adjustment = indices[i]; int localLength = Math.min(length, s.capacity() - (index - adjustment)); buffers.add(s.toByteBuffer(index - adjustment, localLength)); index += localLength; length -= localLength; i ++; } return buffers.toArray(new ByteBuffer[buffers.size()]); }
public void getBytes(int index, ByteBuffer dst) { int componentId = componentId(index); 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"); } int i = componentId; try { while (length > 0) { ChannelBuffer s = components[i]; int adjustment = indices[i]; 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); } }
public void 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; } int componentId = componentId(index); int i = componentId; while (length > 0) { ChannelBuffer s = components[i]; int adjustment = indices[i]; int localLength = Math.min(length, s.capacity() - (index - adjustment)); s.getBytes(index - adjustment, dst, dstIndex, localLength); index += localLength; dstIndex += localLength; length -= localLength; i ++; } }
public ChannelBuffer copy(int index, int length) { int componentId = componentId(index); if (index > capacity() - length) { throw new IndexOutOfBoundsException("Too many bytes to copy - Needs " + (index + length) + ", maximum is " + capacity()); } ChannelBuffer dst = factory().getBuffer(order(), length); copyTo(index, length, componentId, dst); return dst; }
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) { int componentId = componentId(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) { ChannelBuffer s = components[i]; int adjustment = indices[i]; int localLength = Math.min(length, s.capacity() - (index - adjustment)); s.setBytes(index - adjustment, src, srcIndex, localLength); index += localLength; srcIndex += localLength; length -= localLength; i ++; } }
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) { 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()); } if (index < 0) { throw new IndexOutOfBoundsException("Index must be >= 0"); } if (length == 0) { return; } int i = componentId(index); while (length > 0) { ChannelBuffer s = components[i]; int adjustment = indices[i]; int localLength = Math.min(length, s.capacity() - (index - adjustment)); s.getBytes(index - adjustment, dst, dstIndex, localLength); index += localLength; dstIndex += localLength; length -= localLength; i ++; } }
/** * Setup this ChannelBuffer from the list */ private void setComponents(List<ChannelBuffer> newComponents) { assert !newComponents.isEmpty(); // Clear the cache. lastAccessedComponentId = 0; // Build the component array. components = new ChannelBuffer[newComponents.size()]; for (int i = 0; i < components.length; i ++) { ChannelBuffer c = newComponents.get(i); if (c.order() != order()) { throw new IllegalArgumentException( "All buffers must have the same endianness."); } assert c.readerIndex() == 0; assert c.writerIndex() == c.capacity(); components[i] = c; } // Build the component lookup table. indices = new int[components.length + 1]; indices[0] = 0; for (int i = 1; i <= components.length; i ++) { indices[i] = indices[i - 1] + components[i - 1].capacity(); } // Reset the indexes. setIndex(0, capacity()); }
final int bytesToMove = capacity() - localReaderIndex; List<ChannelBuffer> list = decompose(localReaderIndex, bytesToMove);