Example usage for java.nio LongBuffer position

List of usage examples for java.nio LongBuffer position

Introduction

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

Prototype

public final int position() 

Source Link

Document

Returns the position of this buffer.

Usage

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();/*  w  w w . jav a 2s.co  m*/
    LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
    System.out.println("Long Buffer");
    while (lb.hasRemaining())
        System.out.println(lb.position() + " -> " + lb.get());
}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();// w  w  w  .j  av a 2s.c o  m
    System.out.println("Byte Buffer");
    while (bb.hasRemaining())
        System.out.println(bb.position() + " -> " + bb.get());
    CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
    System.out.println("Char Buffer");
    while (cb.hasRemaining())
        System.out.println(cb.position() + " -> " + cb.get());
    FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer();
    System.out.println("Float Buffer");
    while (fb.hasRemaining())
        System.out.println(fb.position() + " -> " + fb.get());
    IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
    System.out.println("Int Buffer");
    while (ib.hasRemaining())
        System.out.println(ib.position() + " -> " + ib.get());
    LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
    System.out.println("Long Buffer");
    while (lb.hasRemaining())
        System.out.println(lb.position() + " -> " + lb.get());
    ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
    System.out.println("Short Buffer");
    while (sb.hasRemaining())
        System.out.println(sb.position() + " -> " + sb.get());
    DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer();
    System.out.println("Double Buffer");
    while (db.hasRemaining())
        System.out.println(db.position() + " -> " + db.get());
}

From source file:MainClass.java

public static void main(String[] args) {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/test/primes.bin");
    FileOutputStream outputFile = null;
    try {/*from  w w  w .j a  v  a  2 s .  c om*/
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    LongBuffer longBuf = buf.asLongBuffer();
    int primesWritten = 0;
    while (primesWritten < primes.length) {
        longBuf.put(primes, primesWritten, min(longBuf.capacity(), primes.length - primesWritten));
        buf.limit(8 * longBuf.position());
        try {
            file.write(buf);
            primesWritten += longBuf.position();
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        longBuf.clear();
        buf.clear();
    }
    try {
        System.out.println("File written is " + file.size() + "bytes.");
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    int count = 100;

    long[] numbers = new long[count];

    for (int i = 0; i < numbers.length; i++) {
        numbers[i] = i;//w ww . j  ava2s  . com

    }
    File aFile = new File("data.bin");
    FileOutputStream outputFile = null;
    try {
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    LongBuffer longBuf = buf.asLongBuffer();

    int numberWritten = 0;

    while (numberWritten < numbers.length) {
        longBuf.put(numbers, numberWritten, min(longBuf.capacity(), numbers.length - numberWritten));
        buf.limit(8 * longBuf.position());
        try {
            file.write(buf);
            numberWritten += longBuf.position();
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(1);
        }
        longBuf.clear();
        buf.clear();
    }

    try {
        System.out.println("File written is " + file.size() + " bytes.");
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:MainClass.java

private static void createFile() throws Exception {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/primes.bin");
    FileOutputStream outputFile = new FileOutputStream(aFile);
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    LongBuffer longBuf = buf.asLongBuffer();
    int primesWritten = 0;
    while (primesWritten < primes.length) {
        longBuf.put(primes, primesWritten, min(longBuf.capacity(), primes.length - primesWritten));
        buf.limit(8 * longBuf.position());

        file.write(buf);//from  w  w w.j  a  v  a2s  .  c  om
        primesWritten += longBuf.position();
        longBuf.clear();
        buf.clear();
    }
    System.out.println("File written is " + file.size() + "bytes.");
    outputFile.close();
}

From source file:edu.cmu.graphchi.shards.QueryShard.java

private long findVertexForOff(long qoff, final LongBuffer tmpBuffer) {
    // Binary search to find the start of the vertex
    if (tmpBuffer != null) {
        // non-pinned

        int n = tmpBuffer.capacity();
        int high = n - 1;
        int low = tmpBuffer.position();

        // Check if we are close
        long cur = tmpBuffer.get();
        long curoff = VertexIdTranslate.getAux(cur);

        // TODO/*from w w w. j  a v  a2s  .  c om*/
        if (qoff > curoff && qoff - curoff < 100) {
            long last = cur;
            final Timer.Context tmr = findEdgeByOffTimerScan.time();
            while (curoff <= qoff) {
                last = cur;
                cur = tmpBuffer.get();
                curoff = VertexIdTranslate.getAux(cur);
            }

            if (tmpBuffer.position() > 0) {
                tmpBuffer.position(tmpBuffer.position() - 1); // Backtrack one
            }
            tmr.stop();
            return VertexIdTranslate.getVertexId(last);
        }

        if (curoff > qoff) {
            low = 0;
        }
        if (curoff == qoff) {
            return VertexIdTranslate.getVertexId(cur);
        }

        while (low <= high) {
            int idx = ((high + low) / 2);
            if (idx == n - 1)
                idx--;
            tmpBuffer.position(idx);
            long x = tmpBuffer.get();
            long x_next = tmpBuffer.get();
            long off = VertexIdTranslate.getAux(x);
            long off_next = VertexIdTranslate.getAux(x_next);

            if (off_next > qoff && off <= qoff) {
                tmpBuffer.position(idx);
                return VertexIdTranslate.getVertexId(x);
            }
            if (off < qoff) {
                low = idx + 1;
            } else {
                high = idx - 1;
            }
        }
        throw new RuntimeException("Could not find " + qoff);
    } else {
        // pinned
        int idx = gammaSeqOffs.getIndexOfLowerBound(qoff);
        if (idx == -1) {
            for (int i = 0; i < gammaSeqOffs.length(); i++) {
                long x = gammaSeqOffs.get(i);
                if (x > qoff)
                    break;
            }

            throw new RuntimeException("(Gamma-version) Could not find " + qoff);
        }
        return gammaSeqVertices.get(idx);
    }
}