code: public static byte[] copyBytes(ByteBuffer bytes) {
          ByteBuffer copy = bytes.duplicate();
          byte[] result = new byte[copy.remaining()];    // bytes between position and limit
          copy.get(result);
          return result;
      }
docs: Extract the bytes in the given ByteBuffer and return it as a byte[] without
  affecting the mark, position, or limit of the given buffer. This method should be
  used instead of {@link #getBytes(ByteBuffer)} when the ByteBuffer might be re-read
  again.
  
  @param   bytes   ByteBuffer.
  @return          Contents between 'position' and 'limit' (aka 'remaining') as a
  byte[]. Parameter object is unaffected.