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 FloatEncodingAlgorithm extends
053: IEEE754FloatingPointEncodingAlgorithm {
054:
055: public final int getPrimtiveLengthFromOctetLength(int octetLength)
056: throws EncodingAlgorithmException {
057: if (octetLength % FLOAT_SIZE != 0) {
058: throw new EncodingAlgorithmException(
059: CommonResourceBundle.getInstance()
060: .getString(
061: "message.lengthNotMultipleOfFloat",
062: new Object[] { Integer
063: .valueOf(FLOAT_SIZE) }));
064: }
065:
066: return octetLength / FLOAT_SIZE;
067: }
068:
069: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
070: return primitiveLength * FLOAT_SIZE;
071: }
072:
073: public final Object decodeFromBytes(byte[] b, int start, int length)
074: throws EncodingAlgorithmException {
075: float[] data = new float[getPrimtiveLengthFromOctetLength(length)];
076: decodeFromBytesToFloatArray(data, 0, b, start, length);
077:
078: return data;
079: }
080:
081: public final Object decodeFromInputStream(InputStream s)
082: throws IOException {
083: return decodeFromInputStreamToFloatArray(s);
084: }
085:
086: public void encodeToOutputStream(Object data, OutputStream s)
087: throws IOException {
088: if (!(data instanceof float[])) {
089: throw new IllegalArgumentException(CommonResourceBundle
090: .getInstance().getString("message.dataNotFloat"));
091: }
092:
093: final float[] fdata = (float[]) data;
094:
095: encodeToOutputStreamFromFloatArray(fdata, s);
096: }
097:
098: public final Object convertFromCharacters(char[] ch, int start,
099: int length) {
100: final CharBuffer cb = CharBuffer.wrap(ch, start, length);
101: final List floatList = new ArrayList();
102:
103: matchWhiteSpaceDelimnatedWords(cb, new WordListener() {
104: public void word(int start, int end) {
105: String fStringValue = cb.subSequence(start, end)
106: .toString();
107: floatList.add(Float.valueOf(fStringValue));
108: }
109: });
110:
111: return generateArrayFromList(floatList);
112: }
113:
114: public final void convertToCharacters(Object data, StringBuffer s) {
115: if (!(data instanceof float[])) {
116: throw new IllegalArgumentException(CommonResourceBundle
117: .getInstance().getString("message.dataNotFloat"));
118: }
119:
120: final float[] fdata = (float[]) data;
121:
122: convertToCharactersFromFloatArray(fdata, s);
123: }
124:
125: public final void decodeFromBytesToFloatArray(float[] data,
126: int fstart, byte[] b, int start, int length) {
127: final int size = length / FLOAT_SIZE;
128: for (int i = 0; i < size; i++) {
129: final int bits = ((b[start++] & 0xFF) << 24)
130: | ((b[start++] & 0xFF) << 16)
131: | ((b[start++] & 0xFF) << 8) | (b[start++] & 0xFF);
132: data[fstart++] = Float.intBitsToFloat(bits);
133: }
134: }
135:
136: public final float[] decodeFromInputStreamToFloatArray(InputStream s)
137: throws IOException {
138: final List floatList = new ArrayList();
139: final byte[] b = new byte[FLOAT_SIZE];
140:
141: while (true) {
142: int n = s.read(b);
143: if (n != 4) {
144: if (n == -1) {
145: break;
146: }
147:
148: while (n != 4) {
149: final int m = s.read(b, n, FLOAT_SIZE - n);
150: if (m == -1) {
151: throw new EOFException();
152: }
153: n += m;
154: }
155: }
156:
157: final int bits = ((b[0] & 0xFF) << 24)
158: | ((b[1] & 0xFF) << 16) | ((b[2] & 0xFF) << 8)
159: | (b[3] & 0xFF);
160: floatList.add(Float.valueOf(Float.intBitsToFloat(bits)));
161: }
162:
163: return generateArrayFromList(floatList);
164: }
165:
166: public final void encodeToOutputStreamFromFloatArray(float[] fdata,
167: OutputStream s) throws IOException {
168: for (int i = 0; i < fdata.length; i++) {
169: final int bits = Float.floatToIntBits(fdata[i]);
170: s.write((bits >>> 24) & 0xFF);
171: s.write((bits >>> 16) & 0xFF);
172: s.write((bits >>> 8) & 0xFF);
173: s.write(bits & 0xFF);
174: }
175: }
176:
177: public final void encodeToBytes(Object array, int astart,
178: int alength, byte[] b, int start) {
179: encodeToBytesFromFloatArray((float[]) array, astart, alength,
180: b, start);
181: }
182:
183: public final void encodeToBytesFromFloatArray(float[] fdata,
184: int fstart, int flength, byte[] b, int start) {
185: final int fend = fstart + flength;
186: for (int i = fstart; i < fend; i++) {
187: final int bits = Float.floatToIntBits(fdata[i]);
188: b[start++] = (byte) ((bits >>> 24) & 0xFF);
189: b[start++] = (byte) ((bits >>> 16) & 0xFF);
190: b[start++] = (byte) ((bits >>> 8) & 0xFF);
191: b[start++] = (byte) (bits & 0xFF);
192: }
193: }
194:
195: public final void convertToCharactersFromFloatArray(float[] fdata,
196: StringBuffer s) {
197: final int end = fdata.length - 1;
198: for (int i = 0; i <= end; i++) {
199: s.append(Float.toString(fdata[i]));
200: if (i != end) {
201: s.append(' ');
202: }
203: }
204: }
205:
206: public final float[] generateArrayFromList(List array) {
207: float[] fdata = new float[array.size()];
208: for (int i = 0; i < fdata.length; i++) {
209: fdata[i] = ((Float) array.get(i)).floatValue();
210: }
211:
212: return fdata;
213: }
214:
215: }
|