Example usage for java.nio ByteBuffer rewind

List of usage examples for java.nio ByteBuffer rewind

Introduction

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

Prototype

public final Buffer rewind() 

Source Link

Document

Rewinds this buffer.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReadableByteChannel channel = new FileInputStream("infile").getChannel();

    ByteBuffer buf = ByteBuffer.allocateDirect(10);

    int numRead = 0;
    while (numRead >= 0) {
        buf.rewind();

        numRead = channel.read(buf);/*from   w ww .  j a  va2s .  c o m*/

        buf.rewind();

        // Read bytes from ByteBuffer; see also
        for (int i = 0; i < numRead; i++) {
            byte b = buf.get();
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[12]);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();// w ww .ja va2  s  .c o m
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
}

From source file:Endians.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[12]);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));
    bb.rewind();/* w  w  w.  j  av a2s.  c o  m*/
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.asCharBuffer().put("abcdef");
    System.out.println(toString(bb.array()));

}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    for (int i = 0; i < data.length; i++)
        data[i] = (byte) i;

    ServerSocketChannel server = ServerSocketChannel.open();
    server.configureBlocking(false);/*from w ww . j  av  a 2  s  . c o m*/

    server.socket().bind(new InetSocketAddress(9000));
    Selector selector = Selector.open();
    server.register(selector, SelectionKey.OP_ACCEPT);

    while (true) {
        selector.select();
        Set readyKeys = selector.selectedKeys();
        Iterator iterator = readyKeys.iterator();
        while (iterator.hasNext()) {
            SelectionKey key = (SelectionKey) iterator.next();
            iterator.remove();
            if (key.isAcceptable()) {
                SocketChannel client = server.accept();
                System.out.println("Accepted connection from " + client);
                client.configureBlocking(false);
                ByteBuffer source = ByteBuffer.wrap(data);
                SelectionKey key2 = client.register(selector, SelectionKey.OP_WRITE);
                key2.attach(source);
            } else if (key.isWritable()) {
                SocketChannel client = (SocketChannel) key.channel();
                ByteBuffer output = (ByteBuffer) key.attachment();
                if (!output.hasRemaining()) {
                    output.rewind();
                }
                client.write(output);
            }
            key.channel().close();
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asCharBuffer().put("Howdy!");
    char c;//  ww w.j a  v  a2  s  . com
    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());

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes()));
    fc.close();/* www.j a va 2s  . c  o m*/
    fc = new FileInputStream("data2.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);
    buff.flip();

    System.out.println(buff.asCharBuffer());
    // Decode using this system's default Charset:
    buff.rewind();
    String encoding = System.getProperty("file.encoding");
    System.out.println("Decoded using " + encoding + ": " + Charset.forName(encoding).decode(buff));
    // Or, we could encode with something that will print:
    fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE")));
    fc.close();

}

From source file:ExplicitChannelRead.java

public static void main(String args[]) {
    FileInputStream fIn;//from   w ww  .  j av a  2 s  .  c  om
    FileChannel fChan;
    long fSize;
    ByteBuffer mBuf;

    try {
        fIn = new FileInputStream("test.txt");
        fChan = fIn.getChannel();
        fSize = fChan.size();
        mBuf = ByteBuffer.allocate((int) fSize);
        fChan.read(mBuf);
        mBuf.rewind();
        for (int i = 0; i < fSize; i++)
            System.out.print((char) mBuf.get());
        fChan.close();
        fIn.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    FileInputStream fIn;// w  w  w.  java2  s.  c  o  m
    FileChannel fChan;
    long fSize;
    ByteBuffer mBuf;

    try {
        fIn = new FileInputStream("test.txt");

        fChan = fIn.getChannel();

        fSize = fChan.size();

        mBuf = ByteBuffer.allocate((int) fSize);

        fChan.read(mBuf);

        mBuf.rewind();

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

From source file:BufferToText.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes()));
    fc.close();//from w  w w.j  ava  2s  . co  m
    fc = new FileInputStream("data2.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);
    buff.flip();
    // Doesn't work:
    System.out.println(buff.asCharBuffer());
    // Decode using this system's default Charset:
    buff.rewind();
    String encoding = System.getProperty("file.encoding");
    System.out.println("Decoded using " + encoding + ": " + Charset.forName(encoding).decode(buff));
    // Or, we could encode with something that will print:
    fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE")));
    fc.close();
    // Now try reading again:
    fc = new FileInputStream("data2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());
    // Use a CharBuffer to write through:
    fc = new FileOutputStream("data2.txt").getChannel();
    buff = ByteBuffer.allocate(24); // More than needed
    buff.asCharBuffer().put("Some text");
    fc.write(buff);
    fc.close();
    // Read and display:
    fc = new FileInputStream("data2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());
}

From source file:ExplicitChannelWrite.java

public static void main(String args[]) {
    FileOutputStream fOut;/*from  w  ww  . j  ava  2  s.c  o  m*/
    FileChannel fChan;
    ByteBuffer mBuf;

    try {
        fOut = new FileOutputStream("test.txt");
        fChan = fOut.getChannel();
        mBuf = ByteBuffer.allocateDirect(26);
        for (int i = 0; i < 26; i++)
            mBuf.put((byte) ('A' + i));
        mBuf.rewind();
        fChan.write(mBuf);
        fChan.close();
        fOut.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}