1 38 39 40 package com.sun.xml.fastinfoset.algorithm; 41 42 import java.io.EOFException ; 43 import java.io.IOException ; 44 import java.io.InputStream ; 45 import java.io.OutputStream ; 46 import java.nio.CharBuffer ; 47 import java.util.ArrayList ; 48 import java.util.List ; 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, 59 1, 60 2, 61 3, 62 4, 63 5, 64 6, 65 7, 66 8, 67 9, -1, -1, -1, -1, -1, -1, -1, 68 10, 69 11, 70 12, 71 13, 72 14, 73 15, 74 -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 10, 77 11, 78 12, 79 13, 80 14, 81 15 }; 82 83 public final Object 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 decodeFromInputStream(InputStream s) throws IOException { 90 throw new UnsupportedOperationException (CommonResourceBundle.getInstance().getString("message.notImplemented")); 91 } 92 93 94 public void encodeToOutputStream(Object data, OutputStream s) throws IOException { 95 if (!(data instanceof byte[])) { 96 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.dataNotByteArray")); 97 } 98 99 s.write((byte[])data); 100 } 101 102 public final Object convertFromCharacters(char[] ch, int start, int length) { 103 if (length == 0) { 104 return new byte[0]; 105 } 106 107 StringBuffer 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 data, StringBuffer 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 array, int astart, int alength, byte[] b, int start) { 153 System.arraycopy((byte[])array, astart, b, start, alength); 154 } 155 } 156 | Popular Tags |