|
1 | | -/* ==================================================================== |
2 | | - * The Apache Software License, Version 1.1 |
3 | | - * |
4 | | - * Copyright (c) 2003 The Apache Software Foundation. All rights |
5 | | - * reserved. |
6 | | - * |
7 | | - * Redistribution and use in source and binary forms, with or without |
8 | | - * modification, are permitted provided that the following conditions |
9 | | - * are met: |
10 | | - * |
11 | | - * 1. Redistributions of source code must retain the above copyright |
12 | | - * notice, this list of conditions and the following disclaimer. |
13 | | - * |
14 | | - * 2. Redistributions in binary form must reproduce the above copyright |
15 | | - * notice, this list of conditions and the following disclaimer in |
16 | | - * the documentation and/or other materials provided with the |
17 | | - * distribution. |
18 | | - * |
19 | | - * 3. The end-user documentation included with the redistribution, |
20 | | - * if any, must include the following acknowledgment: |
21 | | - * "This product includes software developed by the |
22 | | - * Apache Software Foundation (http://www.apache.org/)." |
23 | | - * Alternately, this acknowledgment may appear in the software itself, |
24 | | - * if and wherever such third-party acknowledgments normally appear. |
25 | | - * |
26 | | - * 4. The names "Apache" and "Apache Software Foundation" and |
27 | | - * "Apache Commons" must not be used to endorse or promote products |
28 | | - * derived from this software without prior written permission. For |
29 | | - * written permission, please contact apache@apache.org. |
30 | | - * |
31 | | - * 5. Products derived from this software may not be called "Apache", |
32 | | - * "Apache Commons", nor may "Apache" appear in their name, without |
33 | | - * prior written permission of the Apache Software Foundation. |
34 | | - * |
35 | | - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
36 | | - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
37 | | - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
38 | | - * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
39 | | - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | | - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | | - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
42 | | - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
43 | | - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
44 | | - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
45 | | - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
46 | | - * SUCH DAMAGE. |
47 | | - * ==================================================================== |
48 | | - * |
49 | | - * This software consists of voluntary contributions made by many |
50 | | - * individuals on behalf of the Apache Software Foundation. For more |
51 | | - * information on the Apache Software Foundation, please see |
52 | | - * <http://www.apache.org/>. |
53 | | - */ |
54 | | - |
55 | | -package org.apache.commons.codec.binary; |
56 | | - |
57 | | -/** |
58 | | - * Hex encoder/decoder |
59 | | - * |
60 | | - * @author <a href="mailto:siege@preoccupied.net">Christopher O'Brien</a> |
61 | | - * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a> |
62 | | - */ |
63 | | -public class Hex { |
64 | | - |
65 | | - |
66 | | - /** for building output as Hex */ |
67 | | - private static char[] digits = { |
68 | | - '0', '1', '2', '3', '4', '5', '6', '7', |
69 | | - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
70 | | - }; |
71 | | - |
72 | | - |
73 | | - |
74 | | - /** |
75 | | - * Converts an array of bytes into an array of characters representing the |
76 | | - * hexidecimal values of each byte in order. The returned array will be |
77 | | - * double the length of the passed array, as it takes two characters to |
78 | | - * represent any given byte. |
79 | | - * |
80 | | - * @param data array of byte to convert to Hex characters |
81 | | - * @return A char[] containing hexidecimal characters |
82 | | - */ |
83 | | - public static char[] encodeHex(byte[] data) { |
84 | | - |
85 | | - int l = data.length; |
86 | | - |
87 | | - char[] out = new char[l << 1]; |
88 | | - |
89 | | - // two characters form the hex value. |
90 | | - for (int i = 0, j = 0; i < l; i++) { |
91 | | - out[j++] = digits[(0xF0 & data[i]) >>> 4 ]; |
92 | | - out[j++] = digits[ 0x0F & data[i] ]; |
93 | | - } |
94 | | - |
95 | | - return out; |
96 | | - } |
97 | | - |
98 | | - |
99 | | - |
100 | | - /** |
101 | | - * Converts an array of characters representing hexidecimal values into an |
102 | | - * array of bytes of those same values. The returned array will be half the |
103 | | - * length of the passed array, as it takes two characters to represent any |
104 | | - * given byte. An exception is thrown if the passed char array has an odd |
105 | | - * number of elements. |
106 | | - * |
107 | | - * @param data An array of characters containing hexidecimal digits |
108 | | - * @return A byte array array containing binary data decoded from |
109 | | - * the supplied char array. |
110 | | - * @throws Exception Thrown if an odd number of characters is supplied |
111 | | - * to this function |
112 | | - */ |
113 | | - public static byte[] decodeHex(char[] data) throws Exception { |
114 | | - |
115 | | - int l = data.length; |
116 | | - |
117 | | - if ((l & 0x01) != 0) { |
118 | | - throw new Exception("odd number of characters."); |
119 | | - } |
120 | | - |
121 | | - byte[] out = new byte[l >> 1]; |
122 | | - |
123 | | - // two characters form the hex value. |
124 | | - for (int i = 0, j = 0; j < l; i++) { |
125 | | - int f = Character.digit(data[j++], 16) << 4; |
126 | | - f = f | Character.digit(data[j++], 16); |
127 | | - out[i] = (byte) (f & 0xFF); |
128 | | - } |
129 | | - |
130 | | - return out; |
131 | | - } |
132 | | - |
133 | | -} |
134 | | - |
| 1 | +/* ==================================================================== |
| 2 | + * The Apache Software License, Version 1.1 |
| 3 | + * |
| 4 | + * Copyright (c) 2003 The Apache Software Foundation. All rights |
| 5 | + * reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer in |
| 16 | + * the documentation and/or other materials provided with the |
| 17 | + * distribution. |
| 18 | + * |
| 19 | + * 3. The end-user documentation included with the redistribution, |
| 20 | + * if any, must include the following acknowledgment: |
| 21 | + * "This product includes software developed by the |
| 22 | + * Apache Software Foundation (http://www.apache.org/)." |
| 23 | + * Alternately, this acknowledgment may appear in the software itself, |
| 24 | + * if and wherever such third-party acknowledgments normally appear. |
| 25 | + * |
| 26 | + * 4. The names "Apache" and "Apache Software Foundation" and |
| 27 | + * "Apache Commons" must not be used to endorse or promote products |
| 28 | + * derived from this software without prior written permission. For |
| 29 | + * written permission, please contact apache@apache.org. |
| 30 | + * |
| 31 | + * 5. Products derived from this software may not be called "Apache", |
| 32 | + * "Apache Commons", nor may "Apache" appear in their name, without |
| 33 | + * prior written permission of the Apache Software Foundation. |
| 34 | + * |
| 35 | + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 36 | + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 37 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 38 | + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| 39 | + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 42 | + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 43 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 44 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 45 | + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 46 | + * SUCH DAMAGE. |
| 47 | + * ==================================================================== |
| 48 | + * |
| 49 | + * This software consists of voluntary contributions made by many |
| 50 | + * individuals on behalf of the Apache Software Foundation. For more |
| 51 | + * information on the Apache Software Foundation, please see |
| 52 | + * <http://www.apache.org/>. |
| 53 | + */ |
| 54 | + |
| 55 | +package org.apache.commons.codec.binary; |
| 56 | + |
| 57 | +import org.apache.commons.codec.BinaryDecoder; |
| 58 | +import org.apache.commons.codec.BinaryEncoder; |
| 59 | +import org.apache.commons.codec.DecoderException; |
| 60 | +import org.apache.commons.codec.EncoderException; |
| 61 | + |
| 62 | +/** |
| 63 | + * Hex encoder/decoder |
| 64 | + * |
| 65 | + * @author <a href="mailto:siege@preoccupied.net">Christopher O'Brien</a> |
| 66 | + * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a> |
| 67 | + */ |
| 68 | +public class Hex implements BinaryEncoder, BinaryDecoder { |
| 69 | + |
| 70 | + |
| 71 | + /** for building output as Hex */ |
| 72 | + private static char[] digits = { |
| 73 | + '0', '1', '2', '3', '4', '5', '6', '7', |
| 74 | + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| 75 | + }; |
| 76 | + |
| 77 | + public Object encode(Object pObject) throws EncoderException { |
| 78 | + if(pObject instanceof String) { |
| 79 | + pObject = ((String) pObject).getBytes(); |
| 80 | + } |
| 81 | + |
| 82 | + try { |
| 83 | + return encodeHex((byte[]) pObject); |
| 84 | + } catch(Exception e) { |
| 85 | + throw new EncoderException(e.getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public byte[] encode(byte[] pArray) { |
| 90 | + return new String(encodeHex(pArray)).getBytes(); |
| 91 | + } |
| 92 | + |
| 93 | + public Object decode(Object pObject) throws DecoderException { |
| 94 | + if(pObject instanceof String) { |
| 95 | + pObject = ((String) pObject).getBytes(); |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + return decodeHex((char[]) pObject); |
| 100 | + } catch(Exception e) { |
| 101 | + throw new DecoderException(e.getMessage()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + public byte[] decode(byte[] pArray) throws DecoderException { |
| 108 | + return decodeHex(new String(pArray).toCharArray()); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Converts an array of bytes into an array of characters representing the |
| 113 | + * hexidecimal values of each byte in order. The returned array will be |
| 114 | + * double the length of the passed array, as it takes two characters to |
| 115 | + * represent any given byte. |
| 116 | + * |
| 117 | + * @param data array of byte to convert to Hex characters |
| 118 | + * @return A char[] containing hexidecimal characters |
| 119 | + */ |
| 120 | + public static char[] encodeHex(byte[] data) { |
| 121 | + |
| 122 | + int l = data.length; |
| 123 | + |
| 124 | + char[] out = new char[l << 1]; |
| 125 | + |
| 126 | + // two characters form the hex value. |
| 127 | + for (int i = 0, j = 0; i < l; i++) { |
| 128 | + out[j++] = digits[(0xF0 & data[i]) >>> 4 ]; |
| 129 | + out[j++] = digits[ 0x0F & data[i] ]; |
| 130 | + } |
| 131 | + |
| 132 | + return out; |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + /** |
| 138 | + * Converts an array of characters representing hexidecimal values into an |
| 139 | + * array of bytes of those same values. The returned array will be half the |
| 140 | + * length of the passed array, as it takes two characters to represent any |
| 141 | + * given byte. An exception is thrown if the passed char array has an odd |
| 142 | + * number of elements. |
| 143 | + * |
| 144 | + * @param data An array of characters containing hexidecimal digits |
| 145 | + * @return A byte array array containing binary data decoded from |
| 146 | + * the supplied char array. |
| 147 | + * @throws Exception Thrown if an odd number of characters is supplied |
| 148 | + * to this function |
| 149 | + */ |
| 150 | + public static byte[] decodeHex(char[] data) throws DecoderException { |
| 151 | + |
| 152 | + int l = data.length; |
| 153 | + |
| 154 | + if ((l & 0x01) != 0) { |
| 155 | + throw new DecoderException("odd number of characters."); |
| 156 | + } |
| 157 | + |
| 158 | + byte[] out = new byte[l >> 1]; |
| 159 | + |
| 160 | + // two characters form the hex value. |
| 161 | + for (int i = 0, j = 0; j < l; i++) { |
| 162 | + int f = Character.digit(data[j++], 16) << 4; |
| 163 | + f = f | Character.digit(data[j++], 16); |
| 164 | + out[i] = (byte) (f & 0xFF); |
| 165 | + } |
| 166 | + |
| 167 | + return out; |
| 168 | + } |
| 169 | + |
| 170 | +} |
| 171 | + |
0 commit comments