Java Utililty Methods ByteBuffer Put

List of utility methods to do ByteBuffer Put

Description

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

Method

intputVInt(ByteBuffer b, int i)
put V Int
int j = 0;
while ((i & ~0x7F) != 0) {
    b.put((byte) ((i & 0x7f) | 0x80));
    i >>>= 7;
    j++;
b.put((byte) i);
return ++j;
...
voidputWhatFits(ByteBuffer dest, ByteBuffer src)
Puts as much of src as will fit in dest.
final int toPut = Math.min(dest.remaining(), src.remaining());
if (toPut == src.remaining()) {
    dest.put(src);
} else {
    if (toPut != 0) {
        final ByteBuffer slice = src.slice();
        slice.limit(toPut);
        src.position(src.position() + toPut);
...
voidputWithChecksum(ByteBuffer buffer, int value, Adler32 checksum)
put With Checksum
buffer.putInt(value);
checksum.update(value);
voidread(InputStream is, ByteBuffer buf, int bytes)
read
while (bytes > 0 & buf.hasRemaining()) {
    int n = is.read(buf.array(), buf.position(), bytes);
    if (n < 0)
        throw new EOFException();
    buf.position(buf.position() + n);
    bytes -= n;
ByteBufferreadAsByteBuffer(final InputStream source)
Reads everything from the input stream using NIO and returns a byte buffer.
try (final ReadableByteChannel bc = Channels.newChannel(source)) {
    ByteBuffer bb = ByteBuffer.allocate(READ_BLOCK);
    while (bc.read(bb) != -1) { 
        bb = resizeBuffer(bb); 
    bb.flip();
    return bb;
intreadBER32(ByteBuffer input)
read BER
int size = 0;
for (int i = 0; i < 4; i++) {
    byte b = input.get();
    size = (size << 7) | (b & 0x7f);
    if (((b & 0xff) >> 7) == 0)
        break;
return size;
...
ByteBufferreadByteBuffer(DataInputStream is)
read Byte Buffer
int len = is.readInt();
if (len == -1) {
    return null;
byte[] arr = new byte[len];
for (int k = 0; k < len; k++) {
    arr[k] = is.readByte();
return ByteBuffer.wrap(arr);
ListreadByteBufferList(DataInputStream is)
read Byte Buffer List
int size = is.readInt();
if (size == -1) {
    return null;
List<ByteBuffer> ret = new ArrayList<ByteBuffer>(size);
for (int k = 0; k < size; k++) {
    ret.add(readByteBuffer(is));
return ret;
voidreadBytes(InputStream is, ByteBuffer buffer)
Reads as many bytes from the input stream into the given ByteBuffer, starting at current buffer position and ending at the current buffer's limit.
byte[] buf = new byte[8192];
while (buffer.remaining() > 0) {
    int len = Math.min(buf.length, buffer.remaining());
    int n = is.read(buf, 0, len);
    if (n < 0) {
        break;
    buffer.put(buf, 0, n);
...
StringreadByteVector8(ByteBuffer input)
read Byte Vector
int length = getInt8(input);
byte[] data = new byte[length];
input.get(data);
return new String(data, StandardCharsets.US_ASCII);