slice ByteBuffer - Java java.nio

Java examples for java.nio:ByteBuffer

Description

slice ByteBuffer

Demo Code

/*//from   w  w  w.  j a  v  a2  s . co m
 * @(#)BufferUtil.java   1.3 10/03/24
 *
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
//package com.java2s;
import java.nio.*;

public class Main {
    public static ByteBuffer slice(ByteBuffer buf, int pos, int size) {
        int origPos = buf.position();
        int origLim = buf.limit();
        buf.clear();
        buf.position(pos);
        buf.limit(pos + size);
        ByteBuffer res = buf.slice();
        res.order(ByteOrder.nativeOrder());
        buf.clear();
        buf.position(origPos);
        buf.limit(origLim);
        return res;
    }
}

Related Tutorials