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 FloatEncodingAlgorithm extends IEEE754FloatingPointEncodingAlgorithm { 56 57 public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { 58 if (octetLength % FLOAT_SIZE != 0) { 59 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance(). 60 getString("message.lengthNotMultipleOfFloat", new Object []{new Integer (FLOAT_SIZE)})); 61 } 62 63 return octetLength / FLOAT_SIZE; 64 } 65 66 public int getOctetLengthFromPrimitiveLength(int primitiveLength) { 67 return primitiveLength * FLOAT_SIZE; 68 } 69 70 public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException { 71 float[] data = new float[getPrimtiveLengthFromOctetLength(length)]; 72 decodeFromBytesToFloatArray(data, 0, b, start, length); 73 74 return data; 75 } 76 77 public final Object decodeFromInputStream(InputStream s) throws IOException { 78 return decodeFromInputStreamToFloatArray(s); 79 } 80 81 82 public void encodeToOutputStream(Object data, OutputStream s) throws IOException { 83 if (!(data instanceof float[])) { 84 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.dataNotFloat")); 85 } 86 87 final float[] fdata = (float[])data; 88 89 encodeToOutputStreamFromFloatArray(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 floatList = 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 floatList.add(Float.valueOf(fStringValue)); 101 } 102 } 103 ); 104 105 return generateArrayFromList(floatList); 106 } 107 108 public final void convertToCharacters(Object data, StringBuffer s) { 109 if (!(data instanceof float[])) { 110 throw new IllegalArgumentException (CommonResourceBundle.getInstance().getString("message.dataNotFloat")); 111 } 112 113 final float[] fdata = (float[])data; 114 115 convertToCharactersFromFloatArray(fdata, s); 116 } 117 118 119 public final void decodeFromBytesToFloatArray(float[] data, int fstart, byte[] b, int start, int length) { 120 final int size = length / FLOAT_SIZE; 121 for (int i = 0; i < size; i++) { 122 final int bits = ((b[start++] & 0xFF) << 24) | 123 ((b[start++] & 0xFF) << 16) | 124 ((b[start++] & 0xFF) << 8) | 125 (b[start++] & 0xFF); 126 data[fstart++] = Float.intBitsToFloat(bits); 127 } 128 } 129 130 public final float[] decodeFromInputStreamToFloatArray(InputStream s) throws IOException { 131 final List floatList = new ArrayList (); 132 final byte[] b = new byte[FLOAT_SIZE]; 133 134 while (true) { 135 int n = s.read(b); 136 if (n != 4) { 137 if (n == -1) { 138 break; 139 } 140 141 while(n != 4) { 142 final int m = s.read(b, n, FLOAT_SIZE - n); 143 if (m == -1) { 144 throw new EOFException (); 145 } 146 n += m; 147 } 148 } 149 150 final int bits = ((b[0] & 0xFF) << 24) | 151 ((b[1] & 0xFF) << 16) | 152 ((b[2] & 0xFF) << 8) | 153 (b[3] & 0xFF); 154 floatList.add(new Float (Float.intBitsToFloat(bits))); 155 } 156 157 return generateArrayFromList(floatList); 158 } 159 160 161 public final void encodeToOutputStreamFromFloatArray(float[] fdata, OutputStream s) throws IOException { 162 for (int i = 0; i < fdata.length; i++) { 163 final int bits = Float.floatToIntBits(fdata[i]); 164 s.write((bits >>> 24) & 0xFF); 165 s.write((bits >>> 16) & 0xFF); 166 s.write((bits >>> 8) & 0xFF); 167 s.write(bits & 0xFF); 168 } 169 } 170 171 public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) { 172 encodeToBytesFromFloatArray((float[])array, astart, alength, b, start); 173 } 174 175 public final void encodeToBytesFromFloatArray(float[] fdata, int fstart, int flength, byte[] b, int start) { 176 final int fend = fstart + flength; 177 for (int i = fstart; i < fend; i++) { 178 final int bits = Float.floatToIntBits(fdata[i]); 179 b[start++] = (byte)((bits >>> 24) & 0xFF); 180 b[start++] = (byte)((bits >>> 16) & 0xFF); 181 b[start++] = (byte)((bits >>> 8) & 0xFF); 182 b[start++] = (byte)(bits & 0xFF); 183 } 184 } 185 186 187 public final void convertToCharactersFromFloatArray(float[] fdata, StringBuffer s) { 188 final int end = fdata.length - 1; 189 for (int i = 0; i <= end; i++) { 190 s.append(Float.toString(fdata[i])); 191 if (i != end) { 192 s.append(' '); 193 } 194 } 195 } 196 197 198 public final float[] generateArrayFromList(List array) { 199 float[] fdata = new float[array.size()]; 200 for (int i = 0; i < fdata.length; i++) { 201 fdata[i] = ((Float )array.get(i)).floatValue(); 202 } 203 204 return fdata; 205 } 206 207 } 208 | Popular Tags |