Java ByteBuffer Fill fill(ByteBuffer to, byte[] b, int off, int len)

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

Description

Like append, but does not throw BufferOverflowException

License

Open Source License

Parameter

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

Declaration

public static int fill(ByteBuffer to, byte[] b, int off, int len) 

Method Source Code

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

import java.nio.ByteBuffer;

public class Main {
    /**/*from w w  w  .  j  ava2  s.  co  m*/
     * Like append, but does not throw {@link BufferOverflowException}
     * @param to Buffer is flush mode
     * @param b bytes to fill
     * @param off offset into byte
     * @param len length to fill
     */
    public static int fill(ByteBuffer to, byte[] b, int off, int len) {
        int pos = flipToFill(to);
        try {
            int remaining = to.remaining();
            int take = remaining < len ? remaining : len;
            to.put(b, off, take);
            return take;
        } 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. fill(ByteBuffer buffer, int off, int len, byte val)
  2. fill(ByteBuffer buffer, int position, int length, byte filler)
  3. fillBuffer(ByteBuffer buffer, byte[] bytes)
  4. fillBuffer(ByteBuffer buffer, int seed)
  5. fillBufFromTime(ByteBuffer buf, Calendar cal)
  6. fillDdsBuffer(ByteBuffer buf)