Top Description Inners Fields Constructors Methods
org.glassfish.grizzly.memory

public Class Buffers

extends Object
Class Inheritance
Imports
java.io.IOException, .UnsupportedEncodingException, java.nio.BufferOverflowException, .BufferUnderflowException, .ByteBuffer, java.nio.channels.FileChannel, java.nio.charset.Charset, java.util.Arrays, .Formatter, java.util.logging.Level, .Logger, org.glassfish.grizzly.Appender, .Buffer, .Grizzly, org.glassfish.grizzly.localization.LogMessages

Class has useful methods to simplify the work with Buffers.
Author
Alexey Stashok
See Also
Buffer

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
private static final Appender<Buffer>
private static final Appender<Buffer>
public static final Buffer
public static final ByteBuffer
public static final ByteBuffer[]
private static final Logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static Buffer

Returns:

the result of appending of two Buffers.
appendBuffers
(final MemoryManager<E extends Buffer>
the MemoryManager to use if a new Buffer needs to be allocated in order to perform the requested operation.
memoryManager
,
final Buffer
the Buffer to append to.
buffer1
,
final Buffer
the Buffer to append.
buffer2
)

Append two Buffers.

public static Buffer

Returns:

the result of appending of two Buffers.
appendBuffers
(final MemoryManager<E extends Buffer>
the MemoryManager to use if a new Buffer needs to be allocated in order to perform the requested operation.
memoryManager
,
final Buffer
the Buffer to append to.
buffer1
,
final Buffer
the Buffer to append.
buffer2
,
final boolean
flag indicating whether or not the resulting composite buffer may be disposed.
isCompositeBufferDisposable
)

Append two Buffers.

public static Buffer

Returns:

the cloned Buffer.
cloneBuffer
(final Buffer
the source Buffer.
srcBuffer
)

Clones the source Buffer.

public static Buffer

Returns:

the cloned Buffer.
cloneBuffer
(final Buffer
the source Buffer.
srcBuffer
,
final int
the start position in the srcBuffer
position
,
final int
the end position in the srcBuffer
limit
)

Clones the source Buffer.

public static void
dumpBuffer(final Appendable
the Appendable to write the hex dump to.
appendable
,
final Buffer
the Buffer to dump.
buffer
)

Generates a hex dump of the provided Buffer.

private static void
dumpBuffer0(final Formatter formatter, final Appendable appendable, final Buffer buffer)

private static void
dumpBuffer0(final Formatter formatter, final Buffer buffer)

public static void
fill(final Buffer buffer, final byte
value
b
)

Fill the Buffer with the specific byte value.

public static void
fill(final Buffer buffer, final int
Buffer position to start with (inclusive)
position
,
final int
Buffer limit, where filling ends (exclusive)
limit
,
final byte
value
b
)

Fill the Buffer's part [position, limit) with the specific byte value starting from the Buffer's position won't be changed.

public static void
fill(final ByteBuffer byteBuffer, final byte
value
b
)

Fill the ByteBuffer with the specific byte value.

public static void
fill(final ByteBuffer byteBuffer, final int
ByteBuffer position to start with (inclusive)
position
,
final int
Buffer limit, where filling ends (exclusive)
limit
,
final byte
value
b
)

Fill the ByteBuffer's part [position, limit) with the specific byte value starting from the ByteBuffer's position won't be changed.

public static void
get(final ByteBuffer srcBuffer, final byte[] dstBytes, final int dstOffset, final int length)

public static Appender<Buffer>

Returns:

the Buffer Appender.
getBufferAppender
(final boolean
if as the result of Buffers appending a new CompositeBuffer will be created - its CompositeBuffer#allowBufferDispose(boolean) value will be set according to this parameter.
isCompositeBufferDisposable
)

Get the Appender which knows how to append Buffers.

private static char
getChar(final int val)

private static MemoryManager<E extends Buffer>
public static void
put(final ByteBuffer srcBuffer, final int srcOffset, final int length, final ByteBuffer dstBuffer)

public static void
put(final Buffer src, final int position, final int length, final Buffer dstBuffer)

public static void
put(final byte[] srcBytes, final int srcOffset, final int length, final ByteBuffer dstBuffer)

public static long

Returns:

the number of bytes read, or -1 if the end of file is reached.
readFromFileChannel
(final FileChannel
the FileChannel to read data from.
fileChannel
,
final Buffer
the destination Buffer.
buffer
)

Reads data from the FileChannel into the Buffer.

