Package org.jboss.netty.buffer
Class CompositeChannelBuffer
- java.lang.Object
-
- org.jboss.netty.buffer.AbstractChannelBuffer
-
- org.jboss.netty.buffer.CompositeChannelBuffer
-
- All Implemented Interfaces:
java.lang.Comparable<ChannelBuffer>,ChannelBuffer
public class CompositeChannelBuffer extends AbstractChannelBuffer
A virtual buffer which shows multiple buffers as a single merged buffer. It is recommended to useChannelBuffers.wrappedBuffer(ChannelBuffer...)instead of calling the constructor explicitly.
-
-
Field Summary
Fields Modifier and Type Field Description private ChannelBuffer[]componentsprivate booleangatheringprivate int[]indicesprivate intlastAccessedComponentIdprivate java.nio.ByteOrderorder
-
Constructor Summary
Constructors Modifier Constructor Description CompositeChannelBuffer(java.nio.ByteOrder endianness, java.util.List<ChannelBuffer> buffers, boolean gathering)privateCompositeChannelBuffer(CompositeChannelBuffer buffer)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]array()Returns the backing byte array of this buffer.intarrayOffset()Returns the offset of the first byte within the backing byte array of this buffer.intcapacity()Returns the number of bytes (octets) this buffer can contain.private intcomponentId(int index)ChannelBuffercopy(int index, int length)Returns a copy of this buffer's sub-region.private voidcopyTo(int index, int length, int componentId, ChannelBuffer dst)java.util.List<ChannelBuffer>decompose(int index, int length)Same withslice(int, int)except that this method returns a list.voiddiscardReadBytes()Discards the bytes between the 0th index andreaderIndex.ChannelBufferduplicate()Returns a buffer which shares the whole region of this buffer.ChannelBufferFactoryfactory()Returns the factory which creates aChannelBufferwhose type and defaultByteOrderare same with this buffer.ChannelBuffergetBuffer(int index)bytegetByte(int index)Gets a byte at the specified absoluteindexin this buffer.voidgetBytes(int index, byte[] dst, int dstIndex, int length)Transfers this buffer's data to the specified destination starting at the specified absoluteindex.voidgetBytes(int index, java.io.OutputStream out, int length)Transfers this buffer's data to the specified stream starting at the specified absoluteindex.voidgetBytes(int index, java.nio.ByteBuffer dst)Transfers this buffer's data to the specified destination starting at the specified absoluteindexuntil the destination's position reaches its limit.intgetBytes(int index, java.nio.channels.GatheringByteChannel out, int length)Transfers this buffer's data to the specified channel starting at the specified absoluteindex.voidgetBytes(int index, ChannelBuffer dst, int dstIndex, int length)Transfers this buffer's data to the specified destination starting at the specified absoluteindex.intgetInt(int index)Gets a 32-bit integer at the specified absoluteindexin this buffer.longgetLong(int index)Gets a 64-bit long integer at the specified absoluteindexin this buffer.shortgetShort(int index)Gets a 16-bit short integer at the specified absoluteindexin this buffer.intgetUnsignedMedium(int index)Gets an unsigned 24-bit medium integer at the specified absoluteindexin this buffer.booleanhasArray()Returnstrueif and only if this buffer has a backing byte array.booleanisDirect()Returnstrueif and only if this buffer is backed by an NIO direct buffer.intnumComponents()java.nio.ByteOrderorder()Returns the endianness of this buffer.voidsetByte(int index, int value)Sets the specified byte at the specified absoluteindexin this buffer.voidsetBytes(int index, byte[] src, int srcIndex, int length)Transfers the specified source array's data to this buffer starting at the specified absoluteindex.intsetBytes(int index, java.io.InputStream in, int length)Transfers the content of the specified source stream to this buffer starting at the specified absoluteindex.voidsetBytes(int index, java.nio.ByteBuffer src)Transfers the specified source buffer's data to this buffer starting at the specified absoluteindexuntil the source buffer's position reaches its limit.intsetBytes(int index, java.nio.channels.ScatteringByteChannel in, int length)Transfers the content of the specified source channel to this buffer starting at the specified absoluteindex.voidsetBytes(int index, ChannelBuffer src, int srcIndex, int length)Transfers the specified source buffer's data to this buffer starting at the specified absoluteindex.private voidsetComponents(java.util.List<ChannelBuffer> newComponents)Setup this ChannelBuffer from the listvoidsetInt(int index, int value)Sets the specified 32-bit integer at the specified absoluteindexin this buffer.voidsetLong(int index, long value)Sets the specified 64-bit long integer at the specified absoluteindexin this buffer.voidsetMedium(int index, int value)Sets the specified 24-bit medium integer at the specified absoluteindexin this buffer.voidsetShort(int index, int value)Sets the specified 16-bit short integer at the specified absoluteindexin this buffer.ChannelBufferslice(int index, int length)Returns a slice of this buffer's sub-region.java.nio.ByteBuffertoByteBuffer(int index, int length)Converts this buffer's sub-region into a NIO buffer.java.nio.ByteBuffer[]toByteBuffers(int index, int length)Converts this buffer's sub-region into an array of NIO buffers.java.lang.StringtoString()Returns the string representation of this buffer.booleanuseGathering()Returntrueif gathering writes / reads should be used for thisCompositeChannelBuffer-
Methods inherited from class org.jboss.netty.buffer.AbstractChannelBuffer
bytesBefore, bytesBefore, bytesBefore, bytesBefore, bytesBefore, bytesBefore, checkReadableBytes, clear, compareTo, copy, ensureWritableBytes, equals, getBytes, getBytes, getBytes, getChar, getDouble, getFloat, getMedium, getUnsignedByte, getUnsignedInt, getUnsignedShort, hashCode, indexOf, indexOf, markReaderIndex, markWriterIndex, readable, readableBytes, readByte, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readBytes, readChar, readDouble, readerIndex, readerIndex, readFloat, readInt, readLong, readMedium, readShort, readSlice, readUnsignedByte, readUnsignedInt, readUnsignedMedium, readUnsignedShort, resetReaderIndex, resetWriterIndex, setBytes, setBytes, setBytes, setChar, setDouble, setFloat, setIndex, setZero, skipBytes, slice, toByteBuffer, toByteBuffers, toString, toString, writable, writableBytes, writeByte, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeBytes, writeChar, writeDouble, writeFloat, writeInt, writeLong, writeMedium, writerIndex, writerIndex, writeShort, writeZero
-
-
-
-
Field Detail
-
order
private final java.nio.ByteOrder order
-
components
private ChannelBuffer[] components
-
indices
private int[] indices
-
lastAccessedComponentId
private int lastAccessedComponentId
-
gathering
private final boolean gathering
-
-
Constructor Detail
-
CompositeChannelBuffer
public CompositeChannelBuffer(java.nio.ByteOrder endianness, java.util.List<ChannelBuffer> buffers, boolean gathering)
-
CompositeChannelBuffer
private CompositeChannelBuffer(CompositeChannelBuffer buffer)
-
-
Method Detail
-
useGathering
public boolean useGathering()
Returntrueif gathering writes / reads should be used for thisCompositeChannelBuffer
-
decompose
public java.util.List<ChannelBuffer> decompose(int index, int length)
Same withslice(int, int)except that this method returns a list.
-
setComponents
private void setComponents(java.util.List<ChannelBuffer> newComponents)
Setup this ChannelBuffer from the list
-
factory
public ChannelBufferFactory factory()
Description copied from interface:ChannelBufferReturns the factory which creates aChannelBufferwhose type and defaultByteOrderare same with this buffer.
-
order
public java.nio.ByteOrder order()
Description copied from interface:ChannelBufferReturns the endianness of this buffer.
-
isDirect
public boolean isDirect()
Description copied from interface:ChannelBufferReturnstrueif and only if this buffer is backed by an NIO direct buffer.
-
hasArray
public boolean hasArray()
Description copied from interface:ChannelBufferReturnstrueif and only if this buffer has a backing byte array. If this method returns true, you can safely callChannelBuffer.array()andChannelBuffer.arrayOffset().
-
array
public byte[] array()
Description copied from interface:ChannelBufferReturns the backing byte array of this buffer.
-
arrayOffset
public int arrayOffset()
Description copied from interface:ChannelBufferReturns the offset of the first byte within the backing byte array of this buffer.
-
capacity
public int capacity()
Description copied from interface:ChannelBufferReturns the number of bytes (octets) this buffer can contain.
-
numComponents
public int numComponents()
-
getByte
public byte getByte(int index)
Description copied from interface:ChannelBufferGets a byte at the specified absoluteindexin this buffer. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
getShort
public short getShort(int index)
Description copied from interface:ChannelBufferGets a 16-bit short integer at the specified absoluteindexin this buffer. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
getUnsignedMedium
public int getUnsignedMedium(int index)
Description copied from interface:ChannelBufferGets an unsigned 24-bit medium integer at the specified absoluteindexin this buffer. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
getInt
public int getInt(int index)
Description copied from interface:ChannelBufferGets a 32-bit integer at the specified absoluteindexin this buffer. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
getLong
public long getLong(int index)
Description copied from interface:ChannelBufferGets a 64-bit long integer at the specified absoluteindexin this buffer. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
getBytes
public void getBytes(int index, byte[] dst, int dstIndex, int length)Description copied from interface:ChannelBufferTransfers this buffer's data to the specified destination starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.dstIndex- the first index of the destinationlength- the number of bytes to transfer
-
getBytes
public void getBytes(int index, java.nio.ByteBuffer dst)Description copied from interface:ChannelBufferTransfers this buffer's data to the specified destination starting at the specified absoluteindexuntil the destination's position reaches its limit. This method does not modifyreaderIndexorwriterIndexof this buffer while the destination'spositionwill be increased.
-
getBytes
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length)Description copied from interface:ChannelBufferTransfers this buffer's data to the specified destination starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof both the source (i.e.this) and the destination.dstIndex- the first index of the destinationlength- the number of bytes to transfer
-
getBytes
public int getBytes(int index, java.nio.channels.GatheringByteChannel out, int length) throws java.io.IOExceptionDescription copied from interface:ChannelBufferTransfers this buffer's data to the specified channel starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.length- the maximum number of bytes to transfer- Returns:
- the actual number of bytes written out to the specified channel
- Throws:
java.io.IOException- if the specified channel threw an exception during I/O
-
getBytes
public void getBytes(int index, java.io.OutputStream out, int length) throws java.io.IOExceptionDescription copied from interface:ChannelBufferTransfers this buffer's data to the specified stream starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.length- the number of bytes to transfer- Throws:
java.io.IOException- if the specified stream threw an exception during I/O
-
setByte
public void setByte(int index, int value)Description copied from interface:ChannelBufferSets the specified byte at the specified absoluteindexin this buffer. The 24 high-order bits of the specified value are ignored. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
setShort
public void setShort(int index, int value)Description copied from interface:ChannelBufferSets the specified 16-bit short integer at the specified absoluteindexin this buffer. The 16 high-order bits of the specified value are ignored. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
setMedium
public void setMedium(int index, int value)Description copied from interface:ChannelBufferSets the specified 24-bit medium integer at the specified absoluteindexin this buffer. Please note that the most significant byte is ignored in the specified value. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
setInt
public void setInt(int index, int value)Description copied from interface:ChannelBufferSets the specified 32-bit integer at the specified absoluteindexin this buffer. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
setLong
public void setLong(int index, long value)Description copied from interface:ChannelBufferSets the specified 64-bit long integer at the specified absoluteindexin this buffer. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
setBytes
public void setBytes(int index, byte[] src, int srcIndex, int length)Description copied from interface:ChannelBufferTransfers the specified source array's data to this buffer starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
setBytes
public void setBytes(int index, java.nio.ByteBuffer src)Description copied from interface:ChannelBufferTransfers the specified source buffer's data to this buffer starting at the specified absoluteindexuntil the source buffer's position reaches its limit. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
setBytes
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length)Description copied from interface:ChannelBufferTransfers the specified source buffer's data to this buffer starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof both the source (i.e.this) and the destination.srcIndex- the first index of the sourcelength- the number of bytes to transfer
-
setBytes
public int setBytes(int index, java.io.InputStream in, int length) throws java.io.IOExceptionDescription copied from interface:ChannelBufferTransfers the content of the specified source stream to this buffer starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.length- the number of bytes to transfer- Returns:
- the actual number of bytes read in from the specified channel.
-1if the specified channel is closed. - Throws:
java.io.IOException- if the specified stream threw an exception during I/O
-
setBytes
public int setBytes(int index, java.nio.channels.ScatteringByteChannel in, int length) throws java.io.IOExceptionDescription copied from interface:ChannelBufferTransfers the content of the specified source channel to this buffer starting at the specified absoluteindex. This method does not modifyreaderIndexorwriterIndexof this buffer.length- the maximum number of bytes to transfer- Returns:
- the actual number of bytes read in from the specified channel.
-1if the specified channel is closed. - Throws:
java.io.IOException- if the specified channel threw an exception during I/O
-
duplicate
public ChannelBuffer duplicate()
Description copied from interface:ChannelBufferReturns a buffer which shares the whole region of this buffer. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks. This method is identical tobuf.slice(0, buf.capacity()). This method does not modifyreaderIndexorwriterIndexof this buffer.
-
copy
public ChannelBuffer copy(int index, int length)
Description copied from interface:ChannelBufferReturns a copy of this buffer's sub-region. Modifying the content of the returned buffer or this buffer does not affect each other at all. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
copyTo
private void copyTo(int index, int length, int componentId, ChannelBuffer dst)
-
getBuffer
public ChannelBuffer getBuffer(int index)
Returns theChannelBufferportion of thisCompositeChannelBufferthat contains the specifiedindex. This is an expert method!Please note that since a
CompositeChannelBufferis made up of multipleChannelBuffers, this does not return the full buffer. Instead, it only returns a portion of the composite buffer where the index is located- Parameters:
index- Theindexto search for and include in the returnedChannelBuffer- Returns:
- The
ChannelBufferthat contains the specifiedindex - Throws:
java.lang.IndexOutOfBoundsException- when the specifiedindexis less than zero, or larger thancapacity()
-
slice
public ChannelBuffer slice(int index, int length)
Description copied from interface:ChannelBufferReturns a slice of this buffer's sub-region. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
toByteBuffer
public java.nio.ByteBuffer toByteBuffer(int index, int length)Description copied from interface:ChannelBufferConverts this buffer's sub-region into a NIO buffer. The returned buffer might or might not share the content with this buffer, while they have separate indexes and marks. This method does not modifyreaderIndexorwriterIndexof this buffer.
-
toByteBuffers
public java.nio.ByteBuffer[] toByteBuffers(int index, int length)Description copied from interface:ChannelBufferConverts this buffer's sub-region into an array of NIO buffers. The returned buffers might or might not share the content with this buffer, while they have separate indexes and marks. This method does not modifyreaderIndexorwriterIndexof this buffer.- Specified by:
toByteBuffersin interfaceChannelBuffer- Overrides:
toByteBuffersin classAbstractChannelBuffer
-
componentId
private int componentId(int index)
-
discardReadBytes
public void discardReadBytes()
Description copied from interface:ChannelBufferDiscards the bytes between the 0th index andreaderIndex. It moves the bytes betweenreaderIndexandwriterIndexto the 0th index, and setsreaderIndexandwriterIndexto0andoldWriterIndex - oldReaderIndexrespectively.Please refer to the class documentation for more detailed explanation.
- Specified by:
discardReadBytesin interfaceChannelBuffer- Overrides:
discardReadBytesin classAbstractChannelBuffer
-
toString
public java.lang.String toString()
Description copied from interface:ChannelBufferReturns the string representation of this buffer. This method does not necessarily return the whole content of the buffer but returns the values of the key properties such asChannelBuffer.readerIndex(),ChannelBuffer.writerIndex()andChannelBuffer.capacity().- Specified by:
toStringin interfaceChannelBuffer- Overrides:
toStringin classAbstractChannelBuffer
-
-