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/IntEncodingAlgorithm.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,193 @@ 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 +public class IntEncodingAlgorithm extends IntegerEncodingAlgorithm { 1.46 + 1.47 + public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { 1.48 + if (octetLength % INT_SIZE != 0) { 1.49 + throw new EncodingAlgorithmException(CommonResourceBundle.getInstance(). 1.50 + getString("message.lengthNotMultipleOfInt", new Object[]{Integer.valueOf(INT_SIZE)})); 1.51 + } 1.52 + 1.53 + return octetLength / INT_SIZE; 1.54 + } 1.55 + 1.56 + public int getOctetLengthFromPrimitiveLength(int primitiveLength) { 1.57 + return primitiveLength * INT_SIZE; 1.58 + } 1.59 + 1.60 + public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException { 1.61 + int[] data = new int[getPrimtiveLengthFromOctetLength(length)]; 1.62 + decodeFromBytesToIntArray(data, 0, b, start, length); 1.63 + 1.64 + return data; 1.65 + } 1.66 + 1.67 + public final Object decodeFromInputStream(InputStream s) throws IOException { 1.68 + return decodeFromInputStreamToIntArray(s); 1.69 + } 1.70 + 1.71 + 1.72 + public void encodeToOutputStream(Object data, OutputStream s) throws IOException { 1.73 + if (!(data instanceof int[])) { 1.74 + throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotIntArray")); 1.75 + } 1.76 + 1.77 + final int[] idata = (int[])data; 1.78 + 1.79 + encodeToOutputStreamFromIntArray(idata, s); 1.80 + } 1.81 + 1.82 + 1.83 + public final Object convertFromCharacters(char[] ch, int start, int length) { 1.84 + final CharBuffer cb = CharBuffer.wrap(ch, start, length); 1.85 + final List integerList = new ArrayList(); 1.86 + 1.87 + matchWhiteSpaceDelimnatedWords(cb, 1.88 + new WordListener() { 1.89 + public void word(int start, int end) { 1.90 + String iStringValue = cb.subSequence(start, end).toString(); 1.91 + integerList.add(Integer.valueOf(iStringValue)); 1.92 + } 1.93 + } 1.94 + ); 1.95 + 1.96 + return generateArrayFromList(integerList); 1.97 + } 1.98 + 1.99 + public final void convertToCharacters(Object data, StringBuffer s) { 1.100 + if (!(data instanceof int[])) { 1.101 + throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotIntArray")); 1.102 + } 1.103 + 1.104 + final int[] idata = (int[])data; 1.105 + 1.106 + convertToCharactersFromIntArray(idata, s); 1.107 + } 1.108 + 1.109 + 1.110 + public final void decodeFromBytesToIntArray(int[] idata, int istart, byte[] b, int start, int length) { 1.111 + final int size = length / INT_SIZE; 1.112 + for (int i = 0; i < size; i++) { 1.113 + idata[istart++] = ((b[start++] & 0xFF) << 24) | 1.114 + ((b[start++] & 0xFF) << 16) | 1.115 + ((b[start++] & 0xFF) << 8) | 1.116 + (b[start++] & 0xFF); 1.117 + } 1.118 + } 1.119 + 1.120 + public final int[] decodeFromInputStreamToIntArray(InputStream s) throws IOException { 1.121 + final List integerList = new ArrayList(); 1.122 + final byte[] b = new byte[INT_SIZE]; 1.123 + 1.124 + while (true) { 1.125 + int n = s.read(b); 1.126 + if (n != 4) { 1.127 + if (n == -1) { 1.128 + break; 1.129 + } 1.130 + 1.131 + while(n != 4) { 1.132 + final int m = s.read(b, n, INT_SIZE - n); 1.133 + if (m == -1) { 1.134 + throw new EOFException(); 1.135 + } 1.136 + n += m; 1.137 + } 1.138 + } 1.139 + 1.140 + final int i = ((b[0] & 0xFF) << 24) | 1.141 + ((b[1] & 0xFF) << 16) | 1.142 + ((b[2] & 0xFF) << 8) | 1.143 + (b[3] & 0xFF); 1.144 + integerList.add(Integer.valueOf(i)); 1.145 + } 1.146 + 1.147 + return generateArrayFromList(integerList); 1.148 + } 1.149 + 1.150 + 1.151 + public final void encodeToOutputStreamFromIntArray(int[] idata, OutputStream s) throws IOException { 1.152 + for (int i = 0; i < idata.length; i++) { 1.153 + final int bits = idata[i]; 1.154 + s.write((bits >>> 24) & 0xFF); 1.155 + s.write((bits >>> 16) & 0xFF); 1.156 + s.write((bits >>> 8) & 0xFF); 1.157 + s.write(bits & 0xFF); 1.158 + } 1.159 + } 1.160 + 1.161 + public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) { 1.162 + encodeToBytesFromIntArray((int[])array, astart, alength, b, start); 1.163 + } 1.164 + 1.165 + public final void encodeToBytesFromIntArray(int[] idata, int istart, int ilength, byte[] b, int start) { 1.166 + final int iend = istart + ilength; 1.167 + for (int i = istart; i < iend; i++) { 1.168 + final int bits = idata[i]; 1.169 + b[start++] = (byte)((bits >>> 24) & 0xFF); 1.170 + b[start++] = (byte)((bits >>> 16) & 0xFF); 1.171 + b[start++] = (byte)((bits >>> 8) & 0xFF); 1.172 + b[start++] = (byte)(bits & 0xFF); 1.173 + } 1.174 + } 1.175 + 1.176 + 1.177 + public final void convertToCharactersFromIntArray(int[] idata, StringBuffer s) { 1.178 + final int end = idata.length - 1; 1.179 + for (int i = 0; i <= end; i++) { 1.180 + s.append(Integer.toString(idata[i])); 1.181 + if (i != end) { 1.182 + s.append(' '); 1.183 + } 1.184 + } 1.185 + } 1.186 + 1.187 + 1.188 + public final int[] generateArrayFromList(List array) { 1.189 + int[] idata = new int[array.size()]; 1.190 + for (int i = 0; i < idata.length; i++) { 1.191 + idata[i] = ((Integer)array.get(i)).intValue(); 1.192 + } 1.193 + 1.194 + return idata; 1.195 + } 1.196 +}