Java ByteBuffer Slice slice(final ByteBuffer buffer, final int offset, final int length)

Here you can find the source of slice(final ByteBuffer buffer, final int offset, final int length)

Description

Slices a part of the specified ByteBuffer into a new byte buffer and returns it.

License

Open Source License

Parameter

Parameter Description
buffer The byte buffer to slice data from.
offset The offset of the part to slice.
length The length of the part to slice.

Return

The new byte buffer with the sliced part.

Declaration

public static ByteBuffer slice(final ByteBuffer buffer, final int offset, final int length) 

Method Source Code


//package com.java2s;
/*/*  ww  w .  j  a  va  2s  .  co m*/
 * Copyright 2013 Luca Longinotti <l@longi.li>
 * See LICENSE.md for licensing information.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Slices a part of the specified {@link ByteBuffer} into a new byte buffer
     * and returns it.
     * 
     * @param buffer
     *            The byte buffer to slice data from.
     * @param offset
     *            The offset of the part to slice.
     * @param length
     *            The length of the part to slice.
     * @return The new byte buffer with the sliced part.
     */
    public static ByteBuffer slice(final ByteBuffer buffer, final int offset, final int length) {
        final int oldPosition = buffer.position();
        final int oldLimit = buffer.limit();

        buffer.position(offset);
        buffer.limit(offset + length);

        final ByteBuffer slice = buffer.slice();

        buffer.position(oldPosition);
        buffer.limit(oldLimit);

        return slice;
    }
}

Related

  1. getSlice(ByteBuffer bb, int offset, int length)
  2. preservingSlice(final ByteBuffer byteBuffer, final int position, final int limit)
  3. slice(ByteBuffer buf, int pos, int limit)
  4. slice(ByteBuffer buf, int start, int end)
  5. slice(ByteBuffer data)
  6. slice(final ByteBuffer buffer, final int position)
  7. sliceBuffer(ByteBuffer byteBuffer, int start, int end)
  8. sliceByteBuffer(ByteBuffer buffer, int position, int length)
  9. sliceListBuffersPool(List buffersPool, int smallBufferSize, int buffersCount)