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 53 59 public class ShortEncodingAlgorithm extends IntegerEncodingAlgorithm { 60 61 public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { 62 if (octetLength % SHORT_SIZE != 0) { 63 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance(). 64 getString("message.lengthNotMultipleOfShort", new Object []{new Integer (SHORT_SIZE)})); 65 } 66 67 return octetLength / SHORT_SIZE; 68 } 69 70 public int getOctetLengthFromPrimitiveLength(int primitiveLength) { 71 return primitiveLength * SHORT_SIZE; 72 } 73 74 public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException { 75 short[] data = new short[getPrimtiveLengthFromOctetLength(length)]; 76 decodeFromBytesToShortArray(data, 0, b, start, length); 77 78 return data; 79 } 80 81 public final Object decodeFromInputStream(InputStream s) throws IOException { 82 return decodeFromInputStreamToShortArray(s); 83 } 84 85 86 public void encodeToOutputStream(Object data, OutputStream s) throws IOException { 87 if (!(data instanceof short[])) { 88 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.dataNotShortArray")); 89 } 90 91 final short[] idata = (short[])data; 92 93 encodeToOutputStreamFromShortArray(idata, s); 94 } 95 96 97 public final Object convertFromCharacters(char[] ch, int start, int length) { 98 final CharBuffer cb = CharBuffer.wrap(ch, start, length); 99 final List shortList = new ArrayList (); 100 101 matchWhiteSpaceDelimnatedWords(cb, 102 new WordListener() { 103 public void word(int start, int end) { 104 String iStringValue = cb.subSequence(start, end).toString(); 105 shortList.add(Short.valueOf(iStringValue)); 106 } 107 } 108 ); 109 110 return generateArrayFromList(shortList); 111 } 112 113 public final void convertToCharacters(Object data, StringBuffer s) { 114 if (!(data instanceof short[])) { 115 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.dataNotShortArray")); 116 } 117 118 final short[] idata = (short[])data; 119 120 convertToCharactersFromShortArray(idata, s); 121 } 122 123 124 public final void decodeFromBytesToShortArray(short[] sdata, int istart, byte[] b, int start, int length) { 125 final int size = length / SHORT_SIZE; 126 for (int i = 0; i < size; i++) { 127 sdata[istart++] = (short) (((b[start++] & 0xFF) << 8) | 128 (b[start++] & 0xFF)); 129 } 130 } 131 132 public final short[] decodeFromInputStreamToShortArray(InputStream s) throws IOException { 133 final List shortList = new ArrayList (); 134 final byte[] b = new byte[SHORT_SIZE]; 135 136 while (true) { 137 int n = s.read(b); 138 if (n != 2) { 139 if (n == -1) { 140 break; 141 } 142 143 while(n != 2) { 144 final int m = s.read(b, n, SHORT_SIZE - n); 145 if (m == -1) { 146 throw new EOFException (); 147 } 148 n += m; 149 } 150 } 151 152 final int i = ((b[0] & 0xFF) << 8) | 153 (b[1] & 0xFF); 154 shortList.add(new Short ((short)i)); 155 } 156 157 return generateArrayFromList(shortList); 158 } 159 160 161 public final void encodeToOutputStreamFromShortArray(short[] idata, OutputStream s) throws IOException { 162 for (int i = 0; i < idata.length; i++) { 163 final int bits = idata[i]; 164 s.write((bits >>> 8) & 0xFF); 165 s.write(bits & 0xFF); 166 } 167 } 168 169 public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) { 170 encodeToBytesFromShortArray((short[])array, astart, alength, b, start); 171 } 172 173 public final void encodeToBytesFromShortArray(short[] sdata, int istart, int ilength, byte[] b, int start) { 174 final int iend = istart + ilength; 175 for (int i = istart; i < iend; i++) { 176 final short bits = sdata[i]; 177 b[start++] = (byte)((bits >>> 8) & 0xFF); 178 b[start++] = (byte)(bits & 0xFF); 179 } 180 } 181 182 183 public final void convertToCharactersFromShortArray(short[] sdata, StringBuffer s) { 184 final int end = sdata.length - 1; 185 for (int i = 0; i <= end; i++) { 186 s.append(Short.toString(sdata[i])); 187 if (i != end) { 188 s.append(' '); 189 } 190 } 191 } 192 193 194 public final short[] generateArrayFromList(List array) { 195 short[] sdata = new short[array.size()]; 196 for (int i = 0; i < sdata.length; i++) { 197 sdata[i] = ((Short )array.get(i)).shortValue(); 198 } 199 200 return sdata; 201 } 202 } 203 | Popular Tags |