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