Java ByteBuffer Slice sliceByteBuffer(ByteBuffer buffer, int position, int length)

Here you can find the source of sliceByteBuffer(ByteBuffer buffer, int position, int length)

Description

Creates a new ByteBuffer sliced from a given ByteBuffer.

License

Apache License

Parameter

Parameter Description
buffer source ByteBuffer to slice
position position in the source ByteBuffer to slice
length length of the sliced ByteBuffer

Return

the sliced ByteBuffer

Declaration

public static ByteBuffer sliceByteBuffer(ByteBuffer buffer,
        int position, int length) 

Method Source Code

//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();
    }
}

Related

  1. slice(ByteBuffer buf, int start, int end)
  2. slice(ByteBuffer data)
  3. slice(final ByteBuffer buffer, final int offset, final int length)
  4. slice(final ByteBuffer buffer, final int position)
  5. sliceBuffer(ByteBuffer byteBuffer, int start, int end)
  6. sliceListBuffersPool(List buffersPool, int smallBufferSize, int buffersCount)
  7. slicePosition(ByteBuffer base, int position)
  8. sliceToByte(ByteBuffer val, int offset, int length)