Java ByteBuffer Flip flipPutFlip(ByteBuffer from, ByteBuffer to)

Here you can find the source of flipPutFlip(ByteBuffer from, ByteBuffer to)

Description

Put data from one buffer into another, avoiding over/under flows

License

Open Source License

Parameter

Parameter Description
from Buffer to take bytes from in flush mode
to Buffer to put bytes to in flush mode. The buffer is flipToFill before the put and flipToFlush after.

Return

number of bytes moved

Declaration

public static int flipPutFlip(ByteBuffer from, ByteBuffer to) 

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 {
    /**/*  w  w w  .  j  ava 2  s  . com*/
     * 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 flush mode. The buffer is flipToFill before the put and flipToFlush after.
     * @return number of bytes moved
     * @deprecated use {@link #append(ByteBuffer, ByteBuffer)}
     */
    public static int flipPutFlip(ByteBuffer from, ByteBuffer to) {
        return append(to, from);
    }

    /** Append bytes to a buffer.
     * @param to Buffer is flush mode
     * @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. flip(ByteBuffer buffer)
  2. flip(ByteBuffer bytes, int width, int height)
  3. flip(ByteBuffer bytes, int width, int height)
  4. flip(ByteBuffer[] buffers)
  5. flip(ByteBuffer[] bufs)
  6. flipToFill(ByteBuffer buffer)
  7. flipToFlush(ByteBuffer buffer, int position)
  8. readAndFlip(ReadableByteChannel channel, ByteBuffer buffer, int bytes)
  9. transfer(@Nonnull final ByteBuffer aSrcBuffer, @Nonnull final ByteBuffer aDstBuffer, final boolean bNeedsFlip)