Package org.glassfish.grizzly.memory
Class Buffers
- java.lang.Object
-
- org.glassfish.grizzly.memory.Buffers
-
-
Field Summary
Fields Modifier and Type Field Description static BufferEMPTY_BUFFERstatic ByteBufferEMPTY_BYTE_BUFFERstatic ByteBuffer[]EMPTY_BYTE_BUFFER_ARRAY
-
Constructor Summary
Constructors Constructor Description Buffers()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static BufferappendBuffers(MemoryManager memoryManager, Buffer buffer1, Buffer buffer2)Append twoBuffers.static BufferappendBuffers(MemoryManager memoryManager, Buffer buffer1, Buffer buffer2, boolean isCompositeBufferDisposable)Append twoBuffers.static BuffercloneBuffer(Buffer srcBuffer)Clones the sourceBuffer.static BuffercloneBuffer(Buffer srcBuffer, int position, int limit)Clones the sourceBuffer.static voiddumpBuffer(Appendable appendable, Buffer buffer)Generates a hex dump of the providedBuffer.static voidfill(ByteBuffer byteBuffer, byte b)Fill theByteBufferwith the specific byte value.static voidfill(ByteBuffer byteBuffer, int position, int limit, byte b)Fill theByteBuffer's part [position, limit) with the specific byte value starting from theByteBuffer's position won't be changed.static voidfill(Buffer buffer, byte b)Fill theBufferwith the specific byte value.static voidfill(Buffer buffer, int position, int limit, byte b)static voidget(ByteBuffer srcBuffer, byte[] dstBytes, int dstOffset, int length)static Appender<Buffer>getBufferAppender(boolean isCompositeBufferDisposable)static voidput(byte[] srcBytes, int srcOffset, int length, ByteBuffer dstBuffer)static voidput(ByteBuffer srcBuffer, int srcOffset, int length, ByteBuffer dstBuffer)static voidput(Buffer src, int position, int length, Buffer dstBuffer)static longreadFromFileChannel(FileChannel fileChannel, Buffer buffer)Reads data from theFileChannelinto theBuffer.static voidsetPositionLimit(ByteBuffer buffer, int position, int limit)static voidsetPositionLimit(Buffer buffer, int position, int limit)static ByteBufferslice(ByteBuffer chunk, int size)SliceByteBufferof required size from big chunk.static ByteBufferslice(ByteBuffer byteBuffer, int position, int limit)Get theByteBuffer's slice basing on its passed position and limit.static StringtoStringContent(ByteBuffer byteBuffer, Charset charset, int position, int limit)StringtoStringContent(Buffer buffer, int headBytesCount, int tailBytesCount)StringtoStringContent(Buffer buffer, int headBytesCount, int tailBytesCount, Charset charset)static Bufferwrap(MemoryManager memoryManager, byte[] array)ReturnsBuffer, which wraps the byte array.static Bufferwrap(MemoryManager memoryManager, byte[] array, int offset, int length)ReturnsBuffer, which wraps the part of byte array with specific offset and length.static Bufferwrap(MemoryManager memoryManager, String s)static Bufferwrap(MemoryManager memoryManager, String s, Charset charset)static Bufferwrap(MemoryManager memoryManager, ByteBuffer byteBuffer)ReturnsBuffer, which wraps theByteBuffer.static longwriteToFileChannel(FileChannel fileChannel, Buffer buffer)Writes data from theBufferinto theFileChannel.
-
-
-
Field Detail
-
EMPTY_BYTE_BUFFER
public static final ByteBuffer EMPTY_BYTE_BUFFER
-
EMPTY_BYTE_BUFFER_ARRAY
public static final ByteBuffer[] EMPTY_BYTE_BUFFER_ARRAY
-
EMPTY_BUFFER
public static final Buffer EMPTY_BUFFER
-
-
Method Detail
-
getBufferAppender
public static Appender<Buffer> getBufferAppender(boolean isCompositeBufferDisposable)
Get theAppenderwhich knows how to appendBuffers. ReturnedAppenderuses the sameBufferappending rules as described hereappendBuffers(org.glassfish.grizzly.memory.MemoryManager, org.glassfish.grizzly.Buffer, org.glassfish.grizzly.Buffer, boolean).- Parameters:
isCompositeBufferDisposable- if as the result ofBuffers appending a newCompositeBufferwill be created - itsBuffer.allowBufferDispose(boolean)value will be set according to this parameter.- Returns:
- the
BufferAppender.
-
wrap
public static Buffer wrap(MemoryManager memoryManager, String s)
- Parameters:
memoryManager-MemoryManager, which should be used for wrapping.s-String- Returns:
Bufferwrapper on top of passedString.
-
wrap
public static Buffer wrap(MemoryManager memoryManager, String s, Charset charset)
-
wrap
public static Buffer wrap(MemoryManager memoryManager, byte[] array)
ReturnsBuffer, which wraps the byte array.- Parameters:
memoryManager-MemoryManager, which should be used for wrapping.array- byte array to wrap.- Returns:
Bufferwrapper on top of passed byte array.
-
wrap
public static Buffer wrap(MemoryManager memoryManager, byte[] array, int offset, int length)
ReturnsBuffer, which wraps the part of byte array with specific offset and length.- Parameters:
memoryManager-MemoryManager, which should be used for wrapping.array- byte array to wrapoffset- byte buffer offsetlength- byte buffer length- Returns:
Bufferwrapper on top of passed byte array.
-
wrap
public static Buffer wrap(MemoryManager memoryManager, ByteBuffer byteBuffer)
ReturnsBuffer, which wraps theByteBuffer.- Parameters:
memoryManager-MemoryManager, which should be used for wrapping.byteBuffer-ByteBufferto wrap- Returns:
Bufferwrapper on top of passedByteBuffer.
-
slice
public static ByteBuffer slice(ByteBuffer chunk, int size)
SliceByteBufferof required size from big chunk. Passed chunk position will be changed, after the slicing (chunk.position += size).- Parameters:
chunk- bigByteBufferpool.size- required slice size.- Returns:
- sliced
ByteBufferof required size.
-
slice
public static ByteBuffer slice(ByteBuffer byteBuffer, int position, int limit)
Get theByteBuffer's slice basing on its passed position and limit. Position and limit values of the passedByteBufferwon't be changed. The resultByteBufferposition will be equal to 0, and limit equal to number of sliced bytes (limit - position).- Parameters:
byteBuffer-ByteBufferto slice/position- the position in the passed byteBuffer, the slice will start from.limit- the limit in the passed byteBuffer, the slice will be ended.- Returns:
- sliced
ByteBufferof required size.
-
toStringContent
public static String toStringContent(ByteBuffer byteBuffer, Charset charset, int position, int limit)
-
setPositionLimit
public static void setPositionLimit(Buffer buffer, int position, int limit)
-
setPositionLimit
public static void setPositionLimit(ByteBuffer buffer, int position, int limit)
-
put
public static void put(ByteBuffer srcBuffer, int srcOffset, int length, ByteBuffer dstBuffer)
-
get
public static void get(ByteBuffer srcBuffer, byte[] dstBytes, int dstOffset, int length)
-
put
public static void put(byte[] srcBytes, int srcOffset, int length, ByteBuffer dstBuffer)
-
appendBuffers
public static Buffer appendBuffers(MemoryManager memoryManager, Buffer buffer1, Buffer buffer2)
Append twoBuffers. If one of theBuffers is null - then anotherBufferwill be returned as result. If the firstBufferisCompositeBufferthen the secondBufferwill be appended to it viaCompositeBuffer.append(Buffer), else if the secondBufferisCompositeBufferthen the firstBufferwill be prepended to it viaCompositeBuffer.prepend(org.glassfish.grizzly.Buffer). If none of theBufferparameters is null norCompositeBuffers - then newCompositeBufferwill be created and bothBuffers will be added there. The resultingCompositeBufferwill be disallowed for disposal.- Parameters:
memoryManager- theMemoryManagerto use if a newBufferneeds to be allocated in order to perform the requested operation.buffer1- theBufferto append to.buffer2- theBufferto append.- Returns:
- the result of appending of two
Buffers.
-
appendBuffers
public static Buffer appendBuffers(MemoryManager memoryManager, Buffer buffer1, Buffer buffer2, boolean isCompositeBufferDisposable)
Append twoBuffers. If one of theBuffers is null - then anotherBufferwill be returned as result. If the firstBufferisCompositeBufferthen the secondBufferwill be appended to it viaCompositeBuffer.append(Buffer), else if the secondBufferisCompositeBufferthen the firstBufferwill be prepended to it viaCompositeBuffer.prepend(org.glassfish.grizzly.Buffer). If none of theBufferparameters is null norCompositeBuffers - then newCompositeBufferwill be created and bothBuffers will be added there. The resultingCompositeBufferwill be assigned according to theisCompositeBufferDisposableparameter.- Parameters:
memoryManager- theMemoryManagerto use if a newBufferneeds to be allocated in order to perform the requested operation.buffer1- theBufferto append to.buffer2- theBufferto append.isCompositeBufferDisposable- flag indicating whether or not the resulting composite buffer may be disposed.- Returns:
- the result of appending of two
Buffers.
-
fill
public static void fill(Buffer buffer, int position, int limit, byte b)
-
fill
public static void fill(ByteBuffer byteBuffer, byte b)
Fill theByteBufferwith the specific byte value.ByteBuffer's position won't be changed.- Parameters:
byteBuffer-ByteBufferb- value
-
fill
public static void fill(ByteBuffer byteBuffer, int position, int limit, byte b)
Fill theByteBuffer's part [position, limit) with the specific byte value starting from theByteBuffer's position won't be changed.- Parameters:
byteBuffer-ByteBufferposition-ByteBufferposition to start with (inclusive)limit-Bufferlimit, where filling ends (exclusive)b- value
-
readFromFileChannel
public static long readFromFileChannel(FileChannel fileChannel, Buffer buffer) throws IOException
Reads data from theFileChannelinto theBuffer.- Parameters:
fileChannel- theFileChannelto read data from.buffer- the destinationBuffer.- Returns:
- the number of bytes read, or -1 if the end of file is reached.
- Throws:
IOExceptionif- an error occurs reading theFileChannel.IOException
-
writeToFileChannel
public static long writeToFileChannel(FileChannel fileChannel, Buffer buffer) throws IOException
Writes data from theBufferinto theFileChannel.- Parameters:
fileChannel- theFileChannelto write data to.buffer- the sourceBuffer.- Returns:
- the number of bytes written, possibly zero.
- Throws:
IOExceptionif- an error occurs writing to theFileChannel.IOException
-
toStringContent
public String toStringContent(Buffer buffer, int headBytesCount, int tailBytesCount)
-
toStringContent
public String toStringContent(Buffer buffer, int headBytesCount, int tailBytesCount, Charset charset)
Returns theBuffer'sStringrepresentation in a form:Buffer#toString()+ "[" ++ "..." + + "]" For example: HeapBuffer (1781633478) [pos=0 lim=285 cap=285][abcde...xyz]
- Parameters:
buffer- theBuffer, could be nullheadBytesCount- the number of heading bytes to include (larger or equal to 0)tailBytesCount- the number of tailing bytes to include (larger or equal to 0)charset-Charset, if null theCharset.defaultCharset()will be used- Returns:
- the
Buffer'sStringrepresentation, or null, if theBufferis null
-
dumpBuffer
public static void dumpBuffer(Appendable appendable, Buffer buffer)
Generates a hex dump of the providedBuffer.- Parameters:
appendable- theAppendableto write the hex dump to.buffer- theBufferto dump.- Since:
- 2.3.23
-
-