aboutsummaryrefslogtreecommitdiff
path: root/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/algorithm/BooleanEncodingAlgorithm.java
blob: f50bd8d54c0d6212453f7dd409eeab8e55ba3741 (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
 */

package com.sun.xml.internal.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 com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException;
import com.sun.xml.internal.fastinfoset.CommonResourceBundle;


/**
 *
 * An encoder for handling boolean values.  Suppports the builtin BOOLEAN encoder.
 *
 * @author Alan Hudson
 * @author Paul Sandoz
 *
 */
public class BooleanEncodingAlgorithm extends BuiltInEncodingAlgorithm {

    /** Table for setting a particular bit of a byte */
    private static final int[] BIT_TABLE = {
        1 << 7,
        1 << 6,
        1 << 5,
        1 << 4,
        1 << 3,
        1 << 2,
        1 << 1,
        1 << 0};

    public int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
        // Cannot determine the number of boolean values from just the octet length
        throw new UnsupportedOperationException();
    }

    public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
        if (primitiveLength < 5) {
            return 1;
        } else {
            final int div = primitiveLength / 8;
            return (div == 0) ? 2 : 1 + div;
        }
    }

    public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
        final int blength = getPrimtiveLengthFromOctetLength(length, b[start]);
        boolean[] data = new boolean[blength];

        decodeFromBytesToBooleanArray(data, 0, blength, b, start, length);
        return data;
    }

    public final Object decodeFromInputStream(InputStream s) throws IOException {
        final List booleanList = new ArrayList();

        int value = s.read();
        if (value == -1) {
            throw new EOFException();
        }
        final int unusedBits = (value >> 4) & 0xFF;

        int bitPosition = 4;
        int bitPositionEnd = 8;
        int valueNext = 0;
        do {
            valueNext = s.read();
            if (valueNext == -1) {
                bitPositionEnd -= unusedBits;
            }

            while(bitPosition < bitPositionEnd) {
                booleanList.add(
                        Boolean.valueOf((value & BIT_TABLE[bitPosition++]) > 0));
            }

            value = valueNext;
        } while (value != -1);

        return generateArrayFromList(booleanList);
    }

    public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
        if (!(data instanceof boolean[])) {
            throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotBoolean"));
        }

        boolean array[] = (boolean[])data;
        final int alength = array.length;

        final int mod = (alength + 4) % 8;
        final int unusedBits = (mod == 0) ? 0 : 8 - mod;

        int bitPosition = 4;
        int value = unusedBits << 4;
        int astart = 0;
        while (astart < alength) {
            if (array[astart++]) {
                value |= BIT_TABLE[bitPosition];
            }

            if (++bitPosition == 8) {
                s.write(value);
                bitPosition = value = 0;
            }
        }

        if (bitPosition != 8) {
            s.write(value);
        }
    }

    public final Object convertFromCharacters(char[] ch, int start, int length) {
        if (length == 0) {
            return new boolean[0];
        }

        final CharBuffer cb = CharBuffer.wrap(ch, start, length);
        final List booleanList = new ArrayList();

        matchWhiteSpaceDelimnatedWords(cb,
            new WordListener() {
                public void word(int start, int end) {
                    if (cb.charAt(start) == 't') {
                        booleanList.add(Boolean.TRUE);
                    } else {
                        booleanList.add(Boolean.FALSE);
                    }
                }
            }
        );

        return generateArrayFromList(booleanList);
    }

    public final void convertToCharacters(Object data, StringBuffer s) {
        if (data == null) {
            return;
        }

        final boolean[] value = (boolean[]) data;
        if (value.length == 0) {
            return;
        }

        // Insure conservately as all false
        s.ensureCapacity(value.length * 5);

        final int end = value.length - 1;
        for (int i = 0; i <= end; i++) {
            if (value[i]) {
                s.append("true");
            } else {
                s.append("false");
            }
            if (i != end) {
                s.append(' ');
            }
        }
    }

    public int getPrimtiveLengthFromOctetLength(int octetLength, int firstOctet) throws EncodingAlgorithmException {
        final int unusedBits = (firstOctet >> 4) & 0xFF;
        if (octetLength == 1) {
           if (unusedBits > 3) {
               throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits4"));
           }
           return 4 - unusedBits;
        } else {
           if (unusedBits > 7) {
               throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits8"));
           }
           return octetLength * 8 - 4 - unusedBits;
        }
    }

    public final void decodeFromBytesToBooleanArray(boolean[] bdata, int bstart, int blength, byte[] b, int start, int length) {
        int value = b[start++] & 0xFF;
        int bitPosition = 4;
        final int bend = bstart + blength;
        while (bstart < bend) {
            if (bitPosition == 8) {
                value = b[start++] & 0xFF;
                bitPosition = 0;
            }

            bdata[bstart++] = (value & BIT_TABLE[bitPosition++]) > 0;
        }
    }

    public void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) {
        if (!(array instanceof boolean[])) {
            throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotBoolean"));
        }

        encodeToBytesFromBooleanArray((boolean[])array, astart, alength, b, start);
    }

    public void encodeToBytesFromBooleanArray(boolean[] array, int astart, int alength, byte[] b, int start) {
        final int mod = (alength + 4) % 8;
        final int unusedBits = (mod == 0) ? 0 : 8 - mod;

        int bitPosition = 4;
        int value = unusedBits << 4;
        final int aend = astart + alength;
        while (astart < aend) {
            if (array[astart++]) {
                value |= BIT_TABLE[bitPosition];
            }

            if (++bitPosition == 8) {
                b[start++] = (byte)value;
                bitPosition = value = 0;
            }
        }

        if (bitPosition > 0) {
            b[start] = (byte)value;
        }
    }


    /**
     *
     * Generate a boolean array from a list of Booleans.
     *
     * @param array The array
     *
     */
    private boolean[] generateArrayFromList(List array) {
        boolean[] bdata = new boolean[array.size()];
        for (int i = 0; i < bdata.length; i++) {
            bdata[i] = ((Boolean)array.get(i)).booleanValue();
        }

        return bdata;
    }

}