ByteBuffer to Bytes - Java java.nio

Java examples for java.nio:ByteBuffer

Description

ByteBuffer to Bytes

Demo Code


//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    public final static byte[] toBytes(final ByteBuffer bb) {
        return toBytes(bb, bb.limit());
    }/*  ww  w  . j av  a  2  s .  c om*/

    public final static byte[] toBytes(final ByteBuffer bb, int len) {
        return toBytes(bb, 0, len);
    }

    /**
     *
     * @param bb
     * @param offset
     * @param len
     *            relative
     * @return
     */
    public final static byte[] toBytes(final ByteBuffer bb, int offset,
            int len) {
        final byte[] result = new byte[len];
        System.arraycopy(bb.array(), offset, result, 0, len);
        return result;
    }
}

Related Tutorials