public static void
setPositionLimit(final Buffer buffer, final int position, final int limit)

public static void
setPositionLimit(final ByteBuffer buffer, final int position, final int limit)

public static ByteBuffer

Returns:

sliced ByteBuffer of required size.
slice
(final ByteBuffer
big ByteBuffer pool.
chunk
,
final int
required slice size.
size
)

Slice ByteBuffer of required size from big chunk.

public static ByteBuffer

Returns:

sliced ByteBuffer of required size.
slice
(final ByteBuffer
ByteBuffer to slice/
byteBuffer
,
final int
the position in the passed byteBuffer, the slice will start from.
position
,
final int
the limit in the passed byteBuffer, the slice will be ended.
limit
)

Get the ByteBuffer's slice basing on its passed position and limit.

public static String
toStringContent(final ByteBuffer byteBuffer, Charset charset, final int position, final int limit)

public String

Returns:

the Buffer's String representation, or null, if the Buffer is null
toStringContent
(final Buffer
the Buffer, could be null
buffer
,
final int
the number of heading bytes to include (larger or equal to 0)
headBytesCount
,
final int
the number of tailing bytes to include (larger or equal to 0)
tailBytesCount
)

Returns the Buffer's String representation in a form: Buffer#toString() + "[" + <head-chunk> + "..." + <tail-chunk> + "]" For example:

HeapBuffer (1781633478) [pos=0 lim=285 cap=285][abcde...xyz]
public String

Returns:

the Buffer's String representation, or null, if the Buffer is null
toStringContent
(final Buffer
the Buffer, could be null
buffer
,
final int
the number of heading bytes to include (larger or equal to 0)
headBytesCount
,
final int
the number of tailing bytes to include (larger or equal to 0)
tailBytesCount
,
final Charset
Charset, if null the Charset#defaultCharset() will be used
charset
)

Returns the Buffer's String representation in a form: Buffer#toString() + "[" + <head-chunk> + "..." + <tail-chunk> + "]" For example:

HeapBuffer (1781633478) [pos=0 lim=285 cap=285][abcde...xyz]
public static Buffer

Returns:

Buffer wrapper on top of passed String.
wrap
(final MemoryManager<E extends Buffer>
MemoryManager, which should be used for wrapping.
memoryManager
,
final String s)

Returns Buffer, which wraps the String.

public static Buffer

Returns:

Buffer wrapper on top of passed String.
wrap
(final MemoryManager<E extends Buffer>
MemoryManager, which should be used for wrapping.
memoryManager
,
final String s, final Charset
Charset, which will be used, when converting String to byte array.
charset
)

Returns Buffer, which wraps the String with the specific Charset.

public static Buffer

Returns:

Buffer wrapper on top of passed byte array.
wrap
(final MemoryManager<E extends Buffer>
MemoryManager, which should be used for wrapping.
memoryManager
,
final byte[]
byte array to wrap.
array
)

Returns Buffer, which wraps the byte array.

public static Buffer

Returns:

Buffer wrapper on top of passed byte array.
wrap
(MemoryManager<E extends Buffer>
MemoryManager, which should be used for wrapping.
memoryManager
,
final byte[]
byte array to wrap
array
,
final int
byte buffer offset
offset
,
final int
byte buffer length
length
)

Returns Buffer, which wraps the part of byte array with specific offset and length.

public static Buffer

Returns:

Buffer wrapper on top of passed ByteBuffer.
wrap
(final MemoryManager<E extends Buffer>
MemoryManager, which should be used for wrapping.
memoryManager
,
final ByteBuffer
ByteBuffer to wrap
byteBuffer
)

Returns Buffer, which wraps the ByteBuffer.

public static long

Returns:

the number of bytes written, possibly zero.
writeToFileChannel
(final FileChannel
the FileChannel to write data to.
fileChannel
,
final Buffer
the source Buffer.
buffer
)

Writes data from the Buffer into the FileChannel.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

APPENDER_DISPOSABLEback to summary
private static final Appender<Buffer> APPENDER_DISPOSABLE
APPENDER_NOT_DISPOSABLEback to summary
private static final Appender<Buffer> APPENDER_NOT_DISPOSABLE
EMPTY_BUFFERback to summary
public static final Buffer EMPTY_BUFFER
EMPTY_BYTE_BUFFERback to summary
public static final ByteBuffer EMPTY_BYTE_BUFFER
EMPTY_BYTE_BUFFER_ARRAYback to summary
public static final ByteBuffer[] EMPTY_BYTE_BUFFER_ARRAY
Annotations
@SuppressWarnings:unused
LOGGERback to summary
private static final Logger LOGGER

