Skip to content

Package: ShortEncodingAlgorithm

ShortEncodingAlgorithm

nameinstructionbranchcomplexitylinemethod
ShortEncodingAlgorithm()
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%
convertToCharactersFromShortArray(short[], 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%
decodeFromBytesToShortArray(short[], int, byte[], int, int)
M: 0 C: 32
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
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%
decodeFromInputStreamToShortArray(InputStream)
M: 66 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%
encodeToBytesFromShortArray(short[], int, int, byte[], int)
M: 0 C: 34
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
encodeToOutputStream(Object, OutputStream)
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%
encodeToOutputStreamFromShortArray(short[], OutputStream)
M: 25 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
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: * An encoder for handling Short values. Suppports the builtin SHORT encoder.
34: *
35: * @author Alan Hudson
36: * @author Paul Sandoz
37: */
38: public class ShortEncodingAlgorithm extends IntegerEncodingAlgorithm {
39:
40: public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
41:• if (octetLength % SHORT_SIZE != 0) {
42: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
43: getString("message.lengthNotMultipleOfShort", new Object[]{Integer.valueOf(SHORT_SIZE)}));
44: }
45:
46: return octetLength / SHORT_SIZE;
47: }
48:
49: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
50: return primitiveLength * SHORT_SIZE;
51: }
52:
53: public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
54: short[] data = new short[getPrimtiveLengthFromOctetLength(length)];
55: decodeFromBytesToShortArray(data, 0, b, start, length);
56:
57: return data;
58: }
59:
60: public final Object decodeFromInputStream(InputStream s) throws IOException {
61: return decodeFromInputStreamToShortArray(s);
62: }
63:
64:
65: public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
66:• if (!(data instanceof short[])) {
67: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotShortArray"));
68: }
69:
70: final short[] idata = (short[])data;
71:
72: encodeToOutputStreamFromShortArray(idata, s);
73: }
74:
75:
76: public final Object convertFromCharacters(char[] ch, int start, int length) {
77: final CharBuffer cb = CharBuffer.wrap(ch, start, length);
78: final List shortList = new ArrayList();
79:
80: matchWhiteSpaceDelimnatedWords(cb,
81: new WordListener() {
82: public void word(int start, int end) {
83: String iStringValue = cb.subSequence(start, end).toString();
84: shortList.add(Short.valueOf(iStringValue));
85: }
86: }
87: );
88:
89: return generateArrayFromList(shortList);
90: }
91:
92: public final void convertToCharacters(Object data, StringBuffer s) {
93:• if (!(data instanceof short[])) {
94: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotShortArray"));
95: }
96:
97: final short[] idata = (short[])data;
98:
99: convertToCharactersFromShortArray(idata, s);
100: }
101:
102:
103: public final void decodeFromBytesToShortArray(short[] sdata, int istart, byte[] b, int start, int length) {
104: final int size = length / SHORT_SIZE;
105:• for (int i = 0; i < size; i++) {
106: sdata[istart++] = (short) (((b[start++] & 0xFF) << 8) |
107: (b[start++] & 0xFF));
108: }
109: }
110:
111: public final short[] decodeFromInputStreamToShortArray(InputStream s) throws IOException {
112: final List shortList = new ArrayList();
113: final byte[] b = new byte[SHORT_SIZE];
114:
115: while (true) {
116: int n = s.read(b);
117:• if (n != 2) {
118:• if (n == -1) {
119: break;
120: }
121:
122:• while(n != 2) {
123: final int m = s.read(b, n, SHORT_SIZE - n);
124:• if (m == -1) {
125: throw new EOFException();
126: }
127: n += m;
128: }
129: }
130:
131: final int i = ((b[0] & 0xFF) << 8) |
132: (b[1] & 0xFF);
133: shortList.add(Short.valueOf((short)i));
134: }
135:
136: return generateArrayFromList(shortList);
137: }
138:
139:
140: public final void encodeToOutputStreamFromShortArray(short[] idata, OutputStream s) throws IOException {
141:• for (int i = 0; i < idata.length; i++) {
142: final int bits = idata[i];
143: s.write((bits >>> 8) & 0xFF);
144: s.write(bits & 0xFF);
145: }
146: }
147:
148: public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) {
149: encodeToBytesFromShortArray((short[])array, astart, alength, b, start);
150: }
151:
152: public final void encodeToBytesFromShortArray(short[] sdata, int istart, int ilength, byte[] b, int start) {
153: final int iend = istart + ilength;
154:• for (int i = istart; i < iend; i++) {
155: final short bits = sdata[i];
156: b[start++] = (byte)((bits >>> 8) & 0xFF);
157: b[start++] = (byte)(bits & 0xFF);
158: }
159: }
160:
161:
162: public final void convertToCharactersFromShortArray(short[] sdata, StringBuffer s) {
163: final int end = sdata.length - 1;
164:• for (int i = 0; i <= end; i++) {
165: s.append(Short.toString(sdata[i]));
166:• if (i != end) {
167: s.append(' ');
168: }
169: }
170: }
171:
172:
173: public final short[] generateArrayFromList(List array) {
174: short[] sdata = new short[array.size()];
175:• for (int i = 0; i < sdata.length; i++) {
176: sdata[i] = ((Short)array.get(i)).shortValue();
177: }
178:
179: return sdata;
180: }
181: }