001package com.nimbusds.jose.util;
002
003
004import java.io.ByteArrayOutputStream;
005import java.io.IOException;
006
007
008/**
009 * Byte utilities.
010 *
011 * @author Vladimir Dzhuvinov
012 * @version 2015-05-12
013 */
014public class ByteUtils {
015
016
017        /**
018         * Concatenates the specified byte arrays.
019         *
020         * @param byteArrays The byte arrays to concatenate, may be
021         *                   {@code null}.
022         *
023         * @return The resulting byte array.
024         */
025        public static byte[] concat(byte[]... byteArrays) {
026
027                try {
028                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
029
030                        for (byte[] bytes : byteArrays) {
031
032                                if (bytes == null) {
033                                        continue; // skip
034                                }
035
036                                baos.write(bytes);
037                        }
038                        return baos.toByteArray();
039
040                } catch (IOException e) {
041                        // Should never happen
042                        throw new IllegalStateException(e.getMessage(), e);
043                }
044        }
045
046
047        /**
048         * Returns a portion of the specified byte array.
049         *
050         * @param byteArray  The byte array. Must not be {@code null}.
051         * @param beginIndex The beginning index, inclusive. Must be zero or
052         *                   positive.
053         * @param length     The length. Must be zero or positive.
054         *
055         * @return The byte array portion.
056         */
057        public static byte[] subArray(byte[] byteArray, int beginIndex, int length) {
058
059                byte[] subArray = new byte[length];
060                System.arraycopy(byteArray, beginIndex, subArray, 0, subArray.length);
061                return subArray;
062        }
063
064
065        /**
066         * Returns the bit length of the specified byte length.
067         *
068         * @param byteLength The byte length.
069         *
070         * @return The bit length.
071         */
072        public static int bitLength(final int byteLength) {
073
074                return byteLength * 8;
075        }
076
077
078        /**
079         * Returns the byte length of the specified byte array.
080         *
081         * @param byteArray The byte array. May be {@code null}.
082         *
083         * @return The bite length, zero if the array is {@code null}.
084         */
085        public static int bitLength(final byte[] byteArray) {
086
087                if (byteArray == null) {
088                        return 0;
089                } else {
090                        return bitLength(byteArray.length);
091                }
092        }
093
094
095        /**
096         * Returns the byte length of the specified bit length.
097         *
098         * @param bitLength The bit length.
099         *
100         * @return The byte byte length.
101         */
102        public static int byteLength(final int bitLength) {
103
104                return bitLength / 8;
105        }
106}