Example usage for java.nio Buffer rewind

List of usage examples for java.nio Buffer rewind

Introduction

In this page you can find the example usage for java.nio Buffer rewind.

Prototype

public final Buffer rewind() 

Source Link

Document

Rewinds this buffer.

Usage

From source file:Main.java

/**
 * Creates an int array from the provided {@link IntBuffer} or {@link ShortBuffer}.
 * //from ww  w  .  jav  a2s  .  co m
 * @param buffer {@link Buffer} the data source. Should be either a {@link IntBuffer} 
 * or {@link ShortBuffer}.
 * @return int array containing the data of the buffer.
 */
public static int[] getIntArrayFromBuffer(Buffer buffer) {
    int[] array = null;
    if (buffer.hasArray()) {
        array = (int[]) buffer.array();
    } else {
        buffer.rewind();
        array = new int[buffer.capacity()];
        if (buffer instanceof IntBuffer) {
            ((IntBuffer) buffer).get(array);
        } else if (buffer instanceof ShortBuffer) {
            int count = 0;
            while (buffer.hasRemaining()) {
                array[count] = (int) (((ShortBuffer) buffer).get());
                ++count;
            }
        }
    }
    return array;
}