Java ByteBuffer Flip flipToFill(ByteBuffer buffer)

Here you can find the source of flipToFill(ByteBuffer buffer)

Description

Flip the buffer to fill mode.

License

Open Source License

Parameter

Parameter Description
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

Declaration

public static int flipToFill(ByteBuffer buffer) 

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 {
    /** 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./*  www  .  j  av a  2  s.c o m*/
     * <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;
    }

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