Example usage for java.nio ByteBuffer position

List of usage examples for java.nio ByteBuffer position

Introduction

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

Prototype

public final Buffer position(int newPosition) 

Source Link

Document

Sets the position of this buffer.

Usage

From source file:Main.java

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

From source file:MainClass.java

public static void main(String argv[]) {
    ByteBuffer bb = ByteBuffer.allocate(100);

    bb.mark();/*from w ww. j  a  v  a  2 s . c  o m*/
    bb.position(5);
    bb.reset();

    bb.mark().position(5).reset();

    char[] myBuffer = new char[100];

    CharBuffer cb = CharBuffer.wrap(myBuffer);
    cb.position(12).limit(21);

    CharBuffer sliced = cb.slice();

    System.out.println("Sliced: offset=" + sliced.arrayOffset() + ", capacity=" + sliced.capacity());
}

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());
    while (true) {
        if (buf.remaining() < 8) {
            if (inChannel.read(buf.compact()) == -1) {
                break;
            }/* w  ww  .j  a  v  a  2 s. c  om*/
            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();
}

From source file:BufferConverter.java

public static void main(String[] arguments) {
    try {/*from ww w. ja  v a  2s . c  om*/
        String data = "friends.dat";
        FileInputStream inData = new FileInputStream(data);
        FileChannel inChannel = inData.getChannel();
        long inSize = inChannel.size();
        ByteBuffer source = ByteBuffer.allocate((int) inSize);
        inChannel.read(source, 0);
        source.position(0);
        for (int i = 0; source.remaining() > 0; i++)
            System.out.print(source.get() + " ");

        source.position(0);
        Charset ascii = Charset.forName("US-ASCII");
        CharsetDecoder toAscii = ascii.newDecoder();
        CharBuffer destination = toAscii.decode(source);
        destination.position(0);
        System.out.println("\n\nNew character data:");
        for (int i = 0; destination.remaining() > 0; i++)
            System.out.print(destination.get());
    } catch (Exception ioe) {
        System.out.println(ioe.getMessage());
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    try {/*from   ww w. ja v  a2s . co  m*/
        File aFile = new File("test.txt");

        FileOutputStream outputFile = null;
        outputFile = new FileOutputStream(aFile, true);
        FileChannel outChannel = outputFile.getChannel();

        ByteBuffer buf = ByteBuffer.allocate(200);

        buf.putInt(10).asCharBuffer().put("www.java2s.com");

        buf.position(10).flip();
        outChannel.write(buf);
        buf.clear();

        outputFile.close();

    } catch (Exception e) {
        e.printStackTrace();

    }

}

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);
    bbuf.put((byte) 0xEE);
    System.out.println(Arrays.toString(bbuf.array()));
}

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);
    bbuf.put((byte) 0xFF);
    int pos = bbuf.position();
    int rem = bbuf.remaining();
    bbuf.limit(7);/*from   w w w . ja va2  s  .co m*/
    bbuf.rewind();
}

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.txt");
    FileOutputStream outputFile = null;
    try {/*from   w  ww  .ja  va2  s  . com*/
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    DoubleBuffer doubleBuf = buf.asDoubleBuffer();
    buf.position(8);
    CharBuffer charBuf = buf.asCharBuffer();
    for (long prime : primes) {
        String primeStr = "prime = " + prime;
        doubleBuf.put(0, (double) primeStr.length());
        charBuf.put(primeStr);
        buf.position(2 * charBuf.position() + 8);
        LongBuffer longBuf = buf.asLongBuffer();
        longBuf.put(prime);
        buf.position(buf.position() + 8);
        buf.flip();
        try {
            file.write(buf);
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        buf.clear();
        doubleBuf.clear();
        charBuf.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[] argv) throws Exception {
    ByteBuffer bb = ByteBuffer.allocate(20);

    bb.put((byte) 0x07);
    bb.put((byte) 0x08);
    bb.put((byte) 0x09);
    bb.put((byte) 0x10);
    bb.put((byte) 0x11);
    bb.put((byte) 0x12);
    bb.put((byte) 0x13);
    bb.put((byte) 0x14);

    bb.position(1).limit(5);
    bb.mark();// w  w  w . j  ava 2  s . co m

    System.out.println("Expect an exception here");
    System.out.println("" + bb.order().toString() + ": " + bb.getLong());

}

From source file:backtype.storm.messaging.TaskMessage.java

public static void main(String[] args) {
    String content = "Content";
    TaskMessage taskMessage = new TaskMessage(100, SerializationUtils.serialize(content));
    taskMessage.setRemoteTuple();/*from   w w w  .  j  av a2  s  .c o m*/
    ByteBuffer bytes = taskMessage.serialize();

    //        System.out.println("first: " + bytes.getShort(0));
    //        System.out.println("second: " + bytes.getShort(2));
    TaskMessage taskMessage1 = new TaskMessage(1, null);
    bytes.position(0);
    taskMessage1.deserialize(bytes);

    System.out.println(taskMessage1);
}