Skip to content

Package: BooleanEncodingAlgorithm$1

BooleanEncodingAlgorithm$1

nameinstructionbranchcomplexitylinemethod
word(int, int)
M: 18 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 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:
22: import java.io.EOFException;
23: import java.io.IOException;
24: import java.io.InputStream;
25: import java.io.OutputStream;
26: import java.nio.CharBuffer;
27: import java.util.ArrayList;
28: import java.util.List;
29:
30: import org.jvnet.fastinfoset.EncodingAlgorithmException;
31: import com.sun.xml.fastinfoset.CommonResourceBundle;
32:
33:
34: /**
35: *
36: * An encoder for handling boolean values. Suppports the builtin BOOLEAN encoder.
37: *
38: * @author Alan Hudson
39: * @author Paul Sandoz
40: *
41: */
42: public class BooleanEncodingAlgorithm extends BuiltInEncodingAlgorithm {
43:
44: /** Table for setting a particular bit of a byte */
45: private static final int[] BIT_TABLE = {
46: 1 << 7,
47: 1 << 6,
48: 1 << 5,
49: 1 << 4,
50: 1 << 3,
51: 1 << 2,
52: 1 << 1,
53: 1 << 0};
54:
55: public int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
56: // Cannot determine the number of boolean values from just the octet length
57: throw new UnsupportedOperationException();
58: }
59:
60: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
61: if (primitiveLength < 5) {
62: return 1;
63: } else {
64: final int div = primitiveLength / 8;
65: return (div == 0) ? 2 : 1 + div;
66: }
67: }
68:
69: public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
70: final int blength = getPrimtiveLengthFromOctetLength(length, b[start]);
71: boolean[] data = new boolean[blength];
72:
73: decodeFromBytesToBooleanArray(data, 0, blength, b, start, length);
74: return data;
75: }
76:
77: public final Object decodeFromInputStream(InputStream s) throws IOException {
78: final List booleanList = new ArrayList();
79:
80: int value = s.read();
81: if (value == -1) {
82: throw new EOFException();
83: }
84: final int unusedBits = (value >> 4) & 0xFF;
85:
86: int bitPosition = 4;
87: int bitPositionEnd = 8;
88: int valueNext = 0;
89: do {
90: valueNext = s.read();
91: if (valueNext == -1) {
92: bitPositionEnd -= unusedBits;
93: }
94:
95: while(bitPosition < bitPositionEnd) {
96: booleanList.add(
97: Boolean.valueOf((value & BIT_TABLE[bitPosition++]) > 0));
98: }
99:
100: value = valueNext;
101: } while (value != -1);
102:
103: return generateArrayFromList(booleanList);
104: }
105:
106: public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
107: if (!(data instanceof boolean[])) {
108: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotBoolean"));
109: }
110:
111: boolean array[] = (boolean[])data;
112: final int alength = array.length;
113:
114: final int mod = (alength + 4) % 8;
115: final int unusedBits = (mod == 0) ? 0 : 8 - mod;
116:
117: int bitPosition = 4;
118: int value = unusedBits << 4;
119: int astart = 0;
120: while (astart < alength) {
121: if (array[astart++]) {
122: value |= BIT_TABLE[bitPosition];
123: }
124:
125: if (++bitPosition == 8) {
126: s.write(value);
127: bitPosition = value = 0;
128: }
129: }
130:
131: if (bitPosition != 8) {
132: s.write(value);
133: }
134: }
135:
136: public final Object convertFromCharacters(char[] ch, int start, int length) {
137: if (length == 0) {
138: return new boolean[0];
139: }
140:
141: final CharBuffer cb = CharBuffer.wrap(ch, start, length);
142: final List booleanList = new ArrayList();
143:
144: matchWhiteSpaceDelimnatedWords(cb,
145: new WordListener() {
146: public void word(int start, int end) {
147:• if (cb.charAt(start) == 't') {
148: booleanList.add(Boolean.TRUE);
149: } else {
150: booleanList.add(Boolean.FALSE);
151: }
152: }
153: }
154: );
155:
156: return generateArrayFromList(booleanList);
157: }
158:
159: public final void convertToCharacters(Object data, StringBuffer s) {
160: if (data == null) {
161: return;
162: }
163:
164: final boolean[] value = (boolean[]) data;
165: if (value.length == 0) {
166: return;
167: }
168:
169: // Insure conservately as all false
170: s.ensureCapacity(value.length * 5);
171:
172: final int end = value.length - 1;
173: for (int i = 0; i <= end; i++) {
174: if (value[i]) {
175: s.append("true");
176: } else {
177: s.append("false");
178: }
179: if (i != end) {
180: s.append(' ');
181: }
182: }
183: }
184:
185: public int getPrimtiveLengthFromOctetLength(int octetLength, int firstOctet) throws EncodingAlgorithmException {
186: final int unusedBits = (firstOctet >> 4) & 0xFF;
187: if (octetLength == 1) {
188: if (unusedBits > 3) {
189: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits4"));
190: }
191: return 4 - unusedBits;
192: } else {
193: if (unusedBits > 7) {
194: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits8"));
195: }
196: return octetLength * 8 - 4 - unusedBits;
197: }
198: }
199:
200: public final void decodeFromBytesToBooleanArray(boolean[] bdata, int bstart, int blength, byte[] b, int start, int length) {
201: int value = b[start++] & 0xFF;
202: int bitPosition = 4;
203: final int bend = bstart + blength;
204: while (bstart < bend) {
205: if (bitPosition == 8) {
206: value = b[start++] & 0xFF;
207: bitPosition = 0;
208: }
209:
210: bdata[bstart++] = (value & BIT_TABLE[bitPosition++]) > 0;
211: }
212: }
213:
214: public void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) {
215: if (!(array instanceof boolean[])) {
216: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotBoolean"));
217: }
218:
219: encodeToBytesFromBooleanArray((boolean[])array, astart, alength, b, start);
220: }
221:
222: public void encodeToBytesFromBooleanArray(boolean[] array, int astart, int alength, byte[] b, int start) {
223: final int mod = (alength + 4) % 8;
224: final int unusedBits = (mod == 0) ? 0 : 8 - mod;
225:
226: int bitPosition = 4;
227: int value = unusedBits << 4;
228: final int aend = astart + alength;
229: while (astart < aend) {
230: if (array[astart++]) {
231: value |= BIT_TABLE[bitPosition];
232: }
233:
234: if (++bitPosition == 8) {
235: b[start++] = (byte)value;
236: bitPosition = value = 0;
237: }
238: }
239:
240: if (bitPosition > 0) {
241: b[start] = (byte)value;
242: }
243: }
244:
245:
246: /**
247: *
248: * Generate a boolean array from a list of Booleans.
249: *
250: * @param array The array
251: *
252: */
253: private boolean[] generateArrayFromList(List array) {
254: boolean[] bdata = new boolean[array.size()];
255: for (int i = 0; i < bdata.length; i++) {
256: bdata[i] = ((Boolean)array.get(i)).booleanValue();
257: }
258:
259: return bdata;
260: }
261:
262: }
263: