java.lang.Object
org.glassfish.grizzly.memory.Buffers
Class has useful methods to simplify the work with
Buffers.- Author:
- Alexey Stashok
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Bufferstatic final ByteBufferstatic final ByteBuffer[] -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 theBufferwith the specific byte value.static voidstatic voidget(ByteBuffer srcBuffer, byte[] dstBytes, int dstOffset, int length) getBufferAppender(boolean isCompositeBufferDisposable) static voidput(byte[] srcBytes, int srcOffset, int length, ByteBuffer dstBuffer) static voidput(ByteBuffer srcBuffer, int srcOffset, int length, ByteBuffer dstBuffer) static voidstatic 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) toStringContent(Buffer buffer, int headBytesCount, int tailBytesCount) toStringContent(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 Details
-
EMPTY_BYTE_BUFFER
-
EMPTY_BYTE_BUFFER_ARRAY
-
EMPTY_BUFFER
-
-
Constructor Details
-
Buffers
public Buffers()
-
-
Method Details
-
getBufferAppender
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
- Parameters:
memoryManager-MemoryManager, which should be used for wrapping.s-String- Returns:
Bufferwrapper on top of passedString.
-
wrap
-
wrap
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
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
ReturnsBuffer, which wraps theByteBuffer.- Parameters:
memoryManager-MemoryManager, which should be used for wrapping.byteBuffer-ByteBufferto wrap- Returns:
Bufferwrapper on top of passedByteBuffer.
-
slice
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
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
-
setPositionLimit
-
put
-
put
-
get
-
put
-
appendBuffers
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
- Parameters:
buffer-Bufferb- value
-
fill
-
fill
Fill theByteBufferwith the specific byte value.ByteBuffer's position won't be changed.- Parameters:
byteBuffer-ByteBufferb- value
-
fill
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
-
cloneBuffer
-
cloneBuffer
-
readFromFileChannel
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
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
-
toStringContent
public String toStringContent(Buffer buffer, int headBytesCount, int tailBytesCount, Charset charset) Returns theBuffer'sStringrepresentation in a form:Buffer#toString()+ "[" + <head-chunk> + "..." + <tail-chunk> + "]" 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
Generates a hex dump of the providedBuffer.- Parameters:
appendable- theAppendableto write the hex dump to.buffer- theBufferto dump.- Since:
- 2.3.23
-