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

voidreadFully(InputStream in, ByteBuffer out)
read Fully
out.mark();
if (out.hasArray()) {
    while (out.hasRemaining()) {
        int r = in.read(out.array(), out.arrayOffset() + out.position(), out.remaining());
        if (r < 0)
            throw new EOFException();
        out.position(out.position() + r);
} else {
    byte[] temp = new byte[Math.min(out.remaining(), 8192)];
    while (out.hasRemaining()) {
        int r = in.read(temp, 0, Math.min(temp.length, out.remaining()));
        if (r < 0)
            throw new EOFException("" + out.position());
        out.put(temp, 0, r);
out.reset();
voidreadFully(InputStream is, ByteBuffer buf)
read Fully
while (buf.hasRemaining()) {
    int n = is.read(buf.array(), buf.position(), buf.remaining());
    if (n < 0)
        throw new EOFException();
    buf.position(buf.position() + n);
voidreadFully(InputStream is, ByteBuffer buffer, int nrBytes)
Reads the given nr of bytes from the InputStream and writes them to the buffer.
if (buffer.remaining() < nrBytes) {
    throw new IllegalArgumentException("There is not enough room left in the buffer to write " + nrBytes
            + " bytes, there is only room for " + buffer.remaining() + " bytes");
for (int i = 0; i < nrBytes; i++) {
    int b = is.read();
    if (b < 0) {
        throw new EOFException();
...
intreadInto(ByteBuffer buf, InputStream inputStream)
read Into
int read = 0;
while (buf.hasRemaining()) {
    int count = inputStream.read(buf.array(), buf.position(), buf.remaining());
    if (count < 0)
        throw new EOFException();
    read += count;
return read;
...
intreadNextLine(InputStream in, ByteBuffer buff, boolean acceptEOF, boolean requireCR)
Read and buffer bytes until end of line is detected (the newline sequence is not included).
if buff is null no bytes are buffered but the end of line is still searched for.
This method does not deal with decoding characters to avoid buffering unused bytes.
int count = 0, curr, cr = 0;
while (true) {
    curr = in.read();
    if (curr == -1) {
        if (acceptEOF)
            return count;
        throw new EOFException();
    if (curr == 10) {
        if (!requireCR || cr == 1)
            return count;
        count++;
        if (buff != null)
            buff.put((byte) 10);
    } else if (curr == 13) {
        if (cr == 0)
            cr = 1;
        else {
            count++;
            if (buff != null)
                buff.put((byte) 13);
    } else {
        count++;
        if (cr == 1)
            count++;
        if (buff != null) {
            if (cr == 1)
                buff.put((byte) 13);
            buff.put((byte) curr);
longreadPackedLong(ByteBuffer input)
read Packed Long
long result = 0;
long read;
long index = 0;
do {
    read = input.get() & 0xFF;
    result += (read & 127) << index;
    index += 7;
} while (read > 127);
...
ByteBufferreadToByteBuffer(InputStream inStream)
read To Byte Buffer
byte[] buffer = new byte[bufferSize];
ByteArrayOutputStream outStream = new ByteArrayOutputStream(bufferSize);
int read;
while (true) {
    read = inStream.read(buffer);
    if (read == -1)
        break;
    outStream.write(buffer, 0, read);
...
voidserializeNullableString(ByteBuffer outputBuffer, String value)
Serializes a nullable string into byte buffer
if (value == null) {
    outputBuffer.putInt(0);
} else {
    outputBuffer.putInt(value.length());
    outputBuffer.put(value.getBytes());
booleanserializeObject(Object obj, ByteBuffer buffer, ByteArrayOutputStream baos, boolean markEndOfHeader)
This method serializes the given object and stores it in the provided byte buffer.
baos = new ByteArrayOutputStream();
ObjectOutputStream objOutputStream = new ObjectOutputStream(baos);
objOutputStream.writeObject(obj);
objOutputStream.flush();
byte[] objAsByteArray = baos.toByteArray();
objOutputStream.close();
if (markEndOfHeader) {
    if ((objAsByteArray.length + BYTE_COUNT_INT + Byte.SIZE) > buffer.capacity()) {
...
voidsetBit(ByteBuffer input, int pos)
Sets the bit in given input to one Sets the pos bit in input to one
input.put(pos >> 3, (byte) (input.get(pos >> 3) | (1 << (pos % NBITS_PER_BYTE))));