Package org.jboss.netty.buffer
Class EmptyChannelBuffer
- java.lang.Object
-
- org.jboss.netty.buffer.AbstractChannelBuffer
-
- org.jboss.netty.buffer.HeapChannelBuffer
-
- org.jboss.netty.buffer.BigEndianHeapChannelBuffer
-
- org.jboss.netty.buffer.EmptyChannelBuffer
-
- All Implemented Interfaces:
Comparable<ChannelBuffer>
,ChannelBuffer
public class EmptyChannelBuffer extends BigEndianHeapChannelBuffer
An immutable empty buffer implementation. Typically used as a singleton viaChannelBuffers.EMPTY_BUFFER
and returned byChannelBuffers.buffer(int)
etc when an empty buffer is requested.Note: For backwards compatibility, this class extends
BigEndianHeapChannelBuffer
. However, it never makes any writes to the reader and writer indices, which avoids contention when the singleton instance is used concurrently.
-
-
Field Summary
-
Fields inherited from class org.jboss.netty.buffer.HeapChannelBuffer
array
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkReadableBytes(int length)
Throws anIndexOutOfBoundsException
the length is not 0.void
clear()
Sets thereaderIndex
andwriterIndex
of this buffer to0
.void
discardReadBytes()
Discards the bytes between the 0th index andreaderIndex
.void
markReaderIndex()
Marks the currentreaderIndex
in this buffer.void
markWriterIndex()
Marks the currentwriterIndex
in this buffer.void
readBytes(byte[] dst)
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
and increases thereaderIndex
by the number of the transferred bytes (=dst.length
).void
readBytes(byte[] dst, int dstIndex, int length)
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
and increases thereaderIndex
by the number of the transferred bytes (=length
).ChannelBuffer
readBytes(int length)
Transfers this buffer's data to a newly created buffer starting at the currentreaderIndex
and increases thereaderIndex
by the number of the transferred bytes (=length
).void
readBytes(OutputStream out, int length)
Transfers this buffer's data to the specified stream starting at the currentreaderIndex
.void
readBytes(ByteBuffer dst)
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
until the destination's position reaches its limit, and increases thereaderIndex
by the number of the transferred bytes.int
readBytes(GatheringByteChannel out, int length)
Transfers this buffer's data to the specified stream starting at the currentreaderIndex
.void
readBytes(ChannelBuffer dst)
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
until the destination becomes non-writable, and increases thereaderIndex
by the number of the transferred bytes.void
readBytes(ChannelBuffer dst, int length)
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
and increases thereaderIndex
by the number of the transferred bytes (=length
).void
readBytes(ChannelBuffer dst, int dstIndex, int length)
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
and increases thereaderIndex
by the number of the transferred bytes (=length
).void
readerIndex(int readerIndex)
Sets thereaderIndex
of this buffer.ChannelBuffer
readSlice(int length)
Returns a new slice of this buffer's sub-region starting at the currentreaderIndex
and increases thereaderIndex
by the size of the new slice (=length
).void
resetReaderIndex()
Repositions the currentreaderIndex
to the markedreaderIndex
in this buffer.void
resetWriterIndex()
Repositions the currentwriterIndex
to the markedwriterIndex
in this buffer.void
setIndex(int readerIndex, int writerIndex)
Sets thereaderIndex
andwriterIndex
of this buffer in one shot.void
skipBytes(int length)
Increases the currentreaderIndex
by the specifiedlength
in this buffer.void
writeBytes(byte[] src, int srcIndex, int length)
Transfers the specified source array's data to this buffer starting at the currentwriterIndex
and increases thewriterIndex
by the number of the transferred bytes (=length
).int
writeBytes(InputStream in, int length)
Transfers the content of the specified stream to this buffer starting at the currentwriterIndex
and increases thewriterIndex
by the number of the transferred bytes.void
writeBytes(ByteBuffer src)
Transfers the specified source buffer's data to this buffer starting at the currentwriterIndex
until the source buffer's position reaches its limit, and increases thewriterIndex
by the number of the transferred bytes.int
writeBytes(ScatteringByteChannel in, int length)
Transfers the content of the specified channel to this buffer starting at the currentwriterIndex
and increases thewriterIndex
by the number of the transferred bytes.void
writeBytes(ChannelBuffer src, int length)
Transfers the specified source buffer's data to this buffer starting at the currentwriterIndex
and increases thewriterIndex
by the number of the transferred bytes (=length
).void
writeBytes(ChannelBuffer src, int srcIndex, int length)
Transfers the specified source buffer's data to this buffer starting at the currentwriterIndex
and increases thewriterIndex
by the number of the transferred bytes (=length
).void
writerIndex(int writerIndex)
Sets thewriterIndex
of this buffer.void
writeZero(int length)
Fills this buffer with NUL (0x00) starting at the currentwriterIndex
and increases thewriterIndex
by the specifiedlength
.-
Methods inherited from class org.jboss.netty.buffer.BigEndianHeapChannelBuffer
copy, duplicate, factory, getInt, getLong, getShort, getUnsignedMedium, order, setInt, setLong, setMedium, setShort
-
Methods inherited from class org.jboss.netty.buffer.HeapChannelBuffer
array, arrayOffset, capacity, getByte, getBytes, getBytes, getBytes, getBytes, getBytes, hasArray, isDirect, setByte, setBytes, setBytes, setBytes, setBytes, setBytes, slice, toByteBuffer
-
Methods inherited from class org.jboss.netty.buffer.AbstractChannelBuffer
bytesBefore, bytesBefore, bytesBefore, bytesBefore, bytesBefore, bytesBefore, compareTo, copy, ensureWritableBytes, equals, getBytes, getBytes, getBytes, getChar, getDouble, getFloat, getMedium, getUnsignedByte, getUnsignedInt, getUnsignedShort, hashCode, indexOf, indexOf, readable, readableBytes, readByte, readChar, readDouble, readerIndex, readFloat, readInt, readLong, readMedium, readShort, readUnsignedByte, readUnsignedInt, readUnsignedMedium, readUnsignedShort, setBytes, setBytes, setBytes, setChar, setDouble, setFloat, setZero, slice, toByteBuffer, toByteBuffers, toByteBuffers, toString, toString, toString, writable, writableBytes, writeByte, writeBytes, writeBytes, writeChar, writeDouble, writeFloat, writeInt, writeLong, writeMedium, writerIndex, writeShort
-
-
-
-
Method Detail
-
clear
public void clear()
Description copied from interface:ChannelBuffer
Sets thereaderIndex
andwriterIndex
of this buffer to0
. This method is identical tosetIndex(0, 0)
.Please note that the behavior of this method is different from that of NIO buffer, which sets the
limit
to thecapacity
of the buffer.- Specified by:
clear
in interfaceChannelBuffer
- Overrides:
clear
in classAbstractChannelBuffer
-
readerIndex
public void readerIndex(int readerIndex)
Description copied from interface:ChannelBuffer
Sets thereaderIndex
of this buffer.- Specified by:
readerIndex
in interfaceChannelBuffer
- Overrides:
readerIndex
in classAbstractChannelBuffer
-
writerIndex
public void writerIndex(int writerIndex)
Description copied from interface:ChannelBuffer
Sets thewriterIndex
of this buffer.- Specified by:
writerIndex
in interfaceChannelBuffer
- Overrides:
writerIndex
in classAbstractChannelBuffer
-
setIndex
public void setIndex(int readerIndex, int writerIndex)
Description copied from interface:ChannelBuffer
Sets thereaderIndex
andwriterIndex
of this buffer in one shot. This method is useful when you have to worry about the invocation order ofChannelBuffer.readerIndex(int)
andChannelBuffer.writerIndex(int)
methods. For example, the following code will fail:// Create a buffer whose readerIndex, writerIndex and capacity are // 0, 0 and 8 respectively.
The following code will also fail:ChannelBuffer
buf =ChannelBuffers
.buffer(8); // IndexOutOfBoundsException is thrown because the specified // readerIndex (2) cannot be greater than the current writerIndex (0). buf.readerIndex(2); buf.writerIndex(4);// Create a buffer whose readerIndex, writerIndex and capacity are // 0, 8 and 8 respectively.
By contrast, this method guarantees that it never throws anChannelBuffer
buf =ChannelBuffers
.wrappedBuffer(new byte[8]); // readerIndex becomes 8. buf.readLong(); // IndexOutOfBoundsException is thrown because the specified // writerIndex (4) cannot be less than the current readerIndex (8). buf.writerIndex(4); buf.readerIndex(2);IndexOutOfBoundsException
as long as the specified indexes meet basic constraints, regardless what the current index values of the buffer are:// No matter what the current state of the buffer is, the following // call always succeeds as long as the capacity of the buffer is not // less than 4. buf.setIndex(2, 4);
- Specified by:
setIndex
in interfaceChannelBuffer
- Overrides:
setIndex
in classAbstractChannelBuffer
-
markReaderIndex
public void markReaderIndex()
Description copied from interface:ChannelBuffer
Marks the currentreaderIndex
in this buffer. You can reposition the currentreaderIndex
to the markedreaderIndex
by callingChannelBuffer.resetReaderIndex()
. The initial value of the markedreaderIndex
is0
.- Specified by:
markReaderIndex
in interfaceChannelBuffer
- Overrides:
markReaderIndex
in classAbstractChannelBuffer
-
resetReaderIndex
public void resetReaderIndex()
Description copied from interface:ChannelBuffer
Repositions the currentreaderIndex
to the markedreaderIndex
in this buffer.- Specified by:
resetReaderIndex
in interfaceChannelBuffer
- Overrides:
resetReaderIndex
in classAbstractChannelBuffer
-
markWriterIndex
public void markWriterIndex()
Description copied from interface:ChannelBuffer
Marks the currentwriterIndex
in this buffer. You can reposition the currentwriterIndex
to the markedwriterIndex
by callingChannelBuffer.resetWriterIndex()
. The initial value of the markedwriterIndex
is0
.- Specified by:
markWriterIndex
in interfaceChannelBuffer
- Overrides:
markWriterIndex
in classAbstractChannelBuffer
-
resetWriterIndex
public void resetWriterIndex()
Description copied from interface:ChannelBuffer
Repositions the currentwriterIndex
to the markedwriterIndex
in this buffer.- Specified by:
resetWriterIndex
in interfaceChannelBuffer
- Overrides:
resetWriterIndex
in classAbstractChannelBuffer
-
discardReadBytes
public void discardReadBytes()
Description copied from interface:ChannelBuffer
Discards the bytes between the 0th index andreaderIndex
. It moves the bytes betweenreaderIndex
andwriterIndex
to the 0th index, and setsreaderIndex
andwriterIndex
to0
andoldWriterIndex - oldReaderIndex
respectively.Please refer to the class documentation for more detailed explanation.
- Specified by:
discardReadBytes
in interfaceChannelBuffer
- Overrides:
discardReadBytes
in classAbstractChannelBuffer
-
readBytes
public ChannelBuffer readBytes(int length)
Description copied from interface:ChannelBuffer
Transfers this buffer's data to a newly created buffer starting at the currentreaderIndex
and increases thereaderIndex
by the number of the transferred bytes (=length
). The returned buffer'sreaderIndex
andwriterIndex
are0
andlength
respectively.- Specified by:
readBytes
in interfaceChannelBuffer
- Overrides:
readBytes
in classAbstractChannelBuffer
- Parameters:
length
- the number of bytes to transfer- Returns:
- the newly created buffer which contains the transferred bytes
-
readSlice
public ChannelBuffer readSlice(int length)
Description copied from interface:ChannelBuffer
Returns a new slice of this buffer's sub-region starting at the currentreaderIndex
and increases thereaderIndex
by the size of the new slice (=length
).- Specified by:
readSlice
in interfaceChannelBuffer
- Overrides:
readSlice
in classAbstractChannelBuffer
- Parameters:
length
- the size of the new slice- Returns:
- the newly created slice
-
readBytes
public void readBytes(byte[] dst, int dstIndex, int length)
Description copied from interface:ChannelBuffer
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
and increases thereaderIndex
by the number of the transferred bytes (=length
).- Specified by:
readBytes
in interfaceChannelBuffer
- Overrides:
readBytes
in classAbstractChannelBuffer
dstIndex
- the first index of the destinationlength
- the number of bytes to transfer
-
readBytes
public void readBytes(byte[] dst)
Description copied from interface:ChannelBuffer
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
and increases thereaderIndex
by the number of the transferred bytes (=dst.length
).- Specified by:
readBytes
in interfaceChannelBuffer
- Overrides:
readBytes
in classAbstractChannelBuffer
-
readBytes
public void readBytes(ChannelBuffer dst)
Description copied from interface:ChannelBuffer
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
until the destination becomes non-writable, and increases thereaderIndex
by the number of the transferred bytes. This method is basically same withChannelBuffer.readBytes(ChannelBuffer, int, int)
, except that this method increases thewriterIndex
of the destination by the number of the transferred bytes whileChannelBuffer.readBytes(ChannelBuffer, int, int)
does not.- Specified by:
readBytes
in interfaceChannelBuffer
- Overrides:
readBytes
in classAbstractChannelBuffer
-
readBytes
public void readBytes(ChannelBuffer dst, int length)
Description copied from interface:ChannelBuffer
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
and increases thereaderIndex
by the number of the transferred bytes (=length
). This method is basically same withChannelBuffer.readBytes(ChannelBuffer, int, int)
, except that this method increases thewriterIndex
of the destination by the number of the transferred bytes (=length
) whileChannelBuffer.readBytes(ChannelBuffer, int, int)
does not.- Specified by:
readBytes
in interfaceChannelBuffer
- Overrides:
readBytes
in classAbstractChannelBuffer
-
readBytes
public void readBytes(ChannelBuffer dst, int dstIndex, int length)
Description copied from interface:ChannelBuffer
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
and increases thereaderIndex
by the number of the transferred bytes (=length
).- Specified by:
readBytes
in interfaceChannelBuffer
- Overrides:
readBytes
in classAbstractChannelBuffer
dstIndex
- the first index of the destinationlength
- the number of bytes to transfer
-
readBytes
public void readBytes(ByteBuffer dst)
Description copied from interface:ChannelBuffer
Transfers this buffer's data to the specified destination starting at the currentreaderIndex
until the destination's position reaches its limit, and increases thereaderIndex
by the number of the transferred bytes.- Specified by:
readBytes
in interfaceChannelBuffer
- Overrides:
readBytes
in classAbstractChannelBuffer
-
readBytes
public int readBytes(GatheringByteChannel out, int length) throws IOException
Description copied from interface:ChannelBuffer
Transfers this buffer's data to the specified stream starting at the currentreaderIndex
.- Specified by:
readBytes
in interfaceChannelBuffer
- Overrides:
readBytes
in classAbstractChannelBuffer
length
- the maximum number of bytes to transfer- Returns:
- the actual number of bytes written out to the specified channel
- Throws:
IOException
- if the specified channel threw an exception during I/O
-
readBytes
public void readBytes(OutputStream out, int length) throws IOException
Description copied from interface:ChannelBuffer
Transfers this buffer's data to the specified stream starting at the currentreaderIndex
.- Specified by:
readBytes
in interfaceChannelBuffer
- Overrides:
readBytes
in classAbstractChannelBuffer
length
- the number of bytes to transfer- Throws:
IOException
- if the specified stream threw an exception during I/O
-
skipBytes
public void skipBytes(int length)
Description copied from interface:ChannelBuffer
Increases the currentreaderIndex
by the specifiedlength
in this buffer.- Specified by:
skipBytes
in interfaceChannelBuffer
- Overrides:
skipBytes
in classAbstractChannelBuffer
-
writeBytes
public void writeBytes(byte[] src, int srcIndex, int length)
Description copied from interface:ChannelBuffer
Transfers the specified source array's data to this buffer starting at the currentwriterIndex
and increases thewriterIndex
by the number of the transferred bytes (=length
).- Specified by:
writeBytes
in interfaceChannelBuffer
- Overrides:
writeBytes
in classAbstractChannelBuffer
srcIndex
- the first index of the sourcelength
- the number of bytes to transfer
-
writeBytes
public void writeBytes(ChannelBuffer src, int length)
Description copied from interface:ChannelBuffer
Transfers the specified source buffer's data to this buffer starting at the currentwriterIndex
and increases thewriterIndex
by the number of the transferred bytes (=length
). This method is basically same withChannelBuffer.writeBytes(ChannelBuffer, int, int)
, except that this method increases thereaderIndex
of the source buffer by the number of the transferred bytes (=length
) whileChannelBuffer.writeBytes(ChannelBuffer, int, int)
does not.- Specified by:
writeBytes
in interfaceChannelBuffer
- Overrides:
writeBytes
in classAbstractChannelBuffer
length
- the number of bytes to transfer
-
writeBytes
public void writeBytes(ChannelBuffer src, int srcIndex, int length)
Description copied from interface:ChannelBuffer
Transfers the specified source buffer's data to this buffer starting at the currentwriterIndex
and increases thewriterIndex
by the number of the transferred bytes (=length
).- Specified by:
writeBytes
in interfaceChannelBuffer
- Overrides:
writeBytes
in classAbstractChannelBuffer
srcIndex
- the first index of the sourcelength
- the number of bytes to transfer
-
writeBytes
public void writeBytes(ByteBuffer src)
Description copied from interface:ChannelBuffer
Transfers the specified source buffer's data to this buffer starting at the currentwriterIndex
until the source buffer's position reaches its limit, and increases thewriterIndex
by the number of the transferred bytes.- Specified by:
writeBytes
in interfaceChannelBuffer
- Overrides:
writeBytes
in classAbstractChannelBuffer
-
writeBytes
public int writeBytes(InputStream in, int length) throws IOException
Description copied from interface:ChannelBuffer
Transfers the content of the specified stream to this buffer starting at the currentwriterIndex
and increases thewriterIndex
by the number of the transferred bytes.- Specified by:
writeBytes
in interfaceChannelBuffer
- Overrides:
writeBytes
in classAbstractChannelBuffer
length
- the number of bytes to transfer- Returns:
- the actual number of bytes read in from the specified stream
- Throws:
IOException
- if the specified stream threw an exception during I/O
-
writeBytes
public int writeBytes(ScatteringByteChannel in, int length) throws IOException
Description copied from interface:ChannelBuffer
Transfers the content of the specified channel to this buffer starting at the currentwriterIndex
and increases thewriterIndex
by the number of the transferred bytes.- Specified by:
writeBytes
in interfaceChannelBuffer
- Overrides:
writeBytes
in classAbstractChannelBuffer
length
- the maximum number of bytes to transfer- Returns:
- the actual number of bytes read in from the specified channel
- Throws:
IOException
- if the specified channel threw an exception during I/O
-
writeZero
public void writeZero(int length)
Description copied from interface:ChannelBuffer
Fills this buffer with NUL (0x00) starting at the currentwriterIndex
and increases thewriterIndex
by the specifiedlength
.- Specified by:
writeZero
in interfaceChannelBuffer
- Overrides:
writeZero
in classAbstractChannelBuffer
- Parameters:
length
- the number of NULs to write to the buffer
-
checkReadableBytes
protected void checkReadableBytes(int length)
Throws anIndexOutOfBoundsException
the length is not 0.- Overrides:
checkReadableBytes
in classAbstractChannelBuffer
-
-