View Javadoc

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 org.apache.geronimo.util.encoders;
20  
21  /**
22   * Converters for going from hex to binary and back. Note: this class assumes ASCII processing.
23   */
24  public class HexTranslator
25      implements Translator
26  {
27      private static final byte[]   hexTable =
28          {
29              (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
30              (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
31          };
32  
33      /**
34       * size of the output block on encoding produced by getDecodedBlockSize()
35       * bytes.
36       */
37      public int getEncodedBlockSize()
38      {
39          return 2;
40      }
41  
42      public int encode(
43          byte[]  in,
44          int     inOff,
45          int     length,
46          byte[]  out,
47          int     outOff)
48      {
49          for (int i = 0, j = 0; i < length; i++, j += 2)
50          {
51              out[outOff + j] = hexTable[(in[inOff] >> 4) & 0x0f];
52              out[outOff + j + 1] = hexTable[in[inOff] & 0x0f];
53  
54              inOff++;
55          }
56  
57          return length * 2;
58      }
59  
60      /**
61       * size of the output block on decoding produced by getEncodedBlockSize()
62       * bytes.
63       */
64      public int getDecodedBlockSize()
65      {
66          return 1;
67      }
68  
69      public int decode(
70          byte[]  in,
71          int     inOff,
72          int     length,
73          byte[]  out,
74          int     outOff)
75      {
76          int halfLength = length / 2;
77          byte left, right;
78          for (int i = 0; i < halfLength; i++)
79          {
80              left  = in[inOff + i * 2];
81              right = in[inOff + i * 2 + 1];
82  
83              if (left < (byte)'a')
84              {
85                  out[outOff] = (byte)((left - '0') << 4);
86              }
87              else
88              {
89                  out[outOff] = (byte)((left - 'a' + 10) << 4);
90              }
91              if (right < (byte)'a')
92              {
93                  out[outOff] += (byte)(right - '0');
94              }
95              else
96              {
97                  out[outOff] += (byte)(right - 'a' + 10);
98              }
99  
100             outOff++;
101         }
102 
103         return halfLength;
104     }
105 }