Java ByteBuffer Copy copyContentsToArray(ByteBuffer src)

Here you can find the source of copyContentsToArray(ByteBuffer src)

Description

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

License

Open Source License

Parameter

Parameter Description
src buffer to copy

Exception

Parameter Description
NullPointerException if any arguments are null

Return

new buffer with the remaining content in src

Declaration

public static byte[] copyContentsToArray(ByteBuffer src) 

Method Source Code


//package com.java2s;
/*// www .  j  a  v a2s.co  m
 * 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.
 */

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

  1. copyByteBuffer(final ByteBuffer pBuffer)
  2. copyByteBuffer(java.nio.ByteBuffer data)
  3. copyBytes(ByteBuffer bytes)
  4. copyBytes(ByteBuffer src, ByteBuffer dst, int length)
  5. copyBytesFrom(ByteBuffer bb)
  6. copyFileByMappedByteBuffer(String srcFileName, String dstFileName)
  7. copyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset, int length)
  8. copyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset, int length)
  9. copyFromStreamToBuffer(ByteBuffer out, DataInputStream in, int length)