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