src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/algorithm/BooleanEncodingAlgorithm.java

Wed, 12 Jun 2013 14:47:09 +0100

author
mkos
date
Wed, 12 Jun 2013 14:47:09 +0100
changeset 384
8f2986ff0235
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8013021: Rebase 8005432 & 8003542 against the latest jdk8/jaxws
8003542: Improve processing of MTOM attachments
8005432: Update access to JAX-WS
Reviewed-by: mullan

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

mercurial