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

Java examples for java.nio:ByteBuffer

Description

Returns a slice of the bytebuffer starting at mark with 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  av  a 2 s  .co  m*/
     * Returns a slice of the bytebuffer starting at mark with given length.
     * 
     * @param buf
     * @param length
     * @return
     */
    final static public ByteBuffer sliceMarkToLength(ByteBuffer buf,
            int length) {
        int pos = buf.position();
        int lim = buf.limit();
        buf.reset();
        buf.limit(buf.position() + length);
        ByteBuffer save = buf.slice();
        buf.position(pos);
        buf.limit(lim);
        return save;
    }
}

Related Tutorials