Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | * |
aoqi@0 | 25 | * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
aoqi@0 | 26 | */ |
aoqi@0 | 27 | |
aoqi@0 | 28 | package com.sun.xml.internal.fastinfoset.algorithm; |
aoqi@0 | 29 | |
aoqi@0 | 30 | |
aoqi@0 | 31 | import java.io.EOFException; |
aoqi@0 | 32 | import java.io.IOException; |
aoqi@0 | 33 | import java.io.InputStream; |
aoqi@0 | 34 | import java.io.OutputStream; |
aoqi@0 | 35 | import java.nio.CharBuffer; |
aoqi@0 | 36 | import java.util.ArrayList; |
aoqi@0 | 37 | import java.util.List; |
aoqi@0 | 38 | |
aoqi@0 | 39 | import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException; |
aoqi@0 | 40 | import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
aoqi@0 | 41 | |
aoqi@0 | 42 | |
aoqi@0 | 43 | /** |
aoqi@0 | 44 | * |
aoqi@0 | 45 | * An encoder for handling boolean values. Suppports the builtin BOOLEAN encoder. |
aoqi@0 | 46 | * |
aoqi@0 | 47 | * @author Alan Hudson |
aoqi@0 | 48 | * @author Paul Sandoz |
aoqi@0 | 49 | * |
aoqi@0 | 50 | */ |
aoqi@0 | 51 | public class BooleanEncodingAlgorithm extends BuiltInEncodingAlgorithm { |
aoqi@0 | 52 | |
aoqi@0 | 53 | /** Table for setting a particular bit of a byte */ |
aoqi@0 | 54 | private static final int[] BIT_TABLE = { |
aoqi@0 | 55 | 1 << 7, |
aoqi@0 | 56 | 1 << 6, |
aoqi@0 | 57 | 1 << 5, |
aoqi@0 | 58 | 1 << 4, |
aoqi@0 | 59 | 1 << 3, |
aoqi@0 | 60 | 1 << 2, |
aoqi@0 | 61 | 1 << 1, |
aoqi@0 | 62 | 1 << 0}; |
aoqi@0 | 63 | |
aoqi@0 | 64 | public int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { |
aoqi@0 | 65 | // Cannot determine the number of boolean values from just the octet length |
aoqi@0 | 66 | throw new UnsupportedOperationException(); |
aoqi@0 | 67 | } |
aoqi@0 | 68 | |
aoqi@0 | 69 | public int getOctetLengthFromPrimitiveLength(int primitiveLength) { |
aoqi@0 | 70 | if (primitiveLength < 5) { |
aoqi@0 | 71 | return 1; |
aoqi@0 | 72 | } else { |
aoqi@0 | 73 | final int div = primitiveLength / 8; |
aoqi@0 | 74 | return (div == 0) ? 2 : 1 + div; |
aoqi@0 | 75 | } |
aoqi@0 | 76 | } |
aoqi@0 | 77 | |
aoqi@0 | 78 | public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException { |
aoqi@0 | 79 | final int blength = getPrimtiveLengthFromOctetLength(length, b[start]); |
aoqi@0 | 80 | boolean[] data = new boolean[blength]; |
aoqi@0 | 81 | |
aoqi@0 | 82 | decodeFromBytesToBooleanArray(data, 0, blength, b, start, length); |
aoqi@0 | 83 | return data; |
aoqi@0 | 84 | } |
aoqi@0 | 85 | |
aoqi@0 | 86 | public final Object decodeFromInputStream(InputStream s) throws IOException { |
aoqi@0 | 87 | final List booleanList = new ArrayList(); |
aoqi@0 | 88 | |
aoqi@0 | 89 | int value = s.read(); |
aoqi@0 | 90 | if (value == -1) { |
aoqi@0 | 91 | throw new EOFException(); |
aoqi@0 | 92 | } |
aoqi@0 | 93 | final int unusedBits = (value >> 4) & 0xFF; |
aoqi@0 | 94 | |
aoqi@0 | 95 | int bitPosition = 4; |
aoqi@0 | 96 | int bitPositionEnd = 8; |
aoqi@0 | 97 | int valueNext = 0; |
aoqi@0 | 98 | do { |
aoqi@0 | 99 | valueNext = s.read(); |
aoqi@0 | 100 | if (valueNext == -1) { |
aoqi@0 | 101 | bitPositionEnd -= unusedBits; |
aoqi@0 | 102 | } |
aoqi@0 | 103 | |
aoqi@0 | 104 | while(bitPosition < bitPositionEnd) { |
aoqi@0 | 105 | booleanList.add( |
aoqi@0 | 106 | Boolean.valueOf((value & BIT_TABLE[bitPosition++]) > 0)); |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | value = valueNext; |
aoqi@0 | 110 | } while (value != -1); |
aoqi@0 | 111 | |
aoqi@0 | 112 | return generateArrayFromList(booleanList); |
aoqi@0 | 113 | } |
aoqi@0 | 114 | |
aoqi@0 | 115 | public void encodeToOutputStream(Object data, OutputStream s) throws IOException { |
aoqi@0 | 116 | if (!(data instanceof boolean[])) { |
aoqi@0 | 117 | throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotBoolean")); |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | boolean array[] = (boolean[])data; |
aoqi@0 | 121 | final int alength = array.length; |
aoqi@0 | 122 | |
aoqi@0 | 123 | final int mod = (alength + 4) % 8; |
aoqi@0 | 124 | final int unusedBits = (mod == 0) ? 0 : 8 - mod; |
aoqi@0 | 125 | |
aoqi@0 | 126 | int bitPosition = 4; |
aoqi@0 | 127 | int value = unusedBits << 4; |
aoqi@0 | 128 | int astart = 0; |
aoqi@0 | 129 | while (astart < alength) { |
aoqi@0 | 130 | if (array[astart++]) { |
aoqi@0 | 131 | value |= BIT_TABLE[bitPosition]; |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | if (++bitPosition == 8) { |
aoqi@0 | 135 | s.write(value); |
aoqi@0 | 136 | bitPosition = value = 0; |
aoqi@0 | 137 | } |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | if (bitPosition != 8) { |
aoqi@0 | 141 | s.write(value); |
aoqi@0 | 142 | } |
aoqi@0 | 143 | } |
aoqi@0 | 144 | |
aoqi@0 | 145 | public final Object convertFromCharacters(char[] ch, int start, int length) { |
aoqi@0 | 146 | if (length == 0) { |
aoqi@0 | 147 | return new boolean[0]; |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | final CharBuffer cb = CharBuffer.wrap(ch, start, length); |
aoqi@0 | 151 | final List booleanList = new ArrayList(); |
aoqi@0 | 152 | |
aoqi@0 | 153 | matchWhiteSpaceDelimnatedWords(cb, |
aoqi@0 | 154 | new WordListener() { |
aoqi@0 | 155 | public void word(int start, int end) { |
aoqi@0 | 156 | if (cb.charAt(start) == 't') { |
aoqi@0 | 157 | booleanList.add(Boolean.TRUE); |
aoqi@0 | 158 | } else { |
aoqi@0 | 159 | booleanList.add(Boolean.FALSE); |
aoqi@0 | 160 | } |
aoqi@0 | 161 | } |
aoqi@0 | 162 | } |
aoqi@0 | 163 | ); |
aoqi@0 | 164 | |
aoqi@0 | 165 | return generateArrayFromList(booleanList); |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | public final void convertToCharacters(Object data, StringBuffer s) { |
aoqi@0 | 169 | if (data == null) { |
aoqi@0 | 170 | return; |
aoqi@0 | 171 | } |
aoqi@0 | 172 | |
aoqi@0 | 173 | final boolean[] value = (boolean[]) data; |
aoqi@0 | 174 | if (value.length == 0) { |
aoqi@0 | 175 | return; |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | // Insure conservately as all false |
aoqi@0 | 179 | s.ensureCapacity(value.length * 5); |
aoqi@0 | 180 | |
aoqi@0 | 181 | final int end = value.length - 1; |
aoqi@0 | 182 | for (int i = 0; i <= end; i++) { |
aoqi@0 | 183 | if (value[i]) { |
aoqi@0 | 184 | s.append("true"); |
aoqi@0 | 185 | } else { |
aoqi@0 | 186 | s.append("false"); |
aoqi@0 | 187 | } |
aoqi@0 | 188 | if (i != end) { |
aoqi@0 | 189 | s.append(' '); |
aoqi@0 | 190 | } |
aoqi@0 | 191 | } |
aoqi@0 | 192 | } |
aoqi@0 | 193 | |
aoqi@0 | 194 | public int getPrimtiveLengthFromOctetLength(int octetLength, int firstOctet) throws EncodingAlgorithmException { |
aoqi@0 | 195 | final int unusedBits = (firstOctet >> 4) & 0xFF; |
aoqi@0 | 196 | if (octetLength == 1) { |
aoqi@0 | 197 | if (unusedBits > 3) { |
aoqi@0 | 198 | throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits4")); |
aoqi@0 | 199 | } |
aoqi@0 | 200 | return 4 - unusedBits; |
aoqi@0 | 201 | } else { |
aoqi@0 | 202 | if (unusedBits > 7) { |
aoqi@0 | 203 | throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits8")); |
aoqi@0 | 204 | } |
aoqi@0 | 205 | return octetLength * 8 - 4 - unusedBits; |
aoqi@0 | 206 | } |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | public final void decodeFromBytesToBooleanArray(boolean[] bdata, int bstart, int blength, byte[] b, int start, int length) { |
aoqi@0 | 210 | int value = b[start++] & 0xFF; |
aoqi@0 | 211 | int bitPosition = 4; |
aoqi@0 | 212 | final int bend = bstart + blength; |
aoqi@0 | 213 | while (bstart < bend) { |
aoqi@0 | 214 | if (bitPosition == 8) { |
aoqi@0 | 215 | value = b[start++] & 0xFF; |
aoqi@0 | 216 | bitPosition = 0; |
aoqi@0 | 217 | } |
aoqi@0 | 218 | |
aoqi@0 | 219 | bdata[bstart++] = (value & BIT_TABLE[bitPosition++]) > 0; |
aoqi@0 | 220 | } |
aoqi@0 | 221 | } |
aoqi@0 | 222 | |
aoqi@0 | 223 | public void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) { |
aoqi@0 | 224 | if (!(array instanceof boolean[])) { |
aoqi@0 | 225 | throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotBoolean")); |
aoqi@0 | 226 | } |
aoqi@0 | 227 | |
aoqi@0 | 228 | encodeToBytesFromBooleanArray((boolean[])array, astart, alength, b, start); |
aoqi@0 | 229 | } |
aoqi@0 | 230 | |
aoqi@0 | 231 | public void encodeToBytesFromBooleanArray(boolean[] array, int astart, int alength, byte[] b, int start) { |
aoqi@0 | 232 | final int mod = (alength + 4) % 8; |
aoqi@0 | 233 | final int unusedBits = (mod == 0) ? 0 : 8 - mod; |
aoqi@0 | 234 | |
aoqi@0 | 235 | int bitPosition = 4; |
aoqi@0 | 236 | int value = unusedBits << 4; |
aoqi@0 | 237 | final int aend = astart + alength; |
aoqi@0 | 238 | while (astart < aend) { |
aoqi@0 | 239 | if (array[astart++]) { |
aoqi@0 | 240 | value |= BIT_TABLE[bitPosition]; |
aoqi@0 | 241 | } |
aoqi@0 | 242 | |
aoqi@0 | 243 | if (++bitPosition == 8) { |
aoqi@0 | 244 | b[start++] = (byte)value; |
aoqi@0 | 245 | bitPosition = value = 0; |
aoqi@0 | 246 | } |
aoqi@0 | 247 | } |
aoqi@0 | 248 | |
aoqi@0 | 249 | if (bitPosition > 0) { |
aoqi@0 | 250 | b[start] = (byte)value; |
aoqi@0 | 251 | } |
aoqi@0 | 252 | } |
aoqi@0 | 253 | |
aoqi@0 | 254 | |
aoqi@0 | 255 | /** |
aoqi@0 | 256 | * |
aoqi@0 | 257 | * Generate a boolean array from a list of Booleans. |
aoqi@0 | 258 | * |
aoqi@0 | 259 | * @param array The array |
aoqi@0 | 260 | * |
aoqi@0 | 261 | */ |
aoqi@0 | 262 | private boolean[] generateArrayFromList(List array) { |
aoqi@0 | 263 | boolean[] bdata = new boolean[array.size()]; |
aoqi@0 | 264 | for (int i = 0; i < bdata.length; i++) { |
aoqi@0 | 265 | bdata[i] = ((Boolean)array.get(i)).booleanValue(); |
aoqi@0 | 266 | } |
aoqi@0 | 267 | |
aoqi@0 | 268 | return bdata; |
aoqi@0 | 269 | } |
aoqi@0 | 270 | |
aoqi@0 | 271 | } |