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

changeset 286
f50545b5e2f1
child 384
8f2986ff0235
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/algorithm/BooleanEncodingAlgorithm.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,271 @@
     1.4 +/*
     1.5 + * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + *
    1.28 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    1.29 + */
    1.30 +
    1.31 +package com.sun.xml.internal.fastinfoset.algorithm;
    1.32 +
    1.33 +
    1.34 +import java.io.EOFException;
    1.35 +import java.io.IOException;
    1.36 +import java.io.InputStream;
    1.37 +import java.io.OutputStream;
    1.38 +import java.nio.CharBuffer;
    1.39 +import java.util.ArrayList;
    1.40 +import java.util.List;
    1.41 +
    1.42 +import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException;
    1.43 +import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
    1.44 +
    1.45 +
    1.46 +/**
    1.47 + *
    1.48 + * An encoder for handling boolean values.  Suppports the builtin BOOLEAN encoder.
    1.49 + *
    1.50 + * @author Alan Hudson
    1.51 + * @author Paul Sandoz
    1.52 + *
    1.53 + */
    1.54 +public class BooleanEncodingAlgorithm extends BuiltInEncodingAlgorithm {
    1.55 +
    1.56 +    /** Table for setting a particular bit of a byte */
    1.57 +    private static final int[] BIT_TABLE = {
    1.58 +        1 << 7,
    1.59 +        1 << 6,
    1.60 +        1 << 5,
    1.61 +        1 << 4,
    1.62 +        1 << 3,
    1.63 +        1 << 2,
    1.64 +        1 << 1,
    1.65 +        1 << 0};
    1.66 +
    1.67 +    public int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
    1.68 +        // Cannot determine the number of boolean values from just the octet length
    1.69 +        throw new UnsupportedOperationException();
    1.70 +    }
    1.71 +
    1.72 +    public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
    1.73 +        if (primitiveLength < 5) {
    1.74 +            return 1;
    1.75 +        } else {
    1.76 +            final int div = primitiveLength / 8;
    1.77 +            return (div == 0) ? 2 : 1 + div;
    1.78 +        }
    1.79 +    }
    1.80 +
    1.81 +    public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
    1.82 +        final int blength = getPrimtiveLengthFromOctetLength(length, b[start]);
    1.83 +        boolean[] data = new boolean[blength];
    1.84 +
    1.85 +        decodeFromBytesToBooleanArray(data, 0, blength, b, start, length);
    1.86 +        return data;
    1.87 +    }
    1.88 +
    1.89 +    public final Object decodeFromInputStream(InputStream s) throws IOException {
    1.90 +        final List booleanList = new ArrayList();
    1.91 +
    1.92 +        int value = s.read();
    1.93 +        if (value == -1) {
    1.94 +            throw new EOFException();
    1.95 +        }
    1.96 +        final int unusedBits = (value >> 4) & 0xFF;
    1.97 +
    1.98 +        int bitPosition = 4;
    1.99 +        int bitPositionEnd = 8;
   1.100 +        int valueNext = 0;
   1.101 +        do {
   1.102 +            valueNext = s.read();
   1.103 +            if (valueNext == -1) {
   1.104 +                bitPositionEnd -= unusedBits;
   1.105 +            }
   1.106 +
   1.107 +            while(bitPosition < bitPositionEnd) {
   1.108 +                booleanList.add(
   1.109 +                        Boolean.valueOf((value & BIT_TABLE[bitPosition++]) > 0));
   1.110 +            }
   1.111 +
   1.112 +            value = valueNext;
   1.113 +        } while (value != -1);
   1.114 +
   1.115 +        return generateArrayFromList(booleanList);
   1.116 +    }
   1.117 +
   1.118 +    public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
   1.119 +        if (!(data instanceof boolean[])) {
   1.120 +            throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotBoolean"));
   1.121 +        }
   1.122 +
   1.123 +        boolean array[] = (boolean[])data;
   1.124 +        final int alength = array.length;
   1.125 +
   1.126 +        final int mod = (alength + 4) % 8;
   1.127 +        final int unusedBits = (mod == 0) ? 0 : 8 - mod;
   1.128 +
   1.129 +        int bitPosition = 4;
   1.130 +        int value = unusedBits << 4;
   1.131 +        int astart = 0;
   1.132 +        while (astart < alength) {
   1.133 +            if (array[astart++]) {
   1.134 +                value |= BIT_TABLE[bitPosition];
   1.135 +            }
   1.136 +
   1.137 +            if (++bitPosition == 8) {
   1.138 +                s.write(value);
   1.139 +                bitPosition = value = 0;
   1.140 +            }
   1.141 +        }
   1.142 +
   1.143 +        if (bitPosition != 8) {
   1.144 +            s.write(value);
   1.145 +        }
   1.146 +    }
   1.147 +
   1.148 +    public final Object convertFromCharacters(char[] ch, int start, int length) {
   1.149 +        if (length == 0) {
   1.150 +            return new boolean[0];
   1.151 +        }
   1.152 +
   1.153 +        final CharBuffer cb = CharBuffer.wrap(ch, start, length);
   1.154 +        final List booleanList = new ArrayList();
   1.155 +
   1.156 +        matchWhiteSpaceDelimnatedWords(cb,
   1.157 +            new WordListener() {
   1.158 +                public void word(int start, int end) {
   1.159 +                    if (cb.charAt(start) == 't') {
   1.160 +                        booleanList.add(Boolean.TRUE);
   1.161 +                    } else {
   1.162 +                        booleanList.add(Boolean.FALSE);
   1.163 +                    }
   1.164 +                }
   1.165 +            }
   1.166 +        );
   1.167 +
   1.168 +        return generateArrayFromList(booleanList);
   1.169 +    }
   1.170 +
   1.171 +    public final void convertToCharacters(Object data, StringBuffer s) {
   1.172 +        if (data == null) {
   1.173 +            return;
   1.174 +        }
   1.175 +
   1.176 +        final boolean[] value = (boolean[]) data;
   1.177 +        if (value.length == 0) {
   1.178 +            return;
   1.179 +        }
   1.180 +
   1.181 +        // Insure conservately as all false
   1.182 +        s.ensureCapacity(value.length * 5);
   1.183 +
   1.184 +        final int end = value.length - 1;
   1.185 +        for (int i = 0; i <= end; i++) {
   1.186 +            if (value[i]) {
   1.187 +                s.append("true");
   1.188 +            } else {
   1.189 +                s.append("false");
   1.190 +            }
   1.191 +            if (i != end) {
   1.192 +                s.append(' ');
   1.193 +            }
   1.194 +        }
   1.195 +    }
   1.196 +
   1.197 +    public int getPrimtiveLengthFromOctetLength(int octetLength, int firstOctet) throws EncodingAlgorithmException {
   1.198 +        final int unusedBits = (firstOctet >> 4) & 0xFF;
   1.199 +        if (octetLength == 1) {
   1.200 +           if (unusedBits > 3) {
   1.201 +               throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits4"));
   1.202 +           }
   1.203 +           return 4 - unusedBits;
   1.204 +        } else {
   1.205 +           if (unusedBits > 7) {
   1.206 +               throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits8"));
   1.207 +           }
   1.208 +           return octetLength * 8 - 4 - unusedBits;
   1.209 +        }
   1.210 +    }
   1.211 +
   1.212 +    public final void decodeFromBytesToBooleanArray(boolean[] bdata, int bstart, int blength, byte[] b, int start, int length) {
   1.213 +        int value = b[start++] & 0xFF;
   1.214 +        int bitPosition = 4;
   1.215 +        final int bend = bstart + blength;
   1.216 +        while (bstart < bend) {
   1.217 +            if (bitPosition == 8) {
   1.218 +                value = b[start++] & 0xFF;
   1.219 +                bitPosition = 0;
   1.220 +            }
   1.221 +
   1.222 +            bdata[bstart++] = (value & BIT_TABLE[bitPosition++]) > 0;
   1.223 +        }
   1.224 +    }
   1.225 +
   1.226 +    public void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) {
   1.227 +        if (!(array instanceof boolean[])) {
   1.228 +            throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotBoolean"));
   1.229 +        }
   1.230 +
   1.231 +        encodeToBytesFromBooleanArray((boolean[])array, astart, alength, b, start);
   1.232 +    }
   1.233 +
   1.234 +    public void encodeToBytesFromBooleanArray(boolean[] array, int astart, int alength, byte[] b, int start) {
   1.235 +        final int mod = (alength + 4) % 8;
   1.236 +        final int unusedBits = (mod == 0) ? 0 : 8 - mod;
   1.237 +
   1.238 +        int bitPosition = 4;
   1.239 +        int value = unusedBits << 4;
   1.240 +        final int aend = astart + alength;
   1.241 +        while (astart < aend) {
   1.242 +            if (array[astart++]) {
   1.243 +                value |= BIT_TABLE[bitPosition];
   1.244 +            }
   1.245 +
   1.246 +            if (++bitPosition == 8) {
   1.247 +                b[start++] = (byte)value;
   1.248 +                bitPosition = value = 0;
   1.249 +            }
   1.250 +        }
   1.251 +
   1.252 +        if (bitPosition > 0) {
   1.253 +            b[start] = (byte)value;
   1.254 +        }
   1.255 +    }
   1.256 +
   1.257 +
   1.258 +    /**
   1.259 +     *
   1.260 +     * Generate a boolean array from a list of Booleans.
   1.261 +     *
   1.262 +     * @param array The array
   1.263 +     *
   1.264 +     */
   1.265 +    private final boolean[] generateArrayFromList(List array) {
   1.266 +        boolean[] bdata = new boolean[array.size()];
   1.267 +        for (int i = 0; i < bdata.length; i++) {
   1.268 +            bdata[i] = ((Boolean)array.get(i)).booleanValue();
   1.269 +        }
   1.270 +
   1.271 +        return bdata;
   1.272 +    }
   1.273 +
   1.274 +}

mercurial