Java Byte Array Add addBytes(byte[] array, byte[] value)

Here you can find the source of addBytes(byte[] array, byte[] value)

Description

add Bytes

License

Apache License

Declaration

public static byte[] addBytes(byte[] array, byte[] value) 

Method Source Code

//package com.java2s;
/*/*from   ww  w  . jav a 2s  .com*/
 * Copyright 2004,2004 The Apache Software Foundation.
 * 
 * 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[] addBytes(byte[] array, byte[] value) {
        if (null != array) {
            byte newarray[] = new byte[array.length + value.length];
            System.arraycopy(array, 0, newarray, 0, array.length);
            System.arraycopy(value, 0, newarray, array.length, value.length);
            array = newarray;
        } else {
            array = value;
        }
        return array;
    }

    public static byte[] addBytes(byte[] array, byte value) {
        if (null != array) {
            byte newarray[] = new byte[array.length + 1];
            System.arraycopy(array, 0, newarray, 0, array.length);
            newarray[newarray.length - 1] = value;
            array = newarray;
        } else {
            array = new byte[1];
            array[0] = value;
        }
        return array;
    }

    public static byte[] addBytes(byte[] array, int value) {
        if (null != array) {
            byte newarray[] = new byte[array.length + 3];
            System.arraycopy(array, 0, newarray, 0, array.length);
            newarray[newarray.length - 3] = (byte) ((value >> 16) & 0xFF);
            newarray[newarray.length - 2] = (byte) ((value >> 8) & 0xFF);
            newarray[newarray.length - 1] = (byte) (value & 0xFF);
            array = newarray;
        } else {
            array = new byte[3];
            array[0] = (byte) ((value >> 16) & 0xFF);
            array[1] = (byte) ((value >> 8) & 0xFF);
            array[2] = (byte) (value & 0xFF);
        }
        return array;
    }

    public static byte[] addBytes(byte[] array, long value) {
        if (null != array) {
            byte newarray[] = new byte[array.length + 4];
            System.arraycopy(array, 0, newarray, 0, array.length);
            newarray[newarray.length - 4] = (byte) ((value >> 24) & 0xFF);
            newarray[newarray.length - 3] = (byte) ((value >> 16) & 0xFF);
            newarray[newarray.length - 2] = (byte) ((value >> 8) & 0xFF);
            newarray[newarray.length - 1] = (byte) (value & 0xFF);
            array = newarray;
        } else {
            array = new byte[4];
            array[0] = (byte) ((value >> 24) & 0xFF);
            array[1] = (byte) ((value >> 16) & 0xFF);
            array[2] = (byte) ((value >> 8) & 0xFF);
            array[3] = (byte) (value & 0xFF);
        }
        return array;
    }

    public static byte[] addBytes(byte[] array, String value) {
        if (null != value) {
            if (null != array) {
                byte newarray[] = new byte[array.length + value.length()];
                System.arraycopy(array, 0, newarray, 0, array.length);
                System.arraycopy(value.getBytes(), 0, newarray,
                        array.length, value.length());
                array = newarray;
            } else {
                array = value.getBytes();
            }
        }
        return array;
    }

    public static byte[] addBytes(byte[] array, short value) {
        if (null != array) {
            byte newarray[] = new byte[array.length + 2];
            System.arraycopy(array, 0, newarray, 0, array.length);
            newarray[newarray.length - 2] = (byte) ((value >> 8) & 0xFF);
            newarray[newarray.length - 1] = (byte) (value & 0xFF);
            array = newarray;
        } else {
            array = new byte[2];
            array[0] = (byte) ((value >> 8) & 0xFF);
            array[1] = (byte) (value & 0xFF);
        }
        return array;
    }
}

Related

  1. addByteArrays(byte[] array1, byte[] array2)
  2. addByteArrays(byte[]... arrays)
  3. addBytes(byte[] initBytes, byte[] addBytes)
  4. addBytes(byte[][] src)
  5. appendByte(byte[] bytes, byte b)
  6. appendByte(byte[] bytes, byte b)