Skip to content

Package: FloatEncodingAlgorithm

FloatEncodingAlgorithm

nameinstructionbranchcomplexitylinemethod
FloatEncodingAlgorithm()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
convertFromCharacters(char[], int, int)
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
convertToCharacters(Object, StringBuffer)
M: 18 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
convertToCharactersFromFloatArray(float[], StringBuffer)
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
decodeFromBytes(byte[], int, int)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
decodeFromBytesToFloatArray(float[], int, byte[], int, int)
M: 0 C: 52
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
decodeFromInputStream(InputStream)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
decodeFromInputStreamToFloatArray(InputStream)
M: 82 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
encodeToBytes(Object, int, int, byte[], int)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
encodeToBytesFromFloatArray(float[], int, int, byte[], int)
M: 0 C: 55
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
encodeToOutputStream(Object, OutputStream)
M: 7 C: 11
61%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
encodeToOutputStreamFromFloatArray(float[], OutputStream)
M: 0 C: 40
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
generateArrayFromList(List)
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getOctetLengthFromPrimitiveLength(int)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getPrimtiveLengthFromOctetLength(int)
M: 14 C: 8
36%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 2
50%
M: 0 C: 1
100%

Coverage

1: /*
2: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3: *
4: * Copyright (c) 2004-2018 Oracle and/or its affiliates. All rights reserved.
5: *
6: * Oracle licenses this file to You under the Apache License, Version 2.0
7: * (the "License"); you may not use this file except in compliance with
8: * the License. You may 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,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package com.sun.xml.fastinfoset.algorithm;
20:
21: import java.io.EOFException;
22: import java.io.IOException;
23: import java.io.InputStream;
24: import java.io.OutputStream;
25: import java.nio.CharBuffer;
26: import java.util.ArrayList;
27: import java.util.List;
28: import org.jvnet.fastinfoset.EncodingAlgorithmException;
29: import com.sun.xml.fastinfoset.CommonResourceBundle;
30:
31:
32:
33: public class FloatEncodingAlgorithm extends IEEE754FloatingPointEncodingAlgorithm {
34:
35: public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
36:• if (octetLength % FLOAT_SIZE != 0) {
37: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
38: getString("message.lengthNotMultipleOfFloat", new Object[]{Integer.valueOf(FLOAT_SIZE)}));
39: }
40:
41: return octetLength / FLOAT_SIZE;
42: }
43:
44: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
45: return primitiveLength * FLOAT_SIZE;
46: }
47:
48: public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
49: float[] data = new float[getPrimtiveLengthFromOctetLength(length)];
50: decodeFromBytesToFloatArray(data, 0, b, start, length);
51:
52: return data;
53: }
54:
55: public final Object decodeFromInputStream(InputStream s) throws IOException {
56: return decodeFromInputStreamToFloatArray(s);
57: }
58:
59:
60: public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
61:• if (!(data instanceof float[])) {
62: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotFloat"));
63: }
64:
65: final float[] fdata = (float[])data;
66:
67: encodeToOutputStreamFromFloatArray(fdata, s);
68: }
69:
70: public final Object convertFromCharacters(char[] ch, int start, int length) {
71: final CharBuffer cb = CharBuffer.wrap(ch, start, length);
72: final List floatList = new ArrayList();
73:
74: matchWhiteSpaceDelimnatedWords(cb,
75: new WordListener() {
76: public void word(int start, int end) {
77: String fStringValue = cb.subSequence(start, end).toString();
78: floatList.add(Float.valueOf(fStringValue));
79: }
80: }
81: );
82:
83: return generateArrayFromList(floatList);
84: }
85:
86: public final void convertToCharacters(Object data, StringBuffer s) {
87:• if (!(data instanceof float[])) {
88: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotFloat"));
89: }
90:
91: final float[] fdata = (float[])data;
92:
93: convertToCharactersFromFloatArray(fdata, s);
94: }
95:
96:
97: public final void decodeFromBytesToFloatArray(float[] data, int fstart, byte[] b, int start, int length) {
98: final int size = length / FLOAT_SIZE;
99:• for (int i = 0; i < size; i++) {
100: final int bits = ((b[start++] & 0xFF) << 24) |
101: ((b[start++] & 0xFF) << 16) |
102: ((b[start++] & 0xFF) << 8) |
103: (b[start++] & 0xFF);
104: data[fstart++] = Float.intBitsToFloat(bits);
105: }
106: }
107:
108: public final float[] decodeFromInputStreamToFloatArray(InputStream s) throws IOException {
109: final List floatList = new ArrayList();
110: final byte[] b = new byte[FLOAT_SIZE];
111:
112: while (true) {
113: int n = s.read(b);
114:• if (n != 4) {
115:• if (n == -1) {
116: break;
117: }
118:
119:• while(n != 4) {
120: final int m = s.read(b, n, FLOAT_SIZE - n);
121:• if (m == -1) {
122: throw new EOFException();
123: }
124: n += m;
125: }
126: }
127:
128: final int bits = ((b[0] & 0xFF) << 24) |
129: ((b[1] & 0xFF) << 16) |
130: ((b[2] & 0xFF) << 8) |
131: (b[3] & 0xFF);
132: floatList.add(Float.valueOf(Float.intBitsToFloat(bits)));
133: }
134:
135: return generateArrayFromList(floatList);
136: }
137:
138:
139: public final void encodeToOutputStreamFromFloatArray(float[] fdata, OutputStream s) throws IOException {
140:• for (int i = 0; i < fdata.length; i++) {
141: final int bits = Float.floatToIntBits(fdata[i]);
142: s.write((bits >>> 24) & 0xFF);
143: s.write((bits >>> 16) & 0xFF);
144: s.write((bits >>> 8) & 0xFF);
145: s.write(bits & 0xFF);
146: }
147: }
148:
149: public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) {
150: encodeToBytesFromFloatArray((float[])array, astart, alength, b, start);
151: }
152:
153: public final void encodeToBytesFromFloatArray(float[] fdata, int fstart, int flength, byte[] b, int start) {
154: final int fend = fstart + flength;
155:• for (int i = fstart; i < fend; i++) {
156: final int bits = Float.floatToIntBits(fdata[i]);
157: b[start++] = (byte)((bits >>> 24) & 0xFF);
158: b[start++] = (byte)((bits >>> 16) & 0xFF);
159: b[start++] = (byte)((bits >>> 8) & 0xFF);
160: b[start++] = (byte)(bits & 0xFF);
161: }
162: }
163:
164:
165: public final void convertToCharactersFromFloatArray(float[] fdata, StringBuffer s) {
166: final int end = fdata.length - 1;
167:• for (int i = 0; i <= end; i++) {
168: s.append(Float.toString(fdata[i]));
169:• if (i != end) {
170: s.append(' ');
171: }
172: }
173: }
174:
175:
176: public final float[] generateArrayFromList(List array) {
177: float[] fdata = new float[array.size()];
178:• for (int i = 0; i < fdata.length; i++) {
179: fdata[i] = ((Float)array.get(i)).floatValue();
180: }
181:
182: return fdata;
183: }
184:
185: }