Java ByteBuffer Resize resizeBuffer(final ByteBuffer in)

Here you can find the source of resizeBuffer(final ByteBuffer in)

Description

A helper method to resize byte buffer upon read.

License

Open Source License

Parameter

Parameter Description
in a parameter

Declaration

private static ByteBuffer resizeBuffer(final ByteBuffer in) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    private static final int READ_BLOCK = 8192;

    /**/*w w  w.  ja v  a2  s.  co  m*/
     * A helper method to resize byte buffer upon read.
     *
     * @param in
     * @return
     */
    private static ByteBuffer resizeBuffer(final ByteBuffer in) {
        if (in.remaining() < READ_BLOCK) {
            // create new buffer with double capacity
            final ByteBuffer result = ByteBuffer.allocate(in.capacity() * 2);
            // flip the in buffer in preparation for it to be copied into the newly created larger buffer
            in.flip();
            // copy the in buffer into new buffer
            result.put(in);
            return result;
        }
        return in;
    }
}

Related

  1. increaseBufferCapatity(ByteBuffer byteBuffer)
  2. increaseByteBuffer(ByteBuffer byteBuffer, int increase)
  3. performResize(BufferedImage source, int newWidth, int newHeight)
  4. resize(ByteBuffer buffer)
  5. resize(ByteBuffer oldBuffer, int newSize)
  6. resizeByteBuffer(ByteBuffer buf, long size)