View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  
29  package org.apache.hc.client5.http.utils;
30  
31  import org.apache.hc.core5.annotation.Internal;
32  
33  @Internal
34  public class Hex {
35  
36      private Hex() {
37      }
38  
39      public static String encodeHexString(final byte[] bytes) {
40  
41          final char[] out = new char[bytes.length * 2];
42  
43          encodeHex(bytes, 0, bytes.length, DIGITS_LOWER, out, 0);
44          return new String(out);
45      }
46  
47      //
48      // The following comes from commons-codec
49      // https://github.com/apache/commons-codec/blob/master/src/main/java/org/apache/commons/codec/binary/Hex.java
50  
51      /**
52       * Used to build output as hex.
53       */
54  
55      private static final char[] DIGITS_LOWER = {
56              '0', '1', '2', '3', '4', '5', '6', '7',
57              '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
58      };
59  
60      /**
61       * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
62       *
63       * @param data       a byte[] to convert to hex characters
64       * @param dataOffset the position in {@code data} to start encoding from
65       * @param dataLen    the number of bytes from {@code dataOffset} to encode
66       * @param toDigits   the output alphabet (must contain at least 16 chars)
67       * @param out        a char[] which will hold the resultant appropriate characters from the alphabet.
68       * @param outOffset  the position within {@code out} at which to start writing the encoded characters.
69       */
70      private static void encodeHex(final byte[] data, final int dataOffset, final int dataLen, final char[] toDigits,
71                                    final char[] out, final int outOffset) {
72          // two characters form the hex value.
73          for (int i = dataOffset, j = outOffset; i < dataOffset + dataLen; i++) {
74              out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
75              out[j++] = toDigits[0x0F & data[i]];
76          }
77      }
78  
79      /*
80  
81         // Can be replaced in Java 17 with the following:
82  
83  
84      private static final java.util.HexFormat HEX_FORMAT = HexFormat.of();
85  
86      public static String encodeHex(byte[] bytes) {
87          return HEX_FORMAT.formatHex(bytes);
88      }
89  
90  
91       */
92  
93  
94  }