Skip to content

Package: DoubleEncodingAlgorithm$1

DoubleEncodingAlgorithm$1

nameinstructionbranchcomplexitylinemethod
word(int, int)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
{...}
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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 DoubleEncodingAlgorithm extends IEEE754FloatingPointEncodingAlgorithm {
34:
35: public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
36: if (octetLength % DOUBLE_SIZE != 0) {
37: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
38: getString("message.lengthIsNotMultipleOfDouble", new Object[]{Integer.valueOf(DOUBLE_SIZE)}));
39: }
40:
41: return octetLength / DOUBLE_SIZE;
42: }
43:
44: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
45: return primitiveLength * DOUBLE_SIZE;
46: }
47:
48: public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
49: double[] data = new double[getPrimtiveLengthFromOctetLength(length)];
50: decodeFromBytesToDoubleArray(data, 0, b, start, length);
51:
52: return data;
53: }
54:
55: public final Object decodeFromInputStream(InputStream s) throws IOException {
56: return decodeFromInputStreamToDoubleArray(s);
57: }
58:
59:
60: public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
61: if (!(data instanceof double[])) {
62: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotDouble"));
63: }
64:
65: final double[] fdata = (double[])data;
66:
67: encodeToOutputStreamFromDoubleArray(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 doubleList = 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: doubleList.add(Double.valueOf(fStringValue));
79: }
80: }
81: );
82:
83: return generateArrayFromList(doubleList);
84: }
85:
86: public final void convertToCharacters(Object data, StringBuffer s) {
87: if (!(data instanceof double[])) {
88: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotDouble"));
89: }
90:
91: final double[] fdata = (double[])data;
92:
93: convertToCharactersFromDoubleArray(fdata, s);
94: }
95:
96:
97: public final void decodeFromBytesToDoubleArray(double[] data, int fstart, byte[] b, int start, int length) {
98: final int size = length / DOUBLE_SIZE;
99: for (int i = 0; i < size; i++) {
100: final long bits =
101: ((long)(b[start++] & 0xFF) << 56) |
102: ((long)(b[start++] & 0xFF) << 48) |
103: ((long)(b[start++] & 0xFF) << 40) |
104: ((long)(b[start++] & 0xFF) << 32) |
105: ((long)(b[start++] & 0xFF) << 24) |
106: ((long)(b[start++] & 0xFF) << 16) |
107: ((long)(b[start++] & 0xFF) << 8) |
108: (long)(b[start++] & 0xFF);
109: data[fstart++] = Double.longBitsToDouble(bits);
110: }
111: }
112:
113: public final double[] decodeFromInputStreamToDoubleArray(InputStream s) throws IOException {
114: final List doubleList = new ArrayList();
115: final byte[] b = new byte[DOUBLE_SIZE];
116:
117: while (true) {
118: int n = s.read(b);
119: if (n != DOUBLE_SIZE) {
120: if (n == -1) {
121: break;
122: }
123:
124: while(n != DOUBLE_SIZE) {
125: final int m = s.read(b, n, DOUBLE_SIZE - n);
126: if (m == -1) {
127: throw new EOFException();
128: }
129: n += m;
130: }
131: }
132:
133: final long bits =
134: ((long)(b[0] & 0xFF) << 56) |
135: ((long)(b[1] & 0xFF) << 48) |
136: ((long)(b[2] & 0xFF) << 40) |
137: ((long)(b[3] & 0xFF) << 32) |
138: ((b[4] & 0xFF) << 24) |
139: ((b[5] & 0xFF) << 16) |
140: ((b[6] & 0xFF) << 8) |
141: (b[7] & 0xFF);
142:
143: doubleList.add(Double.valueOf(Double.longBitsToDouble(bits)));
144: }
145:
146: return generateArrayFromList(doubleList);
147: }
148:
149:
150: public final void encodeToOutputStreamFromDoubleArray(double[] fdata, OutputStream s) throws IOException {
151: for (int i = 0; i < fdata.length; i++) {
152: final long bits = Double.doubleToLongBits(fdata[i]);
153: s.write((int)((bits >>> 56) & 0xFF));
154: s.write((int)((bits >>> 48) & 0xFF));
155: s.write((int)((bits >>> 40) & 0xFF));
156: s.write((int)((bits >>> 32) & 0xFF));
157: s.write((int)((bits >>> 24) & 0xFF));
158: s.write((int)((bits >>> 16) & 0xFF));
159: s.write((int)((bits >>> 8) & 0xFF));
160: s.write((int)(bits & 0xFF));
161: }
162: }
163:
164: public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) {
165: encodeToBytesFromDoubleArray((double[])array, astart, alength, b, start);
166: }
167:
168: public final void encodeToBytesFromDoubleArray(double[] fdata, int fstart, int flength, byte[] b, int start) {
169: final int fend = fstart + flength;
170: for (int i = fstart; i < fend; i++) {
171: final long bits = Double.doubleToLongBits(fdata[i]);
172: b[start++] = (byte)((bits >>> 56) & 0xFF);
173: b[start++] = (byte)((bits >>> 48) & 0xFF);
174: b[start++] = (byte)((bits >>> 40) & 0xFF);
175: b[start++] = (byte)((bits >>> 32) & 0xFF);
176: b[start++] = (byte)((bits >>> 24) & 0xFF);
177: b[start++] = (byte)((bits >>> 16) & 0xFF);
178: b[start++] = (byte)((bits >>> 8) & 0xFF);
179: b[start++] = (byte)(bits & 0xFF);
180: }
181: }
182:
183:
184: public final void convertToCharactersFromDoubleArray(double[] fdata, StringBuffer s) {
185: final int end = fdata.length - 1;
186: for (int i = 0; i <= end; i++) {
187: s.append(Double.toString(fdata[i]));
188: if (i != end) {
189: s.append(' ');
190: }
191: }
192: }
193:
194:
195: public final double[] generateArrayFromList(List array) {
196: double[] fdata = new double[array.size()];
197: for (int i = 0; i < fdata.length; i++) {
198: fdata[i] = ((Double)array.get(i)).doubleValue();
199: }
200:
201: return fdata;
202: }
203:
204: }