Java Utililty Methods ByteBuffer Set

List of utility methods to do ByteBuffer Set

Description

The list of methods to do ByteBuffer Set are organized into topic(s).

Method

voidmemset(ByteBuffer dstBuffer, int dstByteOffset, byte value, int length)
memset
for (int ii = dstByteOffset; ii < dstByteOffset + length; ++ii) {
    dstBuffer.put(ii, value);
StringparseContent(ByteBuffer buffer, int offset, int length)
parse Content
byte[] chunk = new byte[length];
char[] start = new char[length];
try {
    buffer.get(chunk, offset, length);
    return new String(chunk, StandardCharsets.ISO_8859_1);
} finally {
    buffer.rewind();
ByteBufferparseToByteBuffer(Object value)
parse To Byte Buffer
if (value == null) {
    return null;
if (value instanceof ByteBuffer) {
    return (ByteBuffer) value;
} else {
    return ByteBuffer.wrap(value.toString().getBytes());
intpourBufferToArray(ByteBuffer source, byte[] destination, int offset, int sizeRequested)
Assumes source is ready to be read.
int numberToRead = Math.min(source.remaining(), sizeRequested);
source.get(destination, offset, numberToRead);
return numberToRead;
BitSetreadBitset(ByteBuffer bb, int numbytes)
Reads a series of bytes from the given buffer, turning them into a little-endian bit-set.
byte[] barr = new byte[numbytes];
bb.get(barr);
BitSet ret = new BitSet(numbytes * 8);
for (int i_byte = 0; i_byte < barr.length; i_byte++) {
    for (int i = 0; i < 8; i++) {
        int idx = i_byte * 8 + i;
        boolean isSet = (barr[i_byte] & (1 << i)) > 0;
        ret.set(idx, isSet);
...
BitSetreadBitSet(ByteBuffer buf, int len)
read Bit Set
BitSet bs = new BitSet((len + 7) >>> 3);
int i;
int b = 0;
int leftBit = 0x01;
int bitMask = 0;
for (i = 0; i < len; i++) {
    if (i % 8 == 0) {
        b = buf.get();
...
intreadBlock(ByteBuffer buffer, String fileName, long startOffset, Logger log)
Read one block from data file.
File file = new File(fileName);
if (!file.exists()) {
    if (log != null)
        log.log(Level.WARNING, "Cannot read file {0}, that does not exist", fileName);
    return 0;
if (startOffset + buffer.remaining() > file.length()) {
    int length = (int) (file.length() - startOffset);
...
voidreadFromChannel(FileChannel fc, ByteBuffer buffer, int bufferOffset, int num)
read From Channel
buffer.position(bufferOffset);
if (buffer.remaining() < num) {
    throw new BufferUnderflowException();
buffer.limit(bufferOffset + num);
int numRead = 0;
int t = 0;
while (numRead < num) {
...
byte[]readKey(ByteBuffer index, int indexByteOffset, byte[] foundKey)
read Key
index.position(indexByteOffset);
index.get(foundKey);
return foundKey;
StringreadPackageName(ByteBuffer buffer, int offset)
Reads the package name from the buffer and repositions the buffer to point directly after the package name.
Charset utf16 = Charset.forName("UTF-16LE");
String str = new String(buffer.array(), offset, PACKAGE_NAME_SIZE, utf16);
buffer.position(offset + PACKAGE_NAME_SIZE);
return str;