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

changeset 0
373ffda63c9a
     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/ShortEncodingAlgorithm.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,190 @@
     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 +import java.io.EOFException;
    1.34 +import java.io.IOException;
    1.35 +import java.io.InputStream;
    1.36 +import java.io.OutputStream;
    1.37 +import java.nio.CharBuffer;
    1.38 +import java.util.ArrayList;
    1.39 +import java.util.List;
    1.40 +import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException;
    1.41 +import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
    1.42 +
    1.43 +
    1.44 +/**
    1.45 + * An encoder for handling Short values.  Suppports the builtin SHORT encoder.
    1.46 + *
    1.47 + * @author Alan Hudson
    1.48 + * @author Paul Sandoz
    1.49 + */
    1.50 +public class ShortEncodingAlgorithm extends IntegerEncodingAlgorithm {
    1.51 +
    1.52 +    public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
    1.53 +        if (octetLength % SHORT_SIZE != 0) {
    1.54 +            throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
    1.55 +                    getString("message.lengthNotMultipleOfShort", new Object[]{Integer.valueOf(SHORT_SIZE)}));
    1.56 +        }
    1.57 +
    1.58 +        return octetLength / SHORT_SIZE;
    1.59 +    }
    1.60 +
    1.61 +    public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
    1.62 +        return primitiveLength * SHORT_SIZE;
    1.63 +    }
    1.64 +
    1.65 +    public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
    1.66 +        short[] data = new short[getPrimtiveLengthFromOctetLength(length)];
    1.67 +        decodeFromBytesToShortArray(data, 0, b, start, length);
    1.68 +
    1.69 +        return data;
    1.70 +    }
    1.71 +
    1.72 +    public final Object decodeFromInputStream(InputStream s) throws IOException {
    1.73 +        return decodeFromInputStreamToShortArray(s);
    1.74 +    }
    1.75 +
    1.76 +
    1.77 +    public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
    1.78 +        if (!(data instanceof short[])) {
    1.79 +            throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotShortArray"));
    1.80 +        }
    1.81 +
    1.82 +        final short[] idata = (short[])data;
    1.83 +
    1.84 +        encodeToOutputStreamFromShortArray(idata, s);
    1.85 +    }
    1.86 +
    1.87 +
    1.88 +    public final Object convertFromCharacters(char[] ch, int start, int length) {
    1.89 +        final CharBuffer cb = CharBuffer.wrap(ch, start, length);
    1.90 +        final List shortList = new ArrayList();
    1.91 +
    1.92 +        matchWhiteSpaceDelimnatedWords(cb,
    1.93 +                new WordListener() {
    1.94 +            public void word(int start, int end) {
    1.95 +                String iStringValue = cb.subSequence(start, end).toString();
    1.96 +                shortList.add(Short.valueOf(iStringValue));
    1.97 +            }
    1.98 +        }
    1.99 +        );
   1.100 +
   1.101 +        return generateArrayFromList(shortList);
   1.102 +    }
   1.103 +
   1.104 +    public final void convertToCharacters(Object data, StringBuffer s) {
   1.105 +        if (!(data instanceof short[])) {
   1.106 +            throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotShortArray"));
   1.107 +        }
   1.108 +
   1.109 +        final short[] idata = (short[])data;
   1.110 +
   1.111 +        convertToCharactersFromShortArray(idata, s);
   1.112 +    }
   1.113 +
   1.114 +
   1.115 +    public final void decodeFromBytesToShortArray(short[] sdata, int istart, byte[] b, int start, int length) {
   1.116 +        final int size = length / SHORT_SIZE;
   1.117 +        for (int i = 0; i < size; i++) {
   1.118 +            sdata[istart++] = (short) (((b[start++] & 0xFF) << 8) |
   1.119 +                    (b[start++] & 0xFF));
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +    public final short[] decodeFromInputStreamToShortArray(InputStream s) throws IOException {
   1.124 +        final List shortList = new ArrayList();
   1.125 +        final byte[] b = new byte[SHORT_SIZE];
   1.126 +
   1.127 +        while (true) {
   1.128 +            int n = s.read(b);
   1.129 +            if (n != 2) {
   1.130 +                if (n == -1) {
   1.131 +                    break;
   1.132 +                }
   1.133 +
   1.134 +                while(n != 2) {
   1.135 +                    final int m = s.read(b, n, SHORT_SIZE - n);
   1.136 +                    if (m == -1) {
   1.137 +                        throw new EOFException();
   1.138 +                    }
   1.139 +                    n += m;
   1.140 +                }
   1.141 +            }
   1.142 +
   1.143 +            final int i = ((b[0] & 0xFF) << 8) |
   1.144 +                    (b[1] & 0xFF);
   1.145 +            shortList.add(Short.valueOf((short)i));
   1.146 +        }
   1.147 +
   1.148 +        return generateArrayFromList(shortList);
   1.149 +    }
   1.150 +
   1.151 +
   1.152 +    public final void encodeToOutputStreamFromShortArray(short[] idata, OutputStream s) throws IOException {
   1.153 +        for (int i = 0; i < idata.length; i++) {
   1.154 +            final int bits = idata[i];
   1.155 +            s.write((bits >>> 8) & 0xFF);
   1.156 +            s.write(bits & 0xFF);
   1.157 +        }
   1.158 +    }
   1.159 +
   1.160 +    public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) {
   1.161 +        encodeToBytesFromShortArray((short[])array, astart, alength, b, start);
   1.162 +    }
   1.163 +
   1.164 +    public final void encodeToBytesFromShortArray(short[] sdata, int istart, int ilength, byte[] b, int start) {
   1.165 +        final int iend = istart + ilength;
   1.166 +        for (int i = istart; i < iend; i++) {
   1.167 +            final short bits = sdata[i];
   1.168 +            b[start++] = (byte)((bits >>> 8) & 0xFF);
   1.169 +            b[start++] = (byte)(bits & 0xFF);
   1.170 +        }
   1.171 +    }
   1.172 +
   1.173 +
   1.174 +    public final void convertToCharactersFromShortArray(short[] sdata, StringBuffer s) {
   1.175 +        final int end = sdata.length - 1;
   1.176 +        for (int i = 0; i <= end; i++) {
   1.177 +            s.append(Short.toString(sdata[i]));
   1.178 +            if (i != end) {
   1.179 +                s.append(' ');
   1.180 +            }
   1.181 +        }
   1.182 +    }
   1.183 +
   1.184 +
   1.185 +    public final short[] generateArrayFromList(List array) {
   1.186 +        short[] sdata = new short[array.size()];
   1.187 +        for (int i = 0; i < sdata.length; i++) {
   1.188 +            sdata[i] = ((Short)array.get(i)).shortValue();
   1.189 +        }
   1.190 +
   1.191 +        return sdata;
   1.192 +    }
   1.193 +}

mercurial