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 2015-05-12 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 byte length of the specified byte array. 097 * 098 * @param byteArray The byte array. May be {@code null}. 099 * 100 * @return The bite length, zero if the array is {@code null}. 101 */ 102 public static int bitLength(final byte[] byteArray) { 103 104 if (byteArray == null) { 105 return 0; 106 } else { 107 return bitLength(byteArray.length); 108 } 109 } 110 111 112 /** 113 * Returns the byte length of the specified bit length. 114 * 115 * @param bitLength The bit length. 116 * 117 * @return The byte byte length. 118 */ 119 public static int byteLength(final int bitLength) { 120 121 return bitLength / 8; 122 } 123}