KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > algorithm > HexadecimalEncodingAlgorithm


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * 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, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.algorithm;
41
42 import java.io.EOFException JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.OutputStream JavaDoc;
46 import java.nio.CharBuffer JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.List JavaDoc;
49 import org.jvnet.fastinfoset.EncodingAlgorithmException;
50 import com.sun.xml.fastinfoset.CommonResourceBundle;
51
52 public class HexadecimalEncodingAlgorithm extends BuiltInEncodingAlgorithm {
53     private static final char NIBBLE_TO_HEXADECIMAL_TABLE[] =
54         { '0','1','2','3','4','5','6','7',
55             '8','9','A','B','B','D','E','F' };
56     
57     private static final int HEXADECIMAL_TO_NIBBLE_TABLE[] = {
58         /*'0'*/ 0,
59         /*'1'*/ 1,
60         /*'2'*/ 2,
61         /*'3'*/ 3,
62         /*'4'*/ 4,
63         /*'5'*/ 5,
64         /*'6'*/ 6,
65         /*'7'*/ 7,
66         /*'8'*/ 8,
67         /*'9'*/ 9, -1, -1, -1, -1, -1, -1, -1,
68         /*'A'*/ 10,
69         /*'B'*/ 11,
70         /*'C'*/ 12,
71         /*'D'*/ 13,
72         /*'E'*/ 14,
73         /*'F'*/ 15,
74         /*'G'-'Z'*/-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
75         /*'[' - '`'*/ -1, -1, -1, -1, -1, -1,
76         /*'a'*/ 10,
77         /*'b'*/ 11,
78         /*'c'*/ 12,
79         /*'d'*/ 13,
80         /*'e'*/ 14,
81         /*'f'*/ 15 };
82
83     public final Object JavaDoc decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
84         final byte[] data = new byte[length];
85         System.arraycopy(b, start, data, 0, length);
86         return data;
87     }
88     
89     public final Object JavaDoc decodeFromInputStream(InputStream JavaDoc s) throws IOException JavaDoc {
90         throw new UnsupportedOperationException JavaDoc(CommonResourceBundle.getInstance().getString("message.notImplemented"));
91     }
92     
93     
94     public void encodeToOutputStream(Object JavaDoc data, OutputStream JavaDoc s) throws IOException JavaDoc {
95         if (!(data instanceof byte[])) {
96             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.dataNotByteArray"));
97         }
98         
99         s.write((byte[])data);
100     }
101     
102     public final Object JavaDoc convertFromCharacters(char[] ch, int start, int length) {
103         if (length == 0) {
104             return new byte[0];
105         }
106         
107         StringBuffer JavaDoc encodedValue = removeWhitespace(ch, start, length);
108         int encodedLength = encodedValue.length();
109         if (encodedLength == 0) {
110             return new byte[0];
111         }
112         
113         int valueLength = encodedValue.length() / 2;
114         byte[] value = new byte[valueLength];
115
116         int encodedIdx = 0;
117         for (int i = 0; i < valueLength; ++i) {
118             int nibble1 = HEXADECIMAL_TO_NIBBLE_TABLE[encodedValue.charAt(encodedIdx++) - '0'];
119             int nibble2 = HEXADECIMAL_TO_NIBBLE_TABLE[encodedValue.charAt(encodedIdx++) - '0'];
120             value[i] = (byte) ((nibble1 << 4) | nibble2);
121         }
122
123         return value;
124     }
125     
126     public final void convertToCharacters(Object JavaDoc data, StringBuffer JavaDoc s) {
127         if (data == null) {
128             return;
129         }
130         final byte[] value = (byte[]) data;
131         if (value.length == 0) {
132             return;
133         }
134
135         s.ensureCapacity(value.length * 2);
136         for (int i = 0; i < value.length; ++i) {
137             s.append(NIBBLE_TO_HEXADECIMAL_TABLE[(value[i] >>> 4) & 0xf]);
138             s.append(NIBBLE_TO_HEXADECIMAL_TABLE[value[i] & 0xf]);
139         }
140     }
141     
142     
143         
144     public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
145         return octetLength * 2;
146     }
147
148     public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
149         return primitiveLength / 2;
150     }
151     
152     public final void encodeToBytes(Object JavaDoc array, int astart, int alength, byte[] b, int start) {
153         System.arraycopy((byte[])array, astart, b, start, alength);
154     }
155 }
156
Popular Tags