Returns a slice of the bytebuffer starting at the current position and with the given length. - Java java.nio

Java examples for java.nio:ByteBuffer

Description

Returns a slice of the bytebuffer starting at the current position and with the given length.

Demo Code


import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;
import org.apache.log4j.Logger;

public class Main{
    /**/*from  w  w w  . j ava 2s  .  c o m*/
     * Returns a slice of the bytebuffer starting at the current position and with the given length.
     * Updates position the the first position not included in returned buffer.
     * 
     * @param buf
     * @param length
     * @return
     */
    final static public ByteBuffer slicePosToLength(ByteBuffer buf,
            int length) {
        int lim = buf.limit();
        int end = buf.position() + length;
        buf.limit(end);
        ByteBuffer save = buf.slice();
        buf.position(end);
        buf.limit(lim);
        return save;
    }
}

Related Tutorials