Constructor Detail

Buffersback to summary
public Buffers()

Method Detail

appendBuffersback to summary
public static Buffer appendBuffers(final MemoryManager<E extends Buffer> memoryManager, final Buffer buffer1, final Buffer buffer2)

Append two Buffers. If one of the Buffers is null - then another Buffer will be returned as result. If the first Buffer is CompositeBuffer then the second Buffer will be appended to it via CompositeBuffer#append(Buffer), else if the second Buffer is CompositeBuffer then the first Buffer will be prepended to it via CompositeBuffer#prepend(org.glassfish.grizzly.Buffer). If none of the Buffer parameters is null nor CompositeBuffers - then new CompositeBuffer will be created and both Buffers will be added there. The resulting CompositeBuffer will be disallowed for disposal.

Parameters
memoryManager:MemoryManager<E extends Buffer>

the MemoryManager to use if a new Buffer needs to be allocated in order to perform the requested operation.

buffer1:Buffer

the Buffer to append to.

buffer2:Buffer

the Buffer to append.

Returns:Buffer

the result of appending of two Buffers.

appendBuffersback to summary
public static Buffer appendBuffers(final MemoryManager<E extends Buffer> memoryManager, final Buffer buffer1, final Buffer buffer2, final boolean isCompositeBufferDisposable)

Append two Buffers. If one of the Buffers is null - then another Buffer will be returned as result. If the first Buffer is CompositeBuffer then the second Buffer will be appended to it via CompositeBuffer#append(Buffer), else if the second Buffer is CompositeBuffer then the first Buffer will be prepended to it via CompositeBuffer#prepend(org.glassfish.grizzly.Buffer). If none of the Buffer parameters is null nor CompositeBuffers - then new CompositeBuffer will be created and both Buffers will be added there. The resulting CompositeBuffer will be assigned according to the isCompositeBufferDisposable parameter.

Parameters
memoryManager:MemoryManager<E extends Buffer>

the MemoryManager to use if a new Buffer needs to be allocated in order to perform the requested operation.

buffer1:Buffer

the Buffer to append to.

buffer2:Buffer

the Buffer to append.

isCompositeBufferDisposable:boolean

flag indicating whether or not the resulting composite buffer may be disposed.

Returns:Buffer

the result of appending of two Buffers.

cloneBufferback to summary
public static Buffer cloneBuffer(final Buffer srcBuffer)

Clones the source Buffer. The method returns a new Buffer instance, which has the same content. Please note, source and result Buffers have the same content, but it is *not* shared, so the following content changes in one of the Buffers won't be visible in another one.

Parameters
srcBuffer:Buffer

the source Buffer.

Returns:Buffer

the cloned Buffer.

cloneBufferback to summary
public static Buffer cloneBuffer(final Buffer srcBuffer, final int position, final int limit)

Clones the source Buffer. The method returns a new Buffer instance, which has the same content. Please note, source and result Buffers have the same content, but it is *not* shared, so the following content changes in one of the Buffers won't be visible in another one.

Parameters
srcBuffer:Buffer

the source Buffer.

position:int

the start position in the srcBuffer

limit:int

the end position in the srcBuffer

Returns:Buffer

the cloned Buffer.

dumpBufferback to summary
public static void dumpBuffer(final Appendable appendable, final Buffer buffer)

Generates a hex dump of the provided Buffer.

Parameters
appendable:Appendable

the Appendable to write the hex dump to.

buffer:Buffer

the Buffer to dump.

Annotations
@SuppressWarnings:unused
Since
2.3.23
dumpBuffer0back to summary
private static void dumpBuffer0(final Formatter formatter, final Appendable appendable, final Buffer buffer)
Annotations
@SuppressWarnings:UnusedParameters
dumpBuffer0back to summary
private static void dumpBuffer0(final Formatter formatter, final Buffer buffer)
fillback to summary
public static void fill(final Buffer buffer, final byte b)

Fill the Buffer with the specific byte value. Buffer's position won't be changed.

Parameters
buffer:Buffer

Buffer

b:byte

value

fillback to summary
public static void fill(final Buffer buffer, final int position, final int limit, final byte b)

Fill the Buffer's part [position, limit) with the specific byte value starting from the Buffer's position won't be changed.

Parameters
buffer:Buffer

Buffer

position:int

Buffer position to start with (inclusive)

limit:int

