1 39 40 41 package com.sun.xml.fastinfoset.algorithm; 42 43 import java.io.EOFException ; 44 import java.io.IOException ; 45 import java.io.InputStream ; 46 import java.io.OutputStream ; 47 import java.nio.CharBuffer ; 48 import java.util.ArrayList ; 49 import java.util.List ; 50 import org.jvnet.fastinfoset.EncodingAlgorithmException; 51 import com.sun.xml.fastinfoset.CommonResourceBundle; 52 53 54 55 public class DoubleEncodingAlgorithm extends IEEE754FloatingPointEncodingAlgorithm { 56 57 public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { 58 if (octetLength % DOUBLE_SIZE != 0) { 59 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance(). 60 getString("message.lengthIsNotMultipleOfDouble", new Object []{new Integer (DOUBLE_SIZE)})); 61 } 62 63 return octetLength / DOUBLE_SIZE; 64 } 65 66 public int getOctetLengthFromPrimitiveLength(int primitiveLength) { 67 return primitiveLength * DOUBLE_SIZE; 68 } 69 70 public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException { 71 double[] data = new double[getPrimtiveLengthFromOctetLength(length)]; 72 decodeFromBytesToDoubleArray(data, 0, b, start, length); 73 74 return data; 75 } 76 77 public final Object decodeFromInputStream(InputStream s) throws IOException { 78 return decodeFromInputStreamToDoubleArray(s); 79 } 80 81 82 public void encodeToOutputStream(Object data, OutputStream s) throws IOException { 83 if (!(data instanceof double[])) { 84 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.dataNotDouble")); 85 } 86 87 final double[] fdata = (double[])data; 88 89 encodeToOutputStreamFromDoubleArray(fdata, s); 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 doubleList = new ArrayList (); 95 96 matchWhiteSpaceDelimnatedWords(cb, 97 new WordListener() { 98 public void word(int start, int end) { 99 String fStringValue = cb.subSequence(start, end).toString(); 100 doubleList.add(Float.valueOf(fStringValue)); 101 } 102 } 103 ); 104 105 return generateArrayFromList(doubleList); 106 } 107 108 public final void convertToCharacters(Object data, StringBuffer s) { 109 if (!(data instanceof double[])) { 110 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.dataNotDouble")); 111 } 112 113 final double[] fdata = (double[])data; 114 115 convertToCharactersFromDoubleArray(fdata, s); 116 } 117 118 119 public final void decodeFromBytesToDoubleArray(double[] data, int fstart, byte[] b, int start, int length) { 120 final int size = length / DOUBLE_SIZE; 121 for (int i = 0; i < size; i++) { 122 final long bits = 123 ((long)(b[start++] & 0xFF) << 56) | 124 ((long)(b[start++] & 0xFF) << 48) | 125 ((long)(b[start++] & 0xFF) << 40) | 126 ((long)(b[start++] & 0xFF) << 32) | 127 ((long)(b[start++] & 0xFF) << 24) | 128 ((long)(b[start++] & 0xFF) << 16) | 129 ((long)(b[start++] & 0xFF) << 8) | 130 (long)(b[start++] & 0xFF); 131 data[fstart++] = Double.longBitsToDouble(bits); 132 } 133 } 134 135 public final double[] decodeFromInputStreamToDoubleArray(InputStream s) throws IOException { 136 final List doubleList = new ArrayList (); 137 final byte[] b = new byte[DOUBLE_SIZE]; 138 139 while (true) { 140 int n = s.read(b); 141 if (n != DOUBLE_SIZE) { 142 if (n == -1) { 143 break; 144 } 145 146 while(n != DOUBLE_SIZE) { 147 final int m = s.read(b, n, DOUBLE_SIZE - n); 148 if (m == -1) { 149 throw new EOFException (); 150 } 151 n += m; 152 } 153 } 154 155 final int bits = 156 ((b[0] & 0xFF) << 56) | 157 ((b[1] & 0xFF) << 48) | 158 ((b[2] & 0xFF) << 40) | 159 ((b[3] & 0xFF) << 32) | 160 ((b[4] & 0xFF) << 24) | 161 ((b[5] & 0xFF) << 16) | 162 ((b[6] & 0xFF) << 8) | 163 (b[7] & 0xFF); 164 165 doubleList.add(new Double (Double.longBitsToDouble(bits))); 166 } 167 168 return generateArrayFromList(doubleList); 169 } 170 171 172 public final void encodeToOutputStreamFromDoubleArray(double[] fdata, OutputStream s) throws IOException { 173 for (int i = 0; i < fdata.length; i++) { 174 final long bits = Double.doubleToLongBits(fdata[i]); 175 s.write((int)((bits >>> 56) & 0xFF)); 176 s.write((int)((bits >>> 48) & 0xFF)); 177 s.write((int)((bits >>> 40) & 0xFF)); 178 s.write((int)((bits >>> 32) & 0xFF)); 179 s.write((int)((bits >>> 24) & 0xFF)); 180 s.write((int)((bits >>> 16) & 0xFF)); 181 s.write((int)((bits >>> 8) & 0xFF)); 182 s.write((int)(bits & 0xFF)); 183 } 184 } 185 186 public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) { 187 encodeToBytesFromDoubleArray((double[])array, astart, alength, b, start); 188 } 189 190 public final void encodeToBytesFromDoubleArray(double[] fdata, int fstart, int flength, byte[] b, int start) { 191 final int fend = fstart + flength; 192 for (int i = fstart; i < fend; i++) { 193 final long bits = Double.doubleToLongBits(fdata[i]); 194 b[start++] = (byte)((bits >>> 56) & 0xFF); 195 b[start++] = (byte)((bits >>> 48) & 0xFF); 196 b[start++] = (byte)((bits >>> 40) & 0xFF); 197 b[start++] = (byte)((bits >>> 32) & 0xFF); 198 b[start++] = (byte)((bits >>> 24) & 0xFF); 199 b[start++] = (byte)((bits >>> 16) & 0xFF); 200 b[start++] = (byte)((bits >>> 8) & 0xFF); 201 b[start++] = (byte)(bits & 0xFF); 202 } 203 } 204 205 206 public final void convertToCharactersFromDoubleArray(double[] fdata, StringBuffer s) { 207 final int end = fdata.length - 1; 208 for (int i = 0; i <= end; i++) { 209 s.append(Double.toString(fdata[i])); 210 if (i != end) { 211 s.append(' '); 212 } 213 } 214 } 215 216 217 public final double[] generateArrayFromList(List array) { 218 double[] fdata = new double[array.size()]; 219 for (int i = 0; i < fdata.length; i++) { 220 fdata[i] = ((Double )array.get(i)).doubleValue(); 221 } 222 223 return fdata; 224 } 225 226 } 227 | Popular Tags |