Java ByteBuffer Set arrayCopy(ByteBuffer buffer, int position, byte[] bytes, int offset, int length)

Here you can find the source of arrayCopy(ByteBuffer buffer, int position, byte[] bytes, int offset, int length)

Description

array Copy

License

Apache License

Declaration

public static void arrayCopy(ByteBuffer buffer, int position, byte[] bytes, int offset, int length) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

public class Main {
    public static void arrayCopy(ByteBuffer buffer, int position, byte[] bytes, int offset, int length) {
        if (buffer.hasArray())
            System.arraycopy(buffer.array(), buffer.arrayOffset() + position, bytes, offset, length);
        else/*  w w w  .j  a  va 2 s .  co m*/
            ((ByteBuffer) buffer.duplicate().position(position)).get(bytes, offset, length);
    }

    /**
    * Transfer bytes from one ByteBuffer to another.
    * This function acts as System.arrayCopy() but for ByteBuffers.
    *
    * @param src the source ByteBuffer
    * @param srcPos starting position in the source ByteBuffer
    * @param dst the destination ByteBuffer
    * @param dstPos starting position in the destination ByteBuffer
    * @param length the number of bytes to copy
    */
    public static void arrayCopy(ByteBuffer src, int srcPos, ByteBuffer dst, int dstPos, int length) {
        if (src.hasArray() && dst.hasArray()) {
            System.arraycopy(src.array(), src.arrayOffset() + srcPos, dst.array(), dst.arrayOffset() + dstPos,
                    length);
        } else {
            if (src.limit() - srcPos < length || dst.limit() - dstPos < length)
                throw new IndexOutOfBoundsException();

            for (int i = 0; i < length; i++)
                // TODO: ByteBuffer.put is polymorphic, and might be slow here
                dst.put(dstPos++, src.get(srcPos++));
        }
    }
}

Related

  1. base64FormatOfBinarySet(Collection bs)
  2. blockCopy(ByteBuffer source, int srcOffset, byte[] dest, int destOffset, int count)
  3. byteBuffersToString(ByteBuffer[] bufs, Charset cs)
  4. byteIndexOf(ByteBuffer buffer, byte[] temp, int offset)