Here you can find the source of sliceByteBuffer(ByteBuffer buffer, int position, int length)
Parameter | Description |
---|---|
buffer | source ByteBuffer to slice |
position | position in the source ByteBuffer to slice |
length | length of the sliced ByteBuffer |
public static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length)
//package com.java2s; /*// w ww. ja va 2 s .c o m * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0 * (the ?License??). You may not use this work except in compliance with the License, which is * available at www.apache.org/licenses/LICENSE-2.0 * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied, as more fully set forth in the License. * * See the NOTICE file distributed with this work for information regarding copyright ownership. */ import java.nio.ByteBuffer; public class Main { /** * 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 */ public static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position, int length) { ByteBuffer slicedBuffer = ((ByteBuffer) buffer.duplicate() .position(position)).slice(); slicedBuffer.limit(length); return slicedBuffer; } /** * Convenience method for {@link #sliceByteBuffer(ByteBuffer, int, int)} where the last parameter * is the number of remaining bytes in the new buffer. * * @param buffer source {@link ByteBuffer} to slice * @param position position in the source {@link ByteBuffer} to slice * @return the sliced {@link ByteBuffer} */ public static ByteBuffer sliceByteBuffer(ByteBuffer buffer, int position) { // The following is an optimization comparing to directly calling // sliceByteBuffer(ByteBuffer, int, int) needs to compute the length of the sliced buffer and // set the limit, but those operations should have been taken care of by the slice() method. return ((ByteBuffer) buffer.duplicate().position(position)).slice(); } }