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:
java.lang.Comparable<ChannelBuffer>,ChannelBuffer
public class EmptyChannelBuffer extends BigEndianHeapChannelBuffer
An immutable empty buffer implementation. Typically used as a singleton viaChannelBuffers.EMPTY_BUFFERand 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 Modifier and Type Field Description private static byte[]BUFFER-
Fields inherited from class org.jboss.netty.buffer.HeapChannelBuffer
array
-
-
Constructor Summary
Constructors Constructor Description EmptyChannelBuffer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcheckReadableBytes(int length)Throws anIndexOutOfBoundsExceptionthe length is not 0.private voidcheckWritableBytes(int length)Throws anIndexOutOfBoundsExceptionthe length is not 0.voidclear()Sets thereaderIndexandwriterIndexof this buffer to0.voiddiscardReadBytes()Discards the bytes between the 0th index andreaderIndex.voidmarkReaderIndex()Marks the currentreaderIndexin this buffer.voidmarkWriterIndex()Marks the currentwriterIndexin this buffer.voidreadBytes(byte[] dst)Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=dst.length).voidreadBytes(byte[] dst, int dstIndex, int length)Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).ChannelBufferreadBytes(int length)Transfers this buffer's data to a newly created buffer starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).voidreadBytes(java.io.OutputStream out, int length)Transfers this buffer's data to the specified stream starting at the currentreaderIndex.voidreadBytes(java.nio.ByteBuffer dst)Transfers this buffer's data to the specified destination starting at the currentreaderIndexuntil the destination's position reaches its limit, and increases thereaderIndexby the number of the transferred bytes.intreadBytes(java.nio.channels.GatheringByteChannel out, int length)Transfers this buffer's data to the specified stream starting at the currentreaderIndex.voidreadBytes(ChannelBuffer dst)Transfers this buffer's data to the specified destination starting at the currentreaderIndexuntil the destination becomes non-writable, and increases thereaderIndexby the number of the transferred bytes.voidreadBytes(ChannelBuffer dst, int length)Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).voidreadBytes(ChannelBuffer dst, int dstIndex, int length)Transfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).voidreaderIndex(int readerIndex)Sets thereaderIndexof this buffer.ChannelBufferreadSlice(int length)Returns a new slice of this buffer's sub-region starting at the currentreaderIndexand increases thereaderIndexby the size of the new slice (=length).voidresetReaderIndex()Repositions the currentreaderIndexto the markedreaderIndexin this buffer.voidresetWriterIndex()Repositions the currentwriterIndexto the markedwriterIndexin this buffer.voidsetIndex(int readerIndex, int writerIndex)Sets thereaderIndexandwriterIndexof this buffer in one shot.voidskipBytes(int length)Increases the currentreaderIndexby the specifiedlengthin this buffer.voidwriteBytes(byte[] src, int srcIndex, int length)Transfers the specified source array's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).intwriteBytes(java.io.InputStream in, int length)Transfers the content of the specified stream to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes.voidwriteBytes(java.nio.ByteBuffer src)Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexuntil the source buffer's position reaches its limit, and increases thewriterIndexby the number of the transferred bytes.intwriteBytes(java.nio.channels.ScatteringByteChannel in, int length)Transfers the content of the specified channel to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes.voidwriteBytes(ChannelBuffer src, int length)Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).voidwriteBytes(ChannelBuffer src, int srcIndex, int length)Transfers the specified source buffer's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).voidwriterIndex(int writerIndex)Sets thewriterIndexof this buffer.voidwriteZero(int length)Fills this buffer with NUL (0x00) starting at the currentwriterIndexand increases thewriterIndexby 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:ChannelBufferSets thereaderIndexandwriterIndexof 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
limitto thecapacityof the buffer.- Specified by:
clearin interfaceChannelBuffer- Overrides:
clearin classAbstractChannelBuffer
-
readerIndex
public void readerIndex(int readerIndex)
Description copied from interface:ChannelBufferSets thereaderIndexof this buffer.- Specified by:
readerIndexin interfaceChannelBuffer- Overrides:
readerIndexin classAbstractChannelBuffer
-
writerIndex
public void writerIndex(int writerIndex)
Description copied from interface:ChannelBufferSets thewriterIndexof this buffer.- Specified by:
writerIndexin interfaceChannelBuffer- Overrides:
writerIndexin classAbstractChannelBuffer
-
setIndex
public void setIndex(int readerIndex, int writerIndex)Description copied from interface:ChannelBufferSets thereaderIndexandwriterIndexof 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:ChannelBufferbuf =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 anChannelBufferbuf =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);IndexOutOfBoundsExceptionas 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:
setIndexin interfaceChannelBuffer- Overrides:
setIndexin classAbstractChannelBuffer
-
markReaderIndex
public void markReaderIndex()
Description copied from interface:ChannelBufferMarks the currentreaderIndexin this buffer. You can reposition the currentreaderIndexto the markedreaderIndexby callingChannelBuffer.resetReaderIndex(). The initial value of the markedreaderIndexis0.- Specified by:
markReaderIndexin interfaceChannelBuffer- Overrides:
markReaderIndexin classAbstractChannelBuffer
-
resetReaderIndex
public void resetReaderIndex()
Description copied from interface:ChannelBufferRepositions the currentreaderIndexto the markedreaderIndexin this buffer.- Specified by:
resetReaderIndexin interfaceChannelBuffer- Overrides:
resetReaderIndexin classAbstractChannelBuffer
-
markWriterIndex
public void markWriterIndex()
Description copied from interface:ChannelBufferMarks the currentwriterIndexin this buffer. You can reposition the currentwriterIndexto the markedwriterIndexby callingChannelBuffer.resetWriterIndex(). The initial value of the markedwriterIndexis0.- Specified by:
markWriterIndexin interfaceChannelBuffer- Overrides:
markWriterIndexin classAbstractChannelBuffer
-
resetWriterIndex
public void resetWriterIndex()
Description copied from interface:ChannelBufferRepositions the currentwriterIndexto the markedwriterIndexin this buffer.- Specified by:
resetWriterIndexin interfaceChannelBuffer- Overrides:
resetWriterIndexin classAbstractChannelBuffer
-
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
-
readBytes
public ChannelBuffer readBytes(int length)
Description copied from interface:ChannelBufferTransfers this buffer's data to a newly created buffer starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length). The returned buffer'sreaderIndexandwriterIndexare0andlengthrespectively.- Specified by:
readBytesin interfaceChannelBuffer- Overrides:
readBytesin 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:ChannelBufferReturns a new slice of this buffer's sub-region starting at the currentreaderIndexand increases thereaderIndexby the size of the new slice (=length).- Specified by:
readSlicein interfaceChannelBuffer- Overrides:
readSlicein 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:ChannelBufferTransfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).- Specified by:
readBytesin interfaceChannelBuffer- Overrides:
readBytesin classAbstractChannelBufferdstIndex- the first index of the destinationlength- the number of bytes to transfer
-
readBytes
public void readBytes(byte[] dst)
Description copied from interface:ChannelBufferTransfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=dst.length).- Specified by:
readBytesin interfaceChannelBuffer- Overrides:
readBytesin classAbstractChannelBuffer
-
readBytes
public void readBytes(ChannelBuffer dst)
Description copied from interface:ChannelBufferTransfers this buffer's data to the specified destination starting at the currentreaderIndexuntil the destination becomes non-writable, and increases thereaderIndexby the number of the transferred bytes. This method is basically same withChannelBuffer.readBytes(ChannelBuffer, int, int), except that this method increases thewriterIndexof the destination by the number of the transferred bytes whileChannelBuffer.readBytes(ChannelBuffer, int, int)does not.- Specified by:
readBytesin interfaceChannelBuffer- Overrides:
readBytesin classAbstractChannelBuffer
-
readBytes
public void readBytes(ChannelBuffer dst, int length)
Description copied from interface:ChannelBufferTransfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length). This method is basically same withChannelBuffer.readBytes(ChannelBuffer, int, int), except that this method increases thewriterIndexof the destination by the number of the transferred bytes (=length) whileChannelBuffer.readBytes(ChannelBuffer, int, int)does not.- Specified by:
readBytesin interfaceChannelBuffer- Overrides:
readBytesin classAbstractChannelBuffer
-
readBytes
public void readBytes(ChannelBuffer dst, int dstIndex, int length)
Description copied from interface:ChannelBufferTransfers this buffer's data to the specified destination starting at the currentreaderIndexand increases thereaderIndexby the number of the transferred bytes (=length).- Specified by:
readBytesin interfaceChannelBuffer- Overrides:
readBytesin classAbstractChannelBufferdstIndex- the first index of the destinationlength- the number of bytes to transfer
-
readBytes
public void readBytes(java.nio.ByteBuffer dst)
Description copied from interface:ChannelBufferTransfers this buffer's data to the specified destination starting at the currentreaderIndexuntil the destination's position reaches its limit, and increases thereaderIndexby the number of the transferred bytes.- Specified by:
readBytesin interfaceChannelBuffer- Overrides:
readBytesin classAbstractChannelBuffer
-
readBytes
public int readBytes(java.nio.channels.GatheringByteChannel out, int length) throws java.io.IOExceptionDescription copied from interface:ChannelBufferTransfers this buffer's data to the specified stream starting at the currentreaderIndex.- Specified by:
readBytesin interfaceChannelBuffer- Overrides:
readBytesin classAbstractChannelBufferlength- 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
-
readBytes
public void readBytes(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 currentreaderIndex.- Specified by:
readBytesin interfaceChannelBuffer- Overrides:
readBytesin classAbstractChannelBufferlength- the number of bytes to transfer- Throws:
java.io.IOException- if the specified stream threw an exception during I/O
-
skipBytes
public void skipBytes(int length)
Description copied from interface:ChannelBufferIncreases the currentreaderIndexby the specifiedlengthin this buffer.- Specified by:
skipBytesin interfaceChannelBuffer- Overrides:
skipBytesin classAbstractChannelBuffer
-
writeBytes
public void writeBytes(byte[] src, int srcIndex, int length)Description copied from interface:ChannelBufferTransfers the specified source array's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).- Specified by:
writeBytesin interfaceChannelBuffer- Overrides:
writeBytesin classAbstractChannelBuffersrcIndex- the first index of the sourcelength- the number of bytes to transfer
-
writeBytes
public void writeBytes(ChannelBuffer src, int length)
Description copied from interface:ChannelBufferTransfers the specified source buffer's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length). This method is basically same withChannelBuffer.writeBytes(ChannelBuffer, int, int), except that this method increases thereaderIndexof the source buffer by the number of the transferred bytes (=length) whileChannelBuffer.writeBytes(ChannelBuffer, int, int)does not.- Specified by:
writeBytesin interfaceChannelBuffer- Overrides:
writeBytesin classAbstractChannelBufferlength- the number of bytes to transfer
-
writeBytes
public void writeBytes(ChannelBuffer src, int srcIndex, int length)
Description copied from interface:ChannelBufferTransfers the specified source buffer's data to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes (=length).- Specified by:
writeBytesin interfaceChannelBuffer- Overrides:
writeBytesin classAbstractChannelBuffersrcIndex- the first index of the sourcelength- the number of bytes to transfer
-
writeBytes
public void writeBytes(java.nio.ByteBuffer src)
Description copied from interface:ChannelBufferTransfers the specified source buffer's data to this buffer starting at the currentwriterIndexuntil the source buffer's position reaches its limit, and increases thewriterIndexby the number of the transferred bytes.- Specified by:
writeBytesin interfaceChannelBuffer- Overrides:
writeBytesin classAbstractChannelBuffer
-
writeBytes
public int writeBytes(java.io.InputStream in, int length) throws java.io.IOExceptionDescription copied from interface:ChannelBufferTransfers the content of the specified stream to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes.- Specified by:
writeBytesin interfaceChannelBuffer- Overrides:
writeBytesin classAbstractChannelBufferlength- the number of bytes to transfer- Returns:
- the actual number of bytes read in from the specified stream
- Throws:
java.io.IOException- if the specified stream threw an exception during I/O
-
writeBytes
public int writeBytes(java.nio.channels.ScatteringByteChannel in, int length) throws java.io.IOExceptionDescription copied from interface:ChannelBufferTransfers the content of the specified channel to this buffer starting at the currentwriterIndexand increases thewriterIndexby the number of the transferred bytes.- Specified by:
writeBytesin interfaceChannelBuffer- Overrides:
writeBytesin classAbstractChannelBufferlength- the maximum number of bytes to transfer- Returns:
- the actual number of bytes read in from the specified channel
- Throws:
java.io.IOException- if the specified channel threw an exception during I/O
-
writeZero
public void writeZero(int length)
Description copied from interface:ChannelBufferFills this buffer with NUL (0x00) starting at the currentwriterIndexand increases thewriterIndexby the specifiedlength.- Specified by:
writeZeroin interfaceChannelBuffer- Overrides:
writeZeroin classAbstractChannelBuffer- Parameters:
length- the number of NULs to write to the buffer
-
checkWritableBytes
private void checkWritableBytes(int length)
Throws anIndexOutOfBoundsExceptionthe length is not 0.
-
checkReadableBytes
protected void checkReadableBytes(int length)
Throws anIndexOutOfBoundsExceptionthe length is not 0.- Overrides:
checkReadableBytesin classAbstractChannelBuffer
-
-