KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > encoders > HexTranslator


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
18 package org.apache.geronimo.util.encoders;
19
20 /**
21  * Converters for going from hex to binary and back. Note: this class assumes ASCII processing.
22  */

23 public class HexTranslator
24     implements Translator
25 {
26     private static final byte[] hexTable =
27         {
28             (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
29             (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
30         };
31
32     /**
33      * size of the output block on encoding produced by getDecodedBlockSize()
34      * bytes.
35      */

36     public int getEncodedBlockSize()
37     {
38         return 2;
39     }
40
41     public int encode(
42         byte[] in,
43         int inOff,
44         int length,
45         byte[] out,
46         int outOff)
47     {
48         for (int i = 0, j = 0; i < length; i++, j += 2)
49         {
50             out[outOff + j] = hexTable[(in[inOff] >> 4) & 0x0f];
51             out[outOff + j + 1] = hexTable[in[inOff] & 0x0f];
52
53             inOff++;
54         }
55
56         return length * 2;
57     }
58
59     /**
60      * size of the output block on decoding produced by getEncodedBlockSize()
61      * bytes.
62      */

63     public int getDecodedBlockSize()
64     {
65         return 1;
66     }
67
68     public int decode(
69         byte[] in,
70         int inOff,
71         int length,
72         byte[] out,
73         int outOff)
74     {
75         int halfLength = length / 2;
76         byte left, right;
77         for (int i = 0; i < halfLength; i++)
78         {
79             left = in[inOff + i * 2];
80             right = in[inOff + i * 2 + 1];
81
82             if (left < (byte)'a')
83             {
84                 out[outOff] = (byte)((left - '0') << 4);
85             }
86             else
87             {
88                 out[outOff] = (byte)((left - 'a' + 10) << 4);
89             }
90             if (right < (byte)'a')
91             {
92                 out[outOff] += (byte)(right - '0');
93             }
94             else
95             {
96                 out[outOff] += (byte)(right - 'a' + 10);
97             }
98
99             outOff++;
100         }
101
102         return halfLength;
103     }
104 }
105
Popular Tags