Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.nio.Buffer;

import java.nio.IntBuffer;
import java.nio.ShortBuffer;

public class Main {
    /**
     * Creates an int array from the provided {@link IntBuffer} or {@link ShortBuffer}.
     * 
     * @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;
    }
}