Java Byte Array Add appendByte(byte[] bytes, byte b)

Here you can find the source of appendByte(byte[] bytes, byte b)

Description

Creates a copy of bytes and appends b to the end of it

License

Apache License

Declaration

public static byte[] appendByte(byte[] bytes, byte b) 

Method Source Code

//package com.java2s;
/*//  w w  w  .j av a2 s  .  c  o m
 * Copyright 2011 Google Inc.
 * Copyright 2014 Andreas Schildbach
 *
 * 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.
 */

import java.util.*;

public class Main {
    /**
     * Creates a copy of bytes and appends b to the end of it
     */
    public static byte[] appendByte(byte[] bytes, byte b) {
        byte[] result = Arrays.copyOf(bytes, bytes.length + 1);
        result[result.length - 1] = b;
        return result;
    }

    public static byte[] copyOf(byte[] in, int length) {
        byte[] out = new byte[length];
        System.arraycopy(in, 0, out, 0, Math.min(length, in.length));
        return out;
    }
}

Related

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