Creating a Non-Byte Java Type Buffer on a ByteBuffer - Java File Path IO

Java examples for File Path IO:ByteBuffer

Description

Creating a Non-Byte Java Type Buffer on a ByteBuffer

Demo Code

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

public class Main {
  public void myMethod() {
    // Obtain a ByteBuffer; see also Creating a ByteBuffer
    ByteBuffer buf = ByteBuffer.allocate(15);
    // remaining = 15

    // Create a character ByteBuffer
    CharBuffer cbuf = buf.asCharBuffer();
    // remaining = 7

    // Create a short ByteBuffer
    ShortBuffer sbuf = buf.asShortBuffer();
    // remaining = 7

    // Create an integer ByteBuffer
    IntBuffer ibuf = buf.asIntBuffer();
    // remaining = 3

    // Create a long ByteBuffer
    LongBuffer lbuf = buf.asLongBuffer();
    // remaining = 1

    // Create a float ByteBuffer
    FloatBuffer fbuf = buf.asFloatBuffer();
    // remaining = 3

    // Create a double ByteBuffer
    DoubleBuffer dbuf = buf.asDoubleBuffer();
    // remaining = 1
  }//from   w ww. j  a  va  2 s .  co m
}

Related Tutorials