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