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 {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.put((byte) 0xFF);
    bbuf.position(5);//from  w  w w .j av  a  2 s  .c om
    bbuf.put((byte) 0xFF);
    int pos = bbuf.position();
    int rem = bbuf.remaining();
    bbuf.limit(7);
    bbuf.rewind();
}

From source file:MainClass.java

public static void main(String args[]) {
    FileOutputStream fileOutputStream;
    FileChannel fileChannel;//from  ww w  .j a v  a  2 s  .  c o  m
    ByteBuffer byteBuffer;

    try {
        fileOutputStream = new FileOutputStream("test.txt");
        fileChannel = fileOutputStream.getChannel();
        byteBuffer = ByteBuffer.allocateDirect(26);

        for (int i = 0; i < 26; i++)
            byteBuffer.put((byte) ('A' + i));
        byteBuffer.rewind();
        fileChannel.write(byteBuffer);
        fileChannel.close();
        fileOutputStream.close();
    } catch (IOException exc) {
        System.out.println(exc);
    }
}

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   www .  j  a  va  2s.c om
    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();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*w w  w . j a v a  2s.c  o m*/
    Charset csets = Charset.forName("UTF-8");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);//  ww w . ja  va 2 s .  co m
    Charset csets = Charset.forName("UTF-16BE");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);//from   w ww .  j  a  v a  2 s. c om
    Charset csets = Charset.forName("ISO-8859-1");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);//from  w  ww. jav  a 2 s.c o m
    Charset csets = Charset.forName("US-ASCII");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*from w w  w .j  av  a  2 s. c  o m*/
    Charset csets = Charset.forName("UTF-16LE");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer
            .wrap(new byte[] { (byte) 'j', (byte) 'a', (byte) 'v', (byte) 'a', (byte) '2', (byte) 's' });
    print(bb);/*from  ww  w  . j a  va 2  s  .  c  o m*/
    Charset csets = Charset.forName("UTF-16LE");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { (byte) 'j', (byte) 'a', (byte) 'v', (byte) 'a', (byte) '2' });
    System.out.println("Initial Byte Buffer");
    print(bb);//w  w w .j av a  2s  .  c  o m
    Charset csets = Charset.forName("UTF-8");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}