Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

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

Prototype

public abstract byte get();

Source Link

Document

Returns the byte at the current position and increases the position by 1.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer buf = ByteBuffer.allocateDirect(10);
    byte b = buf.get();
}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    int i = 0;//  w  w  w  . ja  v  a2 s  .  com
    while (i++ < bb.limit())
        if (bb.get() != 0)
            System.out.println("nonzero");
    System.out.println("i = " + i);
}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    int i = 0;/*from w  ww .j  a  va  2  s . c  o  m*/
    while (i++ < bb.limit())
        if (bb.get() != 0) {
            System.out.println("nonzero");
        }

    System.out.println("i = " + i);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileInputStream("data.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);//from  w  ww. j  a va  2s  . c  om
    buff.flip();
    while (buff.hasRemaining())
        System.out.print((char) buff.get());
}

From source file:GetChannel.java

public static void main(String[] args) throws Exception {
    // Write a file:
    FileChannel fc = new FileOutputStream("data.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text ".getBytes()));
    fc.close();/*from w w  w  .  j  a  va2  s  .c o m*/
    // Add to the end of the file:
    fc = new RandomAccessFile("data.txt", "rw").getChannel();
    fc.position(fc.size()); // Move to the end
    fc.write(ByteBuffer.wrap("Some more".getBytes()));
    fc.close();
    // Read the file:
    fc = new FileInputStream("data.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);
    buff.flip();
    while (buff.hasRemaining())
        System.out.print((char) buff.get());
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileInputStream fIn = new FileInputStream("test.txt");
    FileChannel fChan = fIn.getChannel();

    long fSize = fChan.size();
    ByteBuffer mBuf = ByteBuffer.allocate((int) fSize);

    fChan.read(mBuf);//from ww  w  .  java2  s . c om
    mBuf.rewind();

    for (int i = 0; i < fSize; i++) {
        System.out.print((char) mBuf.get());
    }
    fChan.close();
    fIn.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileInputStream fIn = new FileInputStream("test.txt");
    FileChannel fChan = fIn.getChannel();

    long fSize = fChan.size();
    ByteBuffer mBuf = ByteBuffer.allocate((int) fSize);

    fChan.read(mBuf, 10);//from  w ww .j  av  a 2  s . c  om
    mBuf.rewind();

    for (int i = 0; i < fSize; i++) {
        System.out.print((char) mBuf.get());
    }
    fChan.close();
    fIn.close();
}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();//from www  .j  a  v  a 2  s  . c  o m
    System.out.println("Byte Buffer");
    while (bb.hasRemaining())
        System.out.println(bb.position() + " -> " + bb.get());
}

From source file:Main.java

public static void main(String[] args) {
    File inputFile = new File("test1.txt");
    if (!inputFile.exists()) {
        System.out.println("The input file " + inputFile.getAbsolutePath() + "  does  not  exist.");
        System.out.println("Aborted the   file reading process.");
        return;/*from www .jav a  2  s  . c om*/
    }
    try (FileChannel fileChannel = new FileInputStream(inputFile).getChannel()) {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (fileChannel.read(buffer) > 0) {
            buffer.flip();
            while (buffer.hasRemaining()) {
                byte b = buffer.get();
                System.out.print((char) b);
            }
            buffer.clear();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:GetData.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    // Allocation automatically zeroes the ByteBuffer:
    int i = 0;//from   w ww .  j  a  v a2 s  .  c  o  m
    while (i++ < bb.limit())
        if (bb.get() != 0)
            System.out.println("nonzero");
    System.out.println("i = " + i);
    bb.rewind();
    // Store and read a char array:
    bb.asCharBuffer().put("Howdy!");
    char c;
    while ((c = bb.getChar()) != 0)
        System.out.print(c + " ");
    System.out.println();
    bb.rewind();
    // Store and read a short:
    bb.asShortBuffer().put((short) 471142);
    System.out.println(bb.getShort());
    bb.rewind();
    // Store and read an int:
    bb.asIntBuffer().put(99471142);
    System.out.println(bb.getInt());
    bb.rewind();
    // Store and read a long:
    bb.asLongBuffer().put(99471142);
    System.out.println(bb.getLong());
    bb.rewind();
    // Store and read a float:
    bb.asFloatBuffer().put(99471142);
    System.out.println(bb.getFloat());
    bb.rewind();
    // Store and read a double:
    bb.asDoubleBuffer().put(99471142);
    System.out.println(bb.getDouble());
    bb.rewind();

}