|
| 1 | + |
| 2 | +/* |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | + * contributor license agreements. See the NOTICE file distributed with |
| 5 | + * this work for additional information regarding copyright ownership. |
| 6 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | + * (the "License"); you may not use this file except in compliance with |
| 8 | + * the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.qiniu.util; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | + |
| 23 | +/** |
| 24 | + * Converts hexadecimal Strings. |
| 25 | + * This class is thread-safe. |
| 26 | + * |
| 27 | + * @version $Id: Hex.java 1619948 2014-08-22 22:53:55Z ggregory $ |
| 28 | + * @since 1.1 |
| 29 | + */ |
| 30 | +public final class Hex { |
| 31 | + |
| 32 | + /** |
| 33 | + * Used to build output as Hex |
| 34 | + */ |
| 35 | + private static final char[] DIGITS_LOWER = |
| 36 | + {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| 37 | + |
| 38 | + /** |
| 39 | + * Used to build output as Hex |
| 40 | + */ |
| 41 | + private static final char[] DIGITS_UPPER = |
| 42 | + {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
| 43 | + |
| 44 | + private Hex() { |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The |
| 49 | + * returned array will be half the length of the passed array, as it takes two characters to represent any given |
| 50 | + * byte. An exception is thrown if the passed char array has an odd number of elements. |
| 51 | + * |
| 52 | + * @param data An array of characters containing hexadecimal digits |
| 53 | + * @return A byte array containing binary data decoded from the supplied char array. |
| 54 | + * @throws HexDecodeException Thrown if an odd number or illegal of characters is supplied |
| 55 | + */ |
| 56 | + public static byte[] decodeHex(final char[] data) throws HexDecodeException { |
| 57 | + |
| 58 | + final int len = data.length; |
| 59 | + |
| 60 | + if ((len & 0x01) != 0) { |
| 61 | + throw new HexDecodeException("Odd number of characters."); |
| 62 | + } |
| 63 | + |
| 64 | + final byte[] out = new byte[len >> 1]; |
| 65 | + |
| 66 | + // two characters form the hex value. |
| 67 | + for (int i = 0, j = 0; j < len; i++) { |
| 68 | + int f = toDigit(data[j], j) << 4; |
| 69 | + j++; |
| 70 | + f = f | toDigit(data[j], j); |
| 71 | + j++; |
| 72 | + out[i] = (byte) (f & 0xFF); |
| 73 | + } |
| 74 | + |
| 75 | + return out; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. |
| 80 | + * The returned array will be double the length of the passed array, as it takes two characters to represent any |
| 81 | + * given byte. |
| 82 | + * |
| 83 | + * @param data a byte[] to convert to Hex characters |
| 84 | + * @return A char[] containing hexadecimal characters |
| 85 | + */ |
| 86 | + public static char[] encodeHex(final byte[] data) { |
| 87 | + return encodeHex(data, true); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. |
| 92 | + * The returned array will be double the length of the passed array, as it takes two characters to represent any |
| 93 | + * given byte. |
| 94 | + * |
| 95 | + * @param data a byte[] to convert to Hex characters |
| 96 | + * @param toLowerCase <code>true</code> converts to lowercase, <code>false</code> to uppercase |
| 97 | + * @return A char[] containing hexadecimal characters |
| 98 | + * @since 1.4 |
| 99 | + */ |
| 100 | + public static char[] encodeHex(final byte[] data, final boolean toLowerCase) { |
| 101 | + return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. |
| 106 | + * The returned array will be double the length of the passed array, as it takes two characters to represent any |
| 107 | + * given byte. |
| 108 | + * |
| 109 | + * @param data a byte[] to convert to Hex characters |
| 110 | + * @param toDigits the output alphabet |
| 111 | + * @return A char[] containing hexadecimal characters |
| 112 | + * @since 1.4 |
| 113 | + */ |
| 114 | + private static char[] encodeHex(final byte[] data, final char[] toDigits) { |
| 115 | + final int l = data.length; |
| 116 | + final char[] out = new char[l << 1]; |
| 117 | + // two characters form the hex value. |
| 118 | + for (int i = 0, j = 0; i < l; i++) { |
| 119 | + out[j++] = toDigits[(0xF0 & data[i]) >>> 4]; |
| 120 | + out[j++] = toDigits[0x0F & data[i]]; |
| 121 | + } |
| 122 | + return out; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned |
| 127 | + * String will be double the length of the passed array, as it takes two characters to represent any given byte. |
| 128 | + * |
| 129 | + * @param data a byte[] to convert to Hex characters |
| 130 | + * @return A String containing hexadecimal characters |
| 131 | + * @since 1.4 |
| 132 | + */ |
| 133 | + public static String encodeHexString(final byte[] data) { |
| 134 | + return new String(encodeHex(data)); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Converts a hexadecimal character to an integer. |
| 139 | + * |
| 140 | + * @param ch A character to convert to an integer digit |
| 141 | + * @param index The index of the character in the source |
| 142 | + * @return An integer |
| 143 | + * @throws HexDecodeException Thrown if ch is an illegal hex character |
| 144 | + */ |
| 145 | + protected static int toDigit(final char ch, final int index) throws HexDecodeException { |
| 146 | + final int digit = Character.digit(ch, 16); |
| 147 | + if (digit == -1) { |
| 148 | + throw new HexDecodeException("Illegal hexadecimal character " + ch + " at index " + index); |
| 149 | + } |
| 150 | + return digit; |
| 151 | + } |
| 152 | + |
| 153 | + public static class HexDecodeException extends IOException { |
| 154 | + public HexDecodeException(String msg) { |
| 155 | + super(msg); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
0 commit comments