001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.util;
019
020
021import java.io.ByteArrayOutputStream;
022import java.io.IOException;
023
024
025/**
026 * Byte utilities.
027 *
028 * @author Vladimir Dzhuvinov
029 * @version 2017-06-01
030 */
031public class ByteUtils {
032
033
034        /**
035         * Concatenates the specified byte arrays.
036         *
037         * @param byteArrays The byte arrays to concatenate, may be
038         *                   {@code null}.
039         *
040         * @return The resulting byte array.
041         */
042        public static byte[] concat(byte[]... byteArrays) {
043
044                try {
045                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
046
047                        for (byte[] bytes : byteArrays) {
048
049                                if (bytes == null) {
050                                        continue; // skip
051                                }
052
053                                baos.write(bytes);
054                        }
055                        return baos.toByteArray();
056
057                } catch (IOException e) {
058                        // Should never happen
059                        throw new IllegalStateException(e.getMessage(), e);
060                }
061        }
062
063
064        /**
065         * Returns a portion of the specified byte array.
066         *
067         * @param byteArray  The byte array. Must not be {@code null}.
068         * @param beginIndex The beginning index, inclusive. Must be zero or
069         *                   positive.
070         * @param length     The length. Must be zero or positive.
071         *
072         * @return The byte array portion.
073         */
074        public static byte[] subArray(byte[] byteArray, int beginIndex, int length) {
075
076                byte[] subArray = new byte[length];
077                System.arraycopy(byteArray, beginIndex, subArray, 0, subArray.length);
078                return subArray;
079        }
080
081
082        /**
083         * Returns the bit length of the specified byte length.
084         *
085         * @param byteLength The byte length.
086         *
087         * @return The bit length.
088         */
089        public static int bitLength(final int byteLength) {
090
091                return byteLength * 8;
092        }
093
094
095        /**
096         * Returns the bit length of the specified byte length, preventing
097         * integer overflow.
098         *
099         * @param byteLength The byte length.
100         *
101         * @return The bit length.
102         *
103         * @throws IntegerOverflowException On a integer overflow.
104         */
105        public static int safeBitLength(final int byteLength)
106                throws IntegerOverflowException {
107                
108                long longResult = (long)byteLength * (long)8;
109                if((long)((int)longResult) != longResult) {
110                        throw new IntegerOverflowException();
111                } else {
112                        return (int)longResult;
113                }
114        }
115
116
117        /**
118         * Returns the byte length of the specified byte array.
119         *
120         * @param byteArray The byte array. May be {@code null}.
121         *
122         * @return The bite length, zero if the array is {@code null}.
123         */
124        public static int bitLength(final byte[] byteArray) {
125
126                if (byteArray == null) {
127                        return 0;
128                } else {
129                        return bitLength(byteArray.length);
130                }
131        }
132
133
134        /**
135         * Returns the byte length of the specified byte array, preventing
136         * integer overflow.
137         *
138         * @param byteArray The byte array. May be {@code null}.
139         *
140         * @return The bite length, zero if the array is {@code null}.
141         *
142         * @throws IntegerOverflowException On a integer overflow.
143         */
144        public static int safeBitLength(final byte[] byteArray)
145                throws IntegerOverflowException {
146
147                if (byteArray == null) {
148                        return 0;
149                } else {
150                        return safeBitLength(byteArray.length);
151                }
152        }
153
154
155        /**
156         * Returns the byte length of the specified bit length.
157         *
158         * @param bitLength The bit length.
159         *
160         * @return The byte byte length.
161         */
162        public static int byteLength(final int bitLength) {
163
164                return bitLength / 8;
165        }
166}