001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.algorithm;
040:
041: import java.io.IOException;
042: import java.io.InputStream;
043: import java.io.OutputStream;
044: import org.jvnet.fastinfoset.EncodingAlgorithmException;
045: import com.sun.xml.fastinfoset.CommonResourceBundle;
046:
047: public class HexadecimalEncodingAlgorithm extends
048: BuiltInEncodingAlgorithm {
049: private static final char NIBBLE_TO_HEXADECIMAL_TABLE[] = { '0',
050: '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'B',
051: 'D', 'E', 'F' };
052:
053: private static final int HEXADECIMAL_TO_NIBBLE_TABLE[] = {
054: /*'0'*/0,
055: /*'1'*/1,
056: /*'2'*/2,
057: /*'3'*/3,
058: /*'4'*/4,
059: /*'5'*/5,
060: /*'6'*/6,
061: /*'7'*/7,
062: /*'8'*/8,
063: /*'9'*/9, -1, -1, -1, -1, -1, -1, -1,
064: /*'A'*/10,
065: /*'B'*/11,
066: /*'C'*/12,
067: /*'D'*/13,
068: /*'E'*/14,
069: /*'F'*/15,
070: /*'G'-'Z'*/-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
071: -1, -1, -1, -1, -1, -1,
072: /*'[' - '`'*/-1, -1, -1, -1, -1, -1,
073: /*'a'*/10,
074: /*'b'*/11,
075: /*'c'*/12,
076: /*'d'*/13,
077: /*'e'*/14,
078: /*'f'*/15 };
079:
080: public final Object decodeFromBytes(byte[] b, int start, int length)
081: throws EncodingAlgorithmException {
082: final byte[] data = new byte[length];
083: System.arraycopy(b, start, data, 0, length);
084: return data;
085: }
086:
087: public final Object decodeFromInputStream(InputStream s)
088: throws IOException {
089: throw new UnsupportedOperationException(CommonResourceBundle
090: .getInstance().getString("message.notImplemented"));
091: }
092:
093: public void encodeToOutputStream(Object data, OutputStream s)
094: throws IOException {
095: if (!(data instanceof byte[])) {
096: throw new IllegalArgumentException(CommonResourceBundle
097: .getInstance()
098: .getString("message.dataNotByteArray"));
099: }
100:
101: s.write((byte[]) data);
102: }
103:
104: public final Object convertFromCharacters(char[] ch, int start,
105: int length) {
106: if (length == 0) {
107: return new byte[0];
108: }
109:
110: StringBuffer encodedValue = removeWhitespace(ch, start, length);
111: int encodedLength = encodedValue.length();
112: if (encodedLength == 0) {
113: return new byte[0];
114: }
115:
116: int valueLength = encodedValue.length() / 2;
117: byte[] value = new byte[valueLength];
118:
119: int encodedIdx = 0;
120: for (int i = 0; i < valueLength; ++i) {
121: int nibble1 = HEXADECIMAL_TO_NIBBLE_TABLE[encodedValue
122: .charAt(encodedIdx++) - '0'];
123: int nibble2 = HEXADECIMAL_TO_NIBBLE_TABLE[encodedValue
124: .charAt(encodedIdx++) - '0'];
125: value[i] = (byte) ((nibble1 << 4) | nibble2);
126: }
127:
128: return value;
129: }
130:
131: public final void convertToCharacters(Object data, StringBuffer s) {
132: if (data == null) {
133: return;
134: }
135: final byte[] value = (byte[]) data;
136: if (value.length == 0) {
137: return;
138: }
139:
140: s.ensureCapacity(value.length * 2);
141: for (int i = 0; i < value.length; ++i) {
142: s
143: .append(NIBBLE_TO_HEXADECIMAL_TABLE[(value[i] >>> 4) & 0xf]);
144: s.append(NIBBLE_TO_HEXADECIMAL_TABLE[value[i] & 0xf]);
145: }
146: }
147:
148: public final int getPrimtiveLengthFromOctetLength(int octetLength)
149: throws EncodingAlgorithmException {
150: return octetLength * 2;
151: }
152:
153: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
154: return primitiveLength / 2;
155: }
156:
157: public final void encodeToBytes(Object array, int astart,
158: int alength, byte[] b, int start) {
159: System.arraycopy((byte[]) array, astart, b, start, alength);
160: }
161: }
|