code: public static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length) {
      ByteBuffer slicedBuffer = ((ByteBuffer) buffer.duplicate().position(position)).slice();
      slicedBuffer.limit(length);
      return slicedBuffer;
    }
docs: Creates a new ByteBuffer sliced from a given ByteBuffer. The new ByteBuffer shares the
  content of the existing one, but with independent position/mark/limit. After slicing, the
  new ByteBuffer has position 0, and the input ByteBuffer is unmodified.
  
  @param buffer source ByteBuffer to slice
  @param position position in the source ByteBuffer to slice
  @param length length of the sliced ByteBuffer
  @return the sliced ByteBuffer