Example usage for java.nio LongBuffer get

List of usage examples for java.nio LongBuffer get

Introduction

In this page you can find the example usage for java.nio LongBuffer get.

Prototype

public LongBuffer get(long[] dest, int off, int len) 

Source Link

Document

Reads longs from the current position into the specified long array, starting from the specified offset, and increase the position by the number of longs read.

Usage

From source file:Main.java

public static void main(String[] args) {
    LongBuffer bb = LongBuffer.allocate(10);
    bb.put(100);/*  w ww.  j  a  v  a  2s . co m*/

    bb.rewind();

    long[] longArray = new long[10];
    bb.get(longArray, 0, 2);

    System.out.println(Arrays.toString(longArray));

}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("data.dat");
    FileInputStream inFile = null;

    try {/*from www.ja va2s .  c o m*/
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }

    FileChannel inChannel = inFile.getChannel();
    final int COUNT = 6;
    ByteBuffer buf = ByteBuffer.allocate(8 * COUNT);
    long[] data = new long[COUNT];
    try {
        int pos = 0;
        while (inChannel.read(buf) != -1) {
            try {
                ((ByteBuffer) (buf.flip())).asLongBuffer().get(data);
                pos = data.length;
            } catch (BufferUnderflowException e) {
                LongBuffer longBuf = buf.asLongBuffer();
                pos = longBuf.remaining();
                longBuf.get(data, 0, pos);
            }
            System.out.println();
            for (int i = 0; i < pos; i++) {
                System.out.printf("%10d", data[i]);
            }
            buf.clear();
        }
        System.out.println("\nEOF reached.");
        inFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}