Code example for ByteBuffer

Methods: array, limit, position

0
    buffer.position(i + 1);	// skip the null byte
    return newBuffer;
  } 
 
  public static String byteBufferToString(ByteBuffer buffer) {
    int position = buffer.position();
    int limit = buffer.limit();
    return new String(buffer.array(), position, limit - position);
  } 
 
  public static void fixLength(byte[] bytes) {
    fixLength(bytes, 1);
  } 
 
  public static void fixLength(byte[] bytes, int offset) {
    int length = bytes.length - offset;
    bytes[offset]     = (byte) (length >> 24);
    bytes[offset + 1] = (byte) (length >> 16);
    bytes[offset + 2] = (byte) (length >> 8);
    bytes[offset + 3] = (byte) (length);
  }