Java Byte Array Copy copyByteArray(byte[] array2Copy)

Here you can find the source of copyByteArray(byte[] array2Copy)

Description

copy Byte Array

License

Apache License

Declaration

public static byte[] copyByteArray(byte[] array2Copy) 

Method Source Code

//package com.java2s;
/*/*from w  ww  .jav  a  2 s. com*/
 * Copyright 2010 sasc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static byte[] copyByteArray(byte[] array2Copy) {
        //        byte[] copy = new byte[array2Copy.length];
        //        System.arraycopy(array2Copy, 0, copy, 0, array2Copy.length);
        //        return copy;
        if (array2Copy == null) {
            //return new byte[0] instead?
            throw new IllegalArgumentException("Argument 'array2Copy' cannot be null");
        }
        return copyByteArray(array2Copy, 0, array2Copy.length);
    }

    public static byte[] copyByteArray(byte[] array2Copy, int startPos, int length) {
        if (array2Copy == null) {
            //return new byte[0] instead?
            throw new IllegalArgumentException("Argument 'array2Copy' cannot be null");
        }
        if (array2Copy.length < startPos + length) {
            throw new IllegalArgumentException("startPos(" + startPos + ")+length(" + length
                    + ") > byteArray.length(" + array2Copy.length + ")");
        }
        byte[] copy = new byte[array2Copy.length];
        System.arraycopy(array2Copy, startPos, copy, 0, length);
        return copy;
    }
}

Related

  1. copyByte(byte[] ASource, int nFrom, int nEnd)
  2. copyByteArray(byte[] source)
  3. copyByteArray(final byte[] src, byte[] dest, int length)
  4. copyBytes(byte[] arr, int length)
  5. copyBytes(byte[] dst, int dstPos, int value)