KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 JavaDoc[]{new Integer JavaDoc(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 JavaDoc 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 JavaDoc decodeFromInputStream(InputStream JavaDoc s) throws IOException JavaDoc {
78         return decodeFromInputStreamToDoubleArray(s);
79     }
80     
81     
82     public void encodeToOutputStream(Object JavaDoc data, OutputStream JavaDoc s) throws IOException JavaDoc {
83         if (!(data instanceof double[])) {
84             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.dataNotDouble"));
85         }
86         
87         final double[] fdata = (double[])data;
88         
89         encodeToOutputStreamFromDoubleArray(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 doubleList = 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                 doubleList.add(Float.valueOf(fStringValue));
101             }
102         }
103         );
104         
105         return generateArrayFromList(doubleList);
106     }
107     
108     public final void convertToCharacters(Object JavaDoc data, StringBuffer JavaDoc s) {
109         if (!(data instanceof double[])) {
110             throw new IllegalArgumentException JavaDoc(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 JavaDoc s) throws IOException JavaDoc {
136         final List JavaDoc doubleList = new ArrayList JavaDoc();
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 JavaDoc();
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 JavaDoc(Double.longBitsToDouble(bits)));
166         }
167         
168         return generateArrayFromList(doubleList);
169     }
170     
171     
172     public final void encodeToOutputStreamFromDoubleArray(double[] fdata, OutputStream JavaDoc s) throws IOException JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc array) {
218         double[] fdata = new double[array.size()];
219         for (int i = 0; i < fdata.length; i++) {
220             fdata[i] = ((Double JavaDoc)array.get(i)).doubleValue();
221         }
222         
223         return fdata;
224     }
225     
226 }
227
Popular Tags