Copy the remaining content of a ByteBuffer in to a new array. - Java java.nio

Java examples for java.nio:ByteBuffer Copy

Description

Copy the remaining content of a ByteBuffer in to a new array.

Demo Code

/*/*from ww  w.  j  a va2  s. com*/
 * Copyright (c) 2013, Kasra Faghihi, All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 */
//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    /**
     * Copy the remaining content of a {@link ByteBuffer} in to a new array. Equivalent to calling
     * {@code copyContentsToArray(src, true)}.
     * @param src buffer to copy
     * @return new buffer with the remaining content in {@code src}
     * @throws NullPointerException if any arguments are {@code null}
     */
    public static byte[] copyContentsToArray(ByteBuffer src) {
        return copyContentsToArray(src, true);
    }

    /**
     * Copy the remaining content of a {@link ByteBuffer} in to a new array.
     * @param src buffer to copy
     * @param incrementSrc of {@code true} increments {@code src}'s position
     * @return new buffer with the remaining content in {@code src}
     * @throws NullPointerException if any arguments are {@code null}
     */
    public static byte[] copyContentsToArray(ByteBuffer src,
            boolean incrementSrc) {
        if (!incrementSrc) {
            src.mark();
        }

        ByteBuffer dst = ByteBuffer.allocate(src.remaining());
        dst.put(src);

        if (!incrementSrc) {
            src.reset();
        }

        return dst.array();
    }
}

Related Tutorials