Buffer limit, where filling ends (exclusive)

b:byte

value

fillback to summary
public static void fill(final ByteBuffer byteBuffer, final byte b)

Fill the ByteBuffer with the specific byte value. ByteBuffer's position won't be changed.

Parameters
byteBuffer:ByteBuffer

ByteBuffer

b:byte

value

fillback to summary
public static void fill(final ByteBuffer byteBuffer, final int position, final int limit, final byte b)

Fill the ByteBuffer's part [position, limit) with the specific byte value starting from the ByteBuffer's position won't be changed.

Parameters
byteBuffer:ByteBuffer

ByteBuffer

position:int

ByteBuffer position to start with (inclusive)

limit:int

Buffer limit, where filling ends (exclusive)

b:byte

value

getback to summary
public static void get(final ByteBuffer srcBuffer, final byte[] dstBytes, final int dstOffset, final int length)
getBufferAppenderback to summary
public static Appender<Buffer> getBufferAppender(final boolean isCompositeBufferDisposable)

Get the Appender which knows how to append Buffers. Returned Appender uses the same Buffer appending rules as described here Buffers#appendBuffers(org.glassfish.grizzly.memory.MemoryManager, org.glassfish.grizzly.Buffer, org.glassfish.grizzly.Buffer, boolean).

Parameters
isCompositeBufferDisposable:boolean

if as the result of Buffers appending a new CompositeBuffer will be created - its CompositeBuffer#allowBufferDispose(boolean) value will be set according to this parameter.

Returns:Appender<Buffer>

the Buffer Appender.

getCharback to summary
private static char getChar(final int val)
getDefaultMemoryManagerback to summary
private static MemoryManager<E extends Buffer> getDefaultMemoryManager()
putback to summary
public static void put(final ByteBuffer srcBuffer, final int srcOffset, final int length, final ByteBuffer dstBuffer)
putback to summary
public static void put(final Buffer src, final int position, final int length, final Buffer dstBuffer)
putback to summary
public static void put(final byte[] srcBytes, final int srcOffset, final int length, final ByteBuffer dstBuffer)
readFromFileChannelback to summary
public static long readFromFileChannel(final FileChannel fileChannel, final Buffer buffer) throws IOException

Reads data from the FileChannel into the Buffer.

Parameters
fileChannel:FileChannel

the FileChannel to read data from.

buffer:Buffer

the destination Buffer.

Returns:long

the number of bytes read, or -1 if the end of file is reached.

Exceptions
IOException:
if an error occurs reading the FileChannel.
setPositionLimitback to summary
public static void setPositionLimit(final Buffer buffer, final int position, final int limit)
setPositionLimitback to summary
public static void setPositionLimit(final ByteBuffer buffer, final int position, final int limit)
sliceback to summary
public static ByteBuffer slice(final ByteBuffer chunk, final int size)

Slice ByteBuffer of required size from big chunk. Passed chunk position will be changed, after the slicing (chunk.position += size).

Parameters
chunk:ByteBuffer

big ByteBuffer pool.

size:int

required slice size.

Returns:ByteBuffer

sliced ByteBuffer of required size.

sliceback to summary
public static ByteBuffer slice(final ByteBuffer byteBuffer, final int position, final int limit)

Get the ByteBuffer's slice basing on its passed position and limit. Position and limit values of the passed ByteBuffer won't be changed. The result ByteBuffer position will be equal to 0, and limit equal to number of sliced bytes (limit - position).

Parameters
byteBuffer:ByteBuffer

ByteBuffer to slice/

position:int

the position in the passed byteBuffer, the slice will start from.

limit:int

the limit in the passed byteBuffer, the slice will be ended.

Returns:ByteBuffer

sliced ByteBuffer of required size.

toStringContentback to summary
public static String toStringContent(final ByteBuffer byteBuffer, Charset charset, final int position, final int limit)
toStringContentback to summary
public String toStringContent(final Buffer buffer, final int headBytesCount, final int tailBytesCount)

Returns the Buffer's String representation in a form: Buffer#toString() + "[" + + "..." + + "]" For example:

HeapBuffer (1781633478) [pos=0 lim=285 cap=285][abcde...xyz]
Parameters
buffer:Buffer

the Buffer, could be null

headBytesCount:int

the number of heading bytes to include (larger or equal to 0)

tailBytesCount:int

the number of tailing bytes to include (larger or equal to 0)

Returns:String

the Buffer's String representation, or null, if the Buffer is null

toStringContentback to summary
public String toStringContent(final Buffer buffer, final int headBytesCount, final int tailBytesCount, final Charset charset)

Returns the Buffer's String representation in a form: Buffer#toString() + "[" + + "..." + + "]" For example:

HeapBuffer (1781633478) [pos=0 lim=285 cap=285][abcde...xyz]
Parameters
buffer:Buffer

the Buffer, could be null

headBytesCount:int

the number of heading bytes to include (larger or equal to 0)

tailBytesCount:int

the number of tailing bytes to include (larger or equal to 0)

charset:Charset

Charset, if null the Charset#defaultCharset() will be used

Returns:String

the Buffer's String representation, or null, if the Buffer is null

wrapback to summary
public static Buffer wrap(final MemoryManager<E extends Buffer> memoryManager, final String s)

Returns Buffer, which wraps the String.

Parameters
memoryManager:MemoryManager<E extends Buffer>

MemoryManager, which should be used for wrapping.

s:String

String

Returns:Buffer

Buffer wrapper on top of passed String.

wrapback to summary
public static Buffer wrap(final MemoryManager<E extends Buffer> memoryManager, final String s, final Charset charset)

Returns Buffer, which wraps the String with the specific Charset.

Parameters
memoryManager:MemoryManager<E extends Buffer>

MemoryManager, which should be used for wrapping.

s:String

String

charset:Charset

Charset, which will be used, when converting String to byte array.

Returns:Buffer

Buffer wrapper on top of passed String.

wrapback to summary
public static Buffer wrap(final MemoryManager<E extends Buffer> memoryManager, final byte[] array)

Returns Buffer, which wraps the byte array.

Parameters
memoryManager:MemoryManager<E extends Buffer>

MemoryManager, which should be used for wrapping.

array:byte[]

byte array to wrap.

Returns:Buffer

Buffer wrapper on top of passed byte array.

wrapback to summary
public static Buffer wrap(MemoryManager<E extends Buffer> memoryManager, final byte[] array, final int offset, final int length)

Returns Buffer, which wraps the part of byte array with specific offset and length.

Parameters
memoryManager:MemoryManager<E extends Buffer>

MemoryManager, which should be used for wrapping.

array:byte[]

byte array to wrap

offset:int

byte buffer offset

length:int

byte buffer length

Returns:Buffer

Buffer wrapper on top of passed byte array.

wrapback to summary
public static Buffer wrap(final MemoryManager<E extends Buffer> memoryManager, final ByteBuffer byteBuffer)

Returns Buffer, which wraps the ByteBuffer.

Parameters
memoryManager:MemoryManager<E extends Buffer>

MemoryManager, which should be used for wrapping.

byteBuffer:ByteBuffer

ByteBuffer to wrap

Returns:Buffer

Buffer wrapper on top of passed ByteBuffer.

writeToFileChannelback to summary
public static long writeToFileChannel(final FileChannel fileChannel, final Buffer buffer) throws IOException

Writes data from the Buffer into the FileChannel.

Parameters
fileChannel:FileChannel

the FileChannel to write data to.

buffer:Buffer

the source Buffer.

Returns:long

the number of bytes written, possibly zero.

Annotations
@SuppressWarnings:UnusedDeclaration
Exceptions
IOException:
if an error occurs writing to the FileChannel.
org.glassfish.grizzly.memory back to summary

private Class Buffers.BuffersAppender

extends Object
implements Appender<Buffer>
Class Inheritance
All Implemented Interfaces
org.glassfish.grizzly.Appender

Field Summary

Modifier and TypeField and Description
private final boolean

Constructor Summary

AccessConstructor and Description
public
BuffersAppender(boolean isCompositeBufferDisposable)

Method Summary

Modifier and TypeMethod and Description
public Buffer
append(final Buffer element1, final Buffer element2)

Implements org.glassfish.grizzly.Appender.append.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

isCompositeBufferDisposableback to summary
private final boolean isCompositeBufferDisposable

Constructor Detail

BuffersAppenderback to summary
public BuffersAppender(boolean isCompositeBufferDisposable)

Method Detail

appendback to summary
public Buffer append(final Buffer element1, final Buffer element2)

Implements org.glassfish.grizzly.Appender.append.

Annotations
@Override
org.glassfish.grizzly.memory back to summary

private final Class Buffers.DumpStrings

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final String[]

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DUMP_STRINGSback to summary
private static final String[] DUMP_STRINGS

Constructor Detail

DumpStringsback to summary
private DumpStrings()