Package org.glassfish.grizzly.memory
Class Buffers
java.lang.Object
org.glassfish.grizzly.memory.Buffers
Class has useful methods to simplify the work with
Buffer
s.- Author:
- Alexey Stashok
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Buffer
static final ByteBuffer
static final ByteBuffer[]
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Buffer
appendBuffers
(MemoryManager memoryManager, Buffer buffer1, Buffer buffer2) Append twoBuffer
s.static Buffer
appendBuffers
(MemoryManager memoryManager, Buffer buffer1, Buffer buffer2, boolean isCompositeBufferDisposable) Append twoBuffer
s.static Buffer
cloneBuffer
(Buffer srcBuffer) Clones the sourceBuffer
.static Buffer
cloneBuffer
(Buffer srcBuffer, int position, int limit) Clones the sourceBuffer
.static void
dumpBuffer
(Appendable appendable, Buffer buffer) Generates a hex dump of the providedBuffer
.static void
fill
(ByteBuffer byteBuffer, byte b) Fill theByteBuffer
with the specific byte value.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.static void
Fill theBuffer
with the specific byte value.static void
static void
get
(ByteBuffer srcBuffer, byte[] dstBytes, int dstOffset, int length) getBufferAppender
(boolean isCompositeBufferDisposable) static void
put
(byte[] srcBytes, int srcOffset, int length, ByteBuffer dstBuffer) static void
put
(ByteBuffer srcBuffer, int srcOffset, int length, ByteBuffer dstBuffer) static void
static long
readFromFileChannel
(FileChannel fileChannel, Buffer buffer) Reads data from theFileChannel
into theBuffer
.static void
setPositionLimit
(ByteBuffer buffer, int position, int limit) static void
setPositionLimit
(Buffer buffer, int position, int limit) static ByteBuffer
slice
(ByteBuffer chunk, int size) SliceByteBuffer
of required size from big chunk.static ByteBuffer
slice
(ByteBuffer byteBuffer, int position, int limit) Get theByteBuffer
's slice basing on its passed position and limit.static String
toStringContent
(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 Buffer
wrap
(MemoryManager memoryManager, byte[] array) ReturnsBuffer
, which wraps the byte array.static Buffer
wrap
(MemoryManager memoryManager, byte[] array, int offset, int length) ReturnsBuffer
, which wraps the part of byte array with specific offset and length.static Buffer
wrap
(MemoryManager memoryManager, String s) static Buffer
wrap
(MemoryManager memoryManager, String s, Charset charset) static Buffer
wrap
(MemoryManager memoryManager, ByteBuffer byteBuffer) ReturnsBuffer
, which wraps theByteBuffer
.static long
writeToFileChannel
(FileChannel fileChannel, Buffer buffer) Writes data from theBuffer
into theFileChannel
.
-
Field Details
-
EMPTY_BYTE_BUFFER
-
EMPTY_BYTE_BUFFER_ARRAY
-
EMPTY_BUFFER
-
-
Constructor Details
-
Buffers
public Buffers()
-
-
Method Details
-
getBufferAppender
Get theAppender
which knows how to appendBuffer
s. ReturnedAppender
uses the sameBuffer
appending rules as described hereappendBuffers(org.glassfish.grizzly.memory.MemoryManager, org.glassfish.grizzly.Buffer, org.glassfish.grizzly.Buffer, boolean)
.- Parameters:
isCompositeBufferDisposable
- if as the result ofBuffer
s appending a newCompositeBuffer
will be created - itsBuffer.allowBufferDispose(boolean)
value will be set according to this parameter.- Returns:
- the
Buffer
Appender
.
-
wrap
- Parameters:
memoryManager
-MemoryManager
, which should be used for wrapping.s
-String
- Returns:
Buffer
wrapper 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:
Buffer
wrapper 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:
Buffer
wrapper on top of passed byte array.
-
wrap
ReturnsBuffer
, which wraps theByteBuffer
.- Parameters:
memoryManager
-MemoryManager
, which should be used for wrapping.byteBuffer
-ByteBuffer
to wrap- Returns:
Buffer
wrapper on top of passedByteBuffer
.
-
slice
SliceByteBuffer
of required size from big chunk. Passed chunk position will be changed, after the slicing (chunk.position += size).- Parameters:
chunk
- bigByteBuffer
pool.size
- required slice size.- Returns:
- sliced
ByteBuffer
of required size.
-
slice
Get theByteBuffer
's slice basing on its passed position and limit. Position and limit values of the passedByteBuffer
won't be changed. The resultByteBuffer
position will be equal to 0, and limit equal to number of sliced bytes (limit - position).- Parameters:
byteBuffer
-ByteBuffer
to 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
ByteBuffer
of required size.
-
toStringContent
public static String toStringContent(ByteBuffer byteBuffer, Charset charset, int position, int limit) -
setPositionLimit
-
setPositionLimit
-
put
-
put
-
get
-
put
-
appendBuffers
Append twoBuffer
s. If one of theBuffer
s is null - then anotherBuffer
will be returned as result. If the firstBuffer
isCompositeBuffer
then the secondBuffer
will be appended to it viaCompositeBuffer.append(Buffer)
, else if the secondBuffer
isCompositeBuffer
then the firstBuffer
will be prepended to it viaCompositeBuffer.prepend(org.glassfish.grizzly.Buffer)
. If none of theBuffer
parameters is null norCompositeBuffer
s - then newCompositeBuffer
will be created and bothBuffer
s will be added there. The resultingCompositeBuffer
will be disallowed for disposal.- Parameters:
memoryManager
- theMemoryManager
to use if a newBuffer
needs to be allocated in order to perform the requested operation.buffer1
- theBuffer
to append to.buffer2
- theBuffer
to append.- Returns:
- the result of appending of two
Buffer
s.
-
appendBuffers
public static Buffer appendBuffers(MemoryManager memoryManager, Buffer buffer1, Buffer buffer2, boolean isCompositeBufferDisposable) Append twoBuffer
s. If one of theBuffer
s is null - then anotherBuffer
will be returned as result. If the firstBuffer
isCompositeBuffer
then the secondBuffer
will be appended to it viaCompositeBuffer.append(Buffer)
, else if the secondBuffer
isCompositeBuffer
then the firstBuffer
will be prepended to it viaCompositeBuffer.prepend(org.glassfish.grizzly.Buffer)
. If none of theBuffer
parameters is null norCompositeBuffer
s - then newCompositeBuffer
will be created and bothBuffer
s will be added there. The resultingCompositeBuffer
will be assigned according to theisCompositeBufferDisposable
parameter.- Parameters:
memoryManager
- theMemoryManager
to use if a newBuffer
needs to be allocated in order to perform the requested operation.buffer1
- theBuffer
to append to.buffer2
- theBuffer
to append.isCompositeBufferDisposable
- flag indicating whether or not the resulting composite buffer may be disposed.- Returns:
- the result of appending of two
Buffer
s.
-
fill
- Parameters:
buffer
-Buffer
b
- value
-
fill
-
fill
Fill theByteBuffer
with the specific byte value.ByteBuffer
's position won't be changed.- Parameters:
byteBuffer
-ByteBuffer
b
- value
-
fill
Fill theByteBuffer
's part [position, limit) with the specific byte value starting from theByteBuffer
's position won't be changed.- Parameters:
byteBuffer
-ByteBuffer
position
-ByteBuffer
position to start with (inclusive)limit
-Buffer
limit, where filling ends (exclusive)b
- value
-
cloneBuffer
-
cloneBuffer
-
readFromFileChannel
Reads data from theFileChannel
into theBuffer
.- Parameters:
fileChannel
- theFileChannel
to 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 theBuffer
into theFileChannel
.- Parameters:
fileChannel
- theFileChannel
to 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
'sString
representation 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
'sString
representation, or null, if theBuffer
is null
-
dumpBuffer
Generates a hex dump of the providedBuffer
.- Parameters:
appendable
- theAppendable
to write the hex dump to.buffer
- theBuffer
to dump.- Since:
- 2.3.23
-