Here you can find the source of flipToFill(ByteBuffer buffer)
Parameter | Description |
---|---|
buffer | The buffer to flip |
public static int flipToFill(ByteBuffer buffer)
//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(); } }