Java I/O How to - Retrieve bytes between the position and limit








Question

We would like to know how to retrieve bytes between the position and limit.

Answer

  //  ww w .j  a  va2 s. c om

import java.nio.ByteBuffer;

public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(bytes);

    bytes = new byte[buf.remaining()];
    buf.get(bytes, 0, bytes.length);
  }
}