Java ByteBuffer Append append(ByteBuffer to, byte[] b, int off, int len)

Here you can find the source of append(ByteBuffer to, byte[] b, int off, int len)

Description

Append bytes to a buffer.

License

Open Source License

Parameter

Parameter Description
to Buffer is flush mode
b bytes to append
off offset into byte
len length to append

Exception

Parameter Description
BufferOverflowException an exception

Declaration

public static void append(ByteBuffer to, byte[] b, int off, int len) throws BufferOverflowException 

Method Source Code

//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;

public class Main {
    /** Append bytes to a buffer.
     * @param to Buffer is flush mode// ww  w  .ja v  a2  s  . co m
     * @param b bytes to append
     * @param off offset into byte
     * @param len length to append
     * @throws BufferOverflowException
     */
    public static void append(ByteBuffer to, byte[] b, int off, int len) throws BufferOverflowException {
        int pos = flipToFill(to);
        try {
            to.put(b, off, len);
        } finally {
            flipToFlush(to, pos);
        }
    }

    /** Appends a byte to a buffer
     * @param to Buffer is flush mode
     * @param b byte to append
     */
    public static void append(ByteBuffer to, byte b) {
        int pos = flipToFill(to);
        try {
            to.put(b);
        } finally {
            flipToFlush(to, pos);
        }
    }

    /** Appends a byte to a buffer
     * @param to Buffer is flush mode
     * @param b bytes to append
     */
    public static int append(ByteBuffer to, ByteBuffer b) {
        int pos = flipToFill(to);
        try {
            return put(b, to);
        } finally {
            flipToFlush(to, pos);
        }
    }

    /** Flip the buffer to fill mode.
     * The position is set to the first unused position in the buffer
     * (the old limit) and the limit is set to the capacity.
     * If the buffer is empty, then this call is effectively {@link #clearToFill(ByteBuffer)}.
     * If there is no unused space to fill, a {@link ByteBuffer#compact()} is done to attempt
     * to create space.
     * <p>
     * This method is used as a replacement to {@link ByteBuffer#compact()}.
     *
     * @param buffer The buffer to flip
     * @return The position of the valid data before the flipped position. This value should be
     * passed to a subsequent call to {@link #flipToFlush(ByteBuffer, int)}
     */
    public static int flipToFill(ByteBuffer buffer) {
        int position = buffer.position();
        int limit = buffer.limit();
        if (position == limit) {
            buffer.position(0);
            buffer.limit(buffer.capacity());
            return 0;
        }

        int capacity = buffer.capacity();
        if (limit == capacity) {
            buffer.compact();
            return 0;
        }

        buffer.position(limit);
        buffer.limit(capacity);
        return position;
    }

    /**
     * Put data from one buffer into another, avoiding over/under flows
     * @param from Buffer to take bytes from in flush mode
     * @param to   Buffer to put bytes to in fill mode.
     * @return number of bytes moved
     */
    public static int put(ByteBuffer from, ByteBuffer to) {
        int put;
        int remaining = from.remaining();
        if (remaining > 0) {
            if (remaining <= to.remaining()) {
                to.put(from);
                put = remaining;
                from.position(from.limit());
            } else if (from.hasArray()) {
                put = to.remaining();
                to.put(from.array(), from.arrayOffset() + from.position(), put);
                from.position(from.position() + put);
            } else {
                put = to.remaining();
                ByteBuffer slice = from.slice();
                slice.limit(put);
                to.put(slice);
                from.position(from.position() + put);
            }
        } else
            put = 0;

        return put;
    }

    /** Flip the buffer to Flush mode.
     * The limit is set to the first unused byte(the old position) and
     * the position is set to the passed position.
     * <p>
     * This method is used as a replacement of {@link Buffer#flip()}.
     * @param buffer   the buffer to be flipped
     * @param position The position of valid data to flip to. This should
     * be the return value of the previous call to {@link #flipToFill(ByteBuffer)}
     */
    public static void flipToFlush(ByteBuffer buffer, int position) {
        buffer.limit(buffer.position());
        buffer.position(position);
    }

    /** Compact the buffer
     * @param buffer the buffer to compact
     * @return true if the compact made a full buffer have space
     */
    public static boolean compact(ByteBuffer buffer) {
        if (buffer.position() == 0)
            return false;
        boolean full = buffer.limit() == buffer.capacity();
        buffer.compact().flip();
        return full && buffer.limit() < buffer.capacity();
    }
}

Related

  1. append(ByteBuffer buffer, String s)
  2. append(ByteBuffer source, int index, byte[] dest)
  3. appendBuffers(ByteBuffer buffer, ByteBuffer buffer1, int incomingBufferSize, int BUFFER_STEP_SIZE)
  4. appendHex(StringBuilder sb, ByteBuffer bb)
  5. appendLong(long value, ByteBuffer buffer)
  6. appendOrRealloc(ByteBuffer dest, ByteBuffer toAppend)