001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from su
024: ch communities and third parties that govern the
025: * use of those portions, and any licenses granted hereunder do not alter any
026: * rights and obligations you may have under such open source licenses,
027: * however, the disclaimer of warranty and limitation of liability provisions
028: * in this License will apply to all Software in this distribution.
029: *
030: * You acknowledge that the Software is not designed, licensed or intended
031: * for use in the design, construction, operation or maintenance of any nuclear
032: * facility.
033: *
034: * Apache License
035: * Version 2.0, January 2004
036: * http://www.apache.org/licenses/
037: *
038: */
039:
040: package com.sun.xml.fastinfoset.algorithm;
041:
042: import java.io.EOFException;
043: import java.io.IOException;
044: import java.io.InputStream;
045: import java.io.OutputStream;
046: import java.nio.CharBuffer;
047: import java.util.ArrayList;
048: import java.util.List;
049: import org.jvnet.fastinfoset.EncodingAlgorithmException;
050: import com.sun.xml.fastinfoset.CommonResourceBundle;
051:
052: public class DoubleEncodingAlgorithm extends
053: IEEE754FloatingPointEncodingAlgorithm {
054:
055: public final int getPrimtiveLengthFromOctetLength(int octetLength)
056: throws EncodingAlgorithmException {
057: if (octetLength % DOUBLE_SIZE != 0) {
058: throw new EncodingAlgorithmException(
059: CommonResourceBundle
060: .getInstance()
061: .getString(
062: "message.lengthIsNotMultipleOfDouble",
063: new Object[] { Integer
064: .valueOf(DOUBLE_SIZE) }));
065: }
066:
067: return octetLength / DOUBLE_SIZE;
068: }
069:
070: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
071: return primitiveLength * DOUBLE_SIZE;
072: }
073:
074: public final Object decodeFromBytes(byte[] b, int start, int length)
075: throws EncodingAlgorithmException {
076: double[] data = new double[getPrimtiveLengthFromOctetLength(length)];
077: decodeFromBytesToDoubleArray(data, 0, b, start, length);
078:
079: return data;
080: }
081:
082: public final Object decodeFromInputStream(InputStream s)
083: throws IOException {
084: return decodeFromInputStreamToDoubleArray(s);
085: }
086:
087: public void encodeToOutputStream(Object data, OutputStream s)
088: throws IOException {
089: if (!(data instanceof double[])) {
090: throw new IllegalArgumentException(CommonResourceBundle
091: .getInstance().getString("message.dataNotDouble"));
092: }
093:
094: final double[] fdata = (double[]) data;
095:
096: encodeToOutputStreamFromDoubleArray(fdata, s);
097: }
098:
099: public final Object convertFromCharacters(char[] ch, int start,
100: int length) {
101: final CharBuffer cb = CharBuffer.wrap(ch, start, length);
102: final List doubleList = new ArrayList();
103:
104: matchWhiteSpaceDelimnatedWords(cb, new WordListener() {
105: public void word(int start, int end) {
106: String fStringValue = cb.subSequence(start, end)
107: .toString();
108: doubleList.add(Double.valueOf(fStringValue));
109: }
110: });
111:
112: return generateArrayFromList(doubleList);
113: }
114:
115: public final void convertToCharacters(Object data, StringBuffer s) {
116: if (!(data instanceof double[])) {
117: throw new IllegalArgumentException(CommonResourceBundle
118: .getInstance().getString("message.dataNotDouble"));
119: }
120:
121: final double[] fdata = (double[]) data;
122:
123: convertToCharactersFromDoubleArray(fdata, s);
124: }
125:
126: public final void decodeFromBytesToDoubleArray(double[] data,
127: int fstart, byte[] b, int start, int length) {
128: final int size = length / DOUBLE_SIZE;
129: for (int i = 0; i < size; i++) {
130: final long bits = ((long) (b[start++] & 0xFF) << 56)
131: | ((long) (b[start++] & 0xFF) << 48)
132: | ((long) (b[start++] & 0xFF) << 40)
133: | ((long) (b[start++] & 0xFF) << 32)
134: | ((long) (b[start++] & 0xFF) << 24)
135: | ((long) (b[start++] & 0xFF) << 16)
136: | ((long) (b[start++] & 0xFF) << 8)
137: | (long) (b[start++] & 0xFF);
138: data[fstart++] = Double.longBitsToDouble(bits);
139: }
140: }
141:
142: public final double[] decodeFromInputStreamToDoubleArray(
143: InputStream s) throws IOException {
144: final List doubleList = new ArrayList();
145: final byte[] b = new byte[DOUBLE_SIZE];
146:
147: while (true) {
148: int n = s.read(b);
149: if (n != DOUBLE_SIZE) {
150: if (n == -1) {
151: break;
152: }
153:
154: while (n != DOUBLE_SIZE) {
155: final int m = s.read(b, n, DOUBLE_SIZE - n);
156: if (m == -1) {
157: throw new EOFException();
158: }
159: n += m;
160: }
161: }
162:
163: final long bits = ((long) (b[0] & 0xFF) << 56)
164: | ((long) (b[1] & 0xFF) << 48)
165: | ((long) (b[2] & 0xFF) << 40)
166: | ((long) (b[3] & 0xFF) << 32)
167: | ((b[4] & 0xFF) << 24) | ((b[5] & 0xFF) << 16)
168: | ((b[6] & 0xFF) << 8) | (b[7] & 0xFF);
169:
170: doubleList.add(Double
171: .valueOf(Double.longBitsToDouble(bits)));
172: }
173:
174: return generateArrayFromList(doubleList);
175: }
176:
177: public final void encodeToOutputStreamFromDoubleArray(
178: double[] fdata, OutputStream s) throws IOException {
179: for (int i = 0; i < fdata.length; i++) {
180: final long bits = Double.doubleToLongBits(fdata[i]);
181: s.write((int) ((bits >>> 56) & 0xFF));
182: s.write((int) ((bits >>> 48) & 0xFF));
183: s.write((int) ((bits >>> 40) & 0xFF));
184: s.write((int) ((bits >>> 32) & 0xFF));
185: s.write((int) ((bits >>> 24) & 0xFF));
186: s.write((int) ((bits >>> 16) & 0xFF));
187: s.write((int) ((bits >>> 8) & 0xFF));
188: s.write((int) (bits & 0xFF));
189: }
190: }
191:
192: public final void encodeToBytes(Object array, int astart,
193: int alength, byte[] b, int start) {
194: encodeToBytesFromDoubleArray((double[]) array, astart, alength,
195: b, start);
196: }
197:
198: public final void encodeToBytesFromDoubleArray(double[] fdata,
199: int fstart, int flength, byte[] b, int start) {
200: final int fend = fstart + flength;
201: for (int i = fstart; i < fend; i++) {
202: final long bits = Double.doubleToLongBits(fdata[i]);
203: b[start++] = (byte) ((bits >>> 56) & 0xFF);
204: b[start++] = (byte) ((bits >>> 48) & 0xFF);
205: b[start++] = (byte) ((bits >>> 40) & 0xFF);
206: b[start++] = (byte) ((bits >>> 32) & 0xFF);
207: b[start++] = (byte) ((bits >>> 24) & 0xFF);
208: b[start++] = (byte) ((bits >>> 16) & 0xFF);
209: b[start++] = (byte) ((bits >>> 8) & 0xFF);
210: b[start++] = (byte) (bits & 0xFF);
211: }
212: }
213:
214: public final void convertToCharactersFromDoubleArray(
215: double[] fdata, StringBuffer s) {
216: final int end = fdata.length - 1;
217: for (int i = 0; i <= end; i++) {
218: s.append(Double.toString(fdata[i]));
219: if (i != end) {
220: s.append(' ');
221: }
222: }
223: }
224:
225: public final double[] generateArrayFromList(List array) {
226: double[] fdata = new double[array.size()];
227: for (int i = 0; i < fdata.length; i++) {
228: fdata[i] = ((Double) array.get(i)).doubleValue();
229: }
230:
231: return fdata;
232: }
233:
234: }
|