KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > algorithm > FloatEncodingAlgorithm


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from su
24  ch communities and third parties that govern the
25  * use of those portions, and any licenses granted hereunder do not alter any
26  * rights and obligations you may have under such open source licenses,
27  * however, the disclaimer of warranty and limitation of liability provisions
28  * in this License will apply to all Software in this distribution.
29  *
30  * You acknowledge that the Software is not designed, licensed or intended
31  * for use in the design, construction, operation or maintenance of any nuclear
32  * facility.
33  *
34  * Apache License
35  * Version 2.0, January 2004
36  * http://www.apache.org/licenses/
37  *
38  */

39
40
41 package com.sun.xml.fastinfoset.algorithm;
42
43 import java.io.EOFException JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.io.OutputStream JavaDoc;
47 import java.nio.CharBuffer JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.List JavaDoc;
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 JavaDoc[]{new Integer JavaDoc(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 JavaDoc 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 JavaDoc decodeFromInputStream(InputStream JavaDoc s) throws IOException JavaDoc {
78         return decodeFromInputStreamToFloatArray(s);
79     }
80     
81     
82     public void encodeToOutputStream(Object JavaDoc data, OutputStream JavaDoc s) throws IOException JavaDoc {
83         if (!(data instanceof float[])) {
84             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.dataNotFloat"));
85         }
86         
87         final float[] fdata = (float[])data;
88         
89         encodeToOutputStreamFromFloatArray(fdata, s);
90     }
91     
92     public final Object JavaDoc convertFromCharacters(char[] ch, int start, int length) {
93         final CharBuffer JavaDoc cb = CharBuffer.wrap(ch, start, length);
94         final List JavaDoc floatList = new ArrayList JavaDoc();
95         
96         matchWhiteSpaceDelimnatedWords(cb,
97                 new WordListener() {
98             public void word(int start, int end) {
99                 String JavaDoc 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 JavaDoc data, StringBuffer JavaDoc s) {
109         if (!(data instanceof float[])) {
110             throw new IllegalArgumentException JavaDoc(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 JavaDoc s) throws IOException JavaDoc {
131         final List JavaDoc floatList = new ArrayList JavaDoc();
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 JavaDoc();
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 JavaDoc(Float.intBitsToFloat(bits)));
155         }
156         
157         return generateArrayFromList(floatList);
158     }
159     
160     
161     public final void encodeToOutputStreamFromFloatArray(float[] fdata, OutputStream JavaDoc s) throws IOException JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc array) {
199         float[] fdata = new float[array.size()];
200         for (int i = 0; i < fdata.length; i++) {
201             fdata[i] = ((Float JavaDoc)array.get(i)).floatValue();
202         }
203         
204         return fdata;
205     }
206     
207 }
208
Popular Tags