public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { if (useGathering()) { return (int) out.write(toByteBuffers(index, length)); } // XXX Gathering write is not supported because of a known issue. // See http://bugs.sun.com/view_bug.do?bug_id=6210541 // This issue appeared in 2004 and is still unresolved!? return out.write(toByteBuffer(index, length)); }
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 ChannelBuffer duplicate() { ChannelBuffer duplicate = new CompositeChannelBuffer(this); duplicate.setIndex(readerIndex(), writerIndex()); return duplicate; }
public void setMedium(int index, int value) { int componentId = componentId(index); if (index + 3 <= indices[componentId + 1]) { components[componentId].setMedium(index - indices[componentId], value); } else if (order() == ByteOrder.BIG_ENDIAN) { setShort(index, (short) (value >> 8)); setByte(index + 2, (byte) value); } else { setShort(index , (short) value); setByte(index + 2, (byte) (value >>> 16)); } }
public void setLong(int index, long value) { int componentId = componentId(index); if (index + 8 <= indices[componentId + 1]) { components[componentId].setLong(index - indices[componentId], value); } else if (order() == ByteOrder.BIG_ENDIAN) { setInt(index, (int) (value >>> 32)); setInt(index + 4, (int) value); } else { setInt(index , (int) value); setInt(index + 4, (int) (value >>> 32)); } }
public void setShort(int index, int value) { int componentId = componentId(index); if (index + 2 <= indices[componentId + 1]) { components[componentId].setShort(index - indices[componentId], value); } else if (order() == ByteOrder.BIG_ENDIAN) { setByte(index, (byte) (value >>> 8)); setByte(index + 1, (byte) value); } else { setByte(index , (byte) value); setByte(index + 1, (byte) (value >>> 8)); } }
final int localReaderIndex = readerIndex(); if (localReaderIndex == 0) { return; int localWriterIndex = writerIndex(); final int bytesToMove = capacity() - localReaderIndex; List<ChannelBuffer> list = decompose(localReaderIndex, bytesToMove); final ChannelBuffer padding = ChannelBuffers.buffer(order(), localReaderIndex); padding.writerIndex(localReaderIndex); list.add(padding); resetReaderIndex(); localMarkedReaderIndex = readerIndex(); } catch (IndexOutOfBoundsException e) { resetWriterIndex(); localMarkedWriterIndex = writerIndex(); } catch (IndexOutOfBoundsException e) { setComponents(list); setIndex(localMarkedReaderIndex, localMarkedWriterIndex); markReaderIndex(); markWriterIndex(); setIndex(0, localWriterIndex);
@Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { Object retVal = super.encode(ctx, channel, msg); if (retVal instanceof CompositeChannelBuffer) { CompositeChannelBuffer ccb = (CompositeChannelBuffer) retVal; if (ccb.useGathering() != Netty3Utils.DEFAULT_GATHERING) { List<ChannelBuffer> decompose = ccb.decompose(ccb.readerIndex(), ccb.readableBytes()); return ChannelBuffers.wrappedBuffer(Netty3Utils.DEFAULT_GATHERING, decompose.toArray(new ChannelBuffer[decompose.size()])); } } return retVal; } }
protected void appendToCumulation(ChannelBuffer input) { ChannelBuffer cumulation = currentMessage.getContent(); if (cumulation instanceof CompositeChannelBuffer) { // Make sure the resulting cumulation buffer has no more than the configured components. CompositeChannelBuffer composite = (CompositeChannelBuffer) cumulation; if (composite.numComponents() >= maxCumulationBufferComponents) { currentMessage.setContent(ChannelBuffers.wrappedBuffer(composite.copy(), input)); } else { List<ChannelBuffer> decomposed = composite.decompose(0, composite.readableBytes()); ChannelBuffer[] buffers = decomposed.toArray(new ChannelBuffer[decomposed.size() + 1]); buffers[buffers.length - 1] = input; currentMessage.setContent(ChannelBuffers.wrappedBuffer(buffers)); } } else { currentMessage.setContent(ChannelBuffers.wrappedBuffer(cumulation, input)); } }
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; }
/** * 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 int getUnsignedMedium(int index) { int componentId = componentId(index); if (index + 3 <= indices[componentId + 1]) { return components[componentId].getUnsignedMedium(index - indices[componentId]); } else if (order() == ByteOrder.BIG_ENDIAN) { return (getShort(index) & 0xffff) << 8 | getByte(index + 2) & 0xff; } else { return getShort(index) & 0xFFFF | (getByte(index + 2) & 0xFF) << 16; } }
private CompositeChannelBuffer(CompositeChannelBuffer buffer) { order = buffer.order; gathering = buffer.gathering; components = buffer.components.clone(); indices = buffer.indices.clone(); setIndex(buffer.readerIndex(), buffer.writerIndex()); }
if (src instanceof CompositeChannelBuffer && ((CompositeChannelBuffer) src).useGathering()) { return new GatheringSendBuffer(src.toByteBuffers());
public long getLong(int index) { int componentId = componentId(index); if (index + 8 <= indices[componentId + 1]) { return components[componentId].getLong(index - indices[componentId]); } else if (order() == ByteOrder.BIG_ENDIAN) { return (getInt(index) & 0xffffffffL) << 32 | getInt(index + 4) & 0xffffffffL; } else { return getInt(index) & 0xFFFFFFFFL | (getInt(index + 4) & 0xFFFFFFFFL) << 32; } }
public void setInt(int index, int value) { int componentId = componentId(index); if (index + 4 <= indices[componentId + 1]) { components[componentId].setInt(index - indices[componentId], value); } else if (order() == ByteOrder.BIG_ENDIAN) { setShort(index, (short) (value >>> 16)); setShort(index + 2, (short) value); } else { setShort(index , (short) value); setShort(index + 2, (short) (value >>> 16)); } }
public short getShort(int index) { int componentId = componentId(index); if (index + 2 <= indices[componentId + 1]) { return components[componentId].getShort(index - indices[componentId]); } else if (order() == ByteOrder.BIG_ENDIAN) { return (short) ((getByte(index) & 0xff) << 8 | getByte(index + 1) & 0xff); } else { return (short) (getByte(index) & 0xff | (getByte(index + 1) & 0xff) << 8); } }
public int getInt(int index) { int componentId = componentId(index); if (index + 4 <= indices[componentId + 1]) { return components[componentId].getInt(index - indices[componentId]); } else if (order() == ByteOrder.BIG_ENDIAN) { return (getShort(index) & 0xffff) << 16 | getShort(index + 2) & 0xffff; } else { return getShort(index) & 0xFFFF | (getShort(index + 2) & 0xFFFF) << 16; } }
/** * 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()); }
private static ChannelBuffer compositeBuffer( ByteOrder endianness, List<ChannelBuffer> components, boolean gathering) { switch (components.size()) { case 0: return EMPTY_BUFFER; case 1: return components.get(0); default: return new CompositeChannelBuffer(endianness, components, gathering); } }