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