View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.tika.mime;
18  
19  /**
20   * 
21   * A set of Hex encoding and decoding utility methods.
22   * 
23   */
24  public class HexCoDec {
25  
26      private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5',
27              '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
28  
29      /**
30       * Decode a hex string
31       * 
32       * @param hexValue
33       *            the string of hex characters
34       * @return the decode hex string as bytes.
35       */
36      public static byte[] decode(String hexValue) {
37          return decode(hexValue.toCharArray());
38      }
39  
40      /**
41       * Decode an array of hex chars
42       * 
43       * @param hexChars
44       *            an array of hex characters.
45       * @return the decode hex chars as bytes.
46       */
47      public static byte[] decode(char[] hexChars) {
48          return decode(hexChars, 0, hexChars.length);
49      }
50  
51      /**
52       * Decode an array of hex chars.
53       * 
54       * @param hexChars
55       *            an array of hex characters.
56       * @param startIndex
57       *            the index of the first character to decode
58       * @param length
59       *            the number of characters to decode.
60       * @return the decode hex chars as bytes.
61       */
62      public static byte[] decode(char[] hexChars, int startIndex, int length) {
63          if ((length & 1) != 0)
64              throw new IllegalArgumentException("Length must be even");
65  
66          byte[] result = new byte[length / 2];
67          for (int j = 0; j < result.length; j++) {
68              result[j] = (byte) (hexCharToNibble(hexChars[startIndex++]) * 16 + hexCharToNibble(hexChars[startIndex++]));
69          }
70          return result;
71      }
72  
73      /**
74       * Hex encode an array of bytes
75       * 
76       * @param bites
77       *            the array of bytes to encode.
78       * @return the array of hex characters.
79       */
80      public static char[] encode(byte[] bites) {
81          return encode(bites, 0, bites.length);
82      }
83  
84      /**
85       * Hex encode an array of bytes
86       * 
87       * @param bites
88       *            the array of bytes to encode.
89       * @param startIndex
90       *            the index of the first character to encode.
91       * @param length
92       *            the number of characters to encode.
93       * @return the array of hex characters.
94       */
95      public static char[] encode(byte[] bites, int startIndex, int length) {
96          char[] result = new char[length * 2];
97          for (int i = 0, j = 0; i < length; i++) {
98              int bite = bites[startIndex++] & 0xff;
99              result[j++] = HEX_CHARS[bite >> 4];
100             result[j++] = HEX_CHARS[bite & 0xf];
101         }
102         return result;
103     }
104 
105     /**
106      * Internal method to turn a hex char into a nibble.
107      */
108     private static int hexCharToNibble(char ch) {
109         if ((ch >= '0') && (ch <= '9')) {
110             return ch - '0';
111         } else if ((ch >= 'a') && (ch <= 'f')) {
112             return ch - 'a' + 10;
113         } else if ((ch >= 'A') && (ch <= 'F')) {
114             return ch - 'A' + 10;
115         } else {
116             throw new IllegalArgumentException("Not a hex char - '" + ch + "'");
117         }
118     }
119 
120 }