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(int index);

Source Link

Document

Returns the byte at the specified index and does not change the position.

Usage

From source file:Main.java

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

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer buf = ByteBuffer.allocate(10);
    buf.putShort(0, (short) 123);
    buf.get(0); // 0
    buf.get(1); // 123
}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("java2s!");
    bb.rewind();//  www. j  av a  2 s  . c  o  m
    byte[] dst = new byte[BSIZE];
    bb.get(dst);
    System.out.println(Arrays.toString(dst));

}

From source file:Test.java

public static void main(String args[]) throws Exception {
    ExecutorService pool = new ScheduledThreadPoolExecutor(3);
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("data.txt"),
            EnumSet.of(StandardOpenOption.READ), pool);
    CompletionHandler<Integer, ByteBuffer> handler = new CompletionHandler<Integer, ByteBuffer>() {
        public synchronized void completed(Integer result, ByteBuffer attachment) {
            for (int i = 0; i < attachment.limit(); i++) {
                System.out.print((char) attachment.get(i));
            }//from  ww w .  j  a v a  2 s .  c om
        }

        public void failed(Throwable e, ByteBuffer attachment) {
        }
    };
    final int bufferCount = 5;
    ByteBuffer buffers[] = new ByteBuffer[bufferCount];
    for (int i = 0; i < bufferCount; i++) {
        buffers[i] = ByteBuffer.allocate(10);
        fileChannel.read(buffers[i], i * 10, buffers[i], handler);
    }
    pool.awaitTermination(1, TimeUnit.SECONDS);
    for (ByteBuffer byteBuffer : buffers) {
        for (int i = 0; i < byteBuffer.limit(); i++) {
            System.out.println((char) byteBuffer.get(i));
        }
    }
}

From source file:Test.java

public static void main(String args[]) throws Exception {
    ExecutorService pool = new ScheduledThreadPoolExecutor(3);
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("data.txt"),
            EnumSet.of(StandardOpenOption.READ), pool);
    CompletionHandler<Integer, ByteBuffer> handler = new CompletionHandler<Integer, ByteBuffer>() {
        @Override// w ww. ja  v a  2  s.  c  o  m
        public synchronized void completed(Integer result, ByteBuffer attachment) {
            for (int i = 0; i < attachment.limit(); i++) {
                System.out.println((char) attachment.get(i));
            }
        }

        @Override
        public void failed(Throwable e, ByteBuffer attachment) {
        }
    };
    final int bufferCount = 5;
    ByteBuffer buffers[] = new ByteBuffer[bufferCount];
    for (int i = 0; i < bufferCount; i++) {
        buffers[i] = ByteBuffer.allocate(10);
        fileChannel.read(buffers[i], i * 10, buffers[i], handler);
    }
    pool.awaitTermination(1, TimeUnit.SECONDS);
    for (ByteBuffer byteBuffer : buffers) {
        for (int i = 0; i < byteBuffer.limit(); i++) {
            System.out.print((char) byteBuffer.get(i));
        }
    }
}

From source file:Test.java

public static void main(String[] args) throws IOException {
    Path path = Paths.get("/users.txt");

    try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        sbc.position(4);//w  w w  . j av a 2  s.c  o  m
        sbc.read(buffer);
        for (int i = 0; i < 5; i++) {
            System.out.print((char) buffer.get(i));
        }

        buffer.clear();
        sbc.position(0);
        sbc.read(buffer);
        for (int i = 0; i < 4; i++) {
            System.out.print((char) buffer.get(i));
        }

    }
}

From source file:Test.java

public static void main(String[] args) throws IOException {
    Path path = Paths.get("/users.txt");

    try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        sbc.position(4);/*from   w ww . j  a  va  2s  . c  o  m*/
        sbc.read(buffer);
        for (int i = 0; i < 5; i++) {
            System.out.print((char) buffer.get(i));
        }

        buffer.clear();
        sbc.position(0);
        sbc.read(buffer);
        for (int i = 0; i < 4; i++) {
            System.out.print((char) buffer.get(i));
        }
        sbc.position(0);
        buffer = ByteBuffer.allocate(1024);
        String encoding = System.getProperty("file.encoding");
        int numberOfBytesRead = sbc.read(buffer);
        System.out.println("Number of bytes read: " + numberOfBytesRead);
        while (numberOfBytesRead > 0) {
            buffer.rewind();
            System.out.print("[" + Charset.forName(encoding).decode(buffer) + "]");
            buffer.flip();
            numberOfBytesRead = sbc.read(buffer);
            System.out.println("\nNumber of bytes read: " + numberOfBytesRead);
        }

    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File aFile = new File("main.java");
    FileInputStream inFile = new FileInputStream(aFile);
    FileChannel inChannel = inFile.getChannel();
    ByteBuffer lengthBuf = ByteBuffer.allocate(8);
    while (true) {
        if (inChannel.read(lengthBuf) == -1) {
            break;
        }/*from   w  w w .  j a va2s. co  m*/
        lengthBuf.flip();
        int strLength = (int) lengthBuf.getDouble();
        ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8);
        if (inChannel.read(buf) == -1) {
            break;
        }
        buf.flip();
        byte[] strChars = new byte[2 * strLength];
        buf.get(strChars);
        System.out.println(strLength);
        System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
        System.out.println(buf.getLong());
        lengthBuf.clear();
    }
    inFile.close();
}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("primes.txt");
    FileInputStream inFile = null;
    try {/*from ww  w  .  ja  v  a  2 s. c om*/
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel inChannel = inFile.getChannel();
    try {
        ByteBuffer lengthBuf = ByteBuffer.allocate(8);
        while (true) {
            if (inChannel.read(lengthBuf) == -1) {
                break;
            }
            lengthBuf.flip();
            int strLength = (int) lengthBuf.getDouble();
            ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8);
            if (inChannel.read(buf) == -1) {
                break;
            }
            buf.flip();
            byte[] strChars = new byte[2 * strLength];
            buf.get(strChars);
            System.out.println(strLength);
            System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
            System.out.println(buf.getLong());
            lengthBuf.clear();
        }
        inFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File aFile = new File("primes.txt");
    FileInputStream inFile = new FileInputStream(aFile);
    FileChannel inChannel = inFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
    buf.position(buf.limit());/*from   w  ww  .j  av  a  2  s  .co  m*/
    while (true) {
        if (buf.remaining() < 8) {
            if (inChannel.read(buf.compact()) == -1) {
                break;
            }
            buf.flip();
        }
        int strLength = (int) buf.getDouble();
        if (buf.remaining() < 2 * strLength) {
            if (inChannel.read(buf.compact()) == -1) {
                break;
            }
            buf.flip();
        }
        byte[] strChars = new byte[2 * strLength];
        buf.get(strChars);
        if (buf.remaining() < 8) {
            if (inChannel.read(buf.compact()) == -1) {
                break;
            }
            buf.flip();
        }
        System.out.println(strLength);
        System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
        System.out.println(buf.getLong());
    }
    inFile.close();
}