Thu, 12 Oct 2017 19:44:07 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | * |
aoqi@0 | 25 | * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
aoqi@0 | 26 | */ |
aoqi@0 | 27 | |
aoqi@0 | 28 | package com.sun.xml.internal.fastinfoset.algorithm; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import java.io.EOFException; |
aoqi@0 | 31 | import java.io.IOException; |
aoqi@0 | 32 | import java.io.InputStream; |
aoqi@0 | 33 | import java.io.OutputStream; |
aoqi@0 | 34 | import java.nio.CharBuffer; |
aoqi@0 | 35 | import java.util.ArrayList; |
aoqi@0 | 36 | import java.util.List; |
aoqi@0 | 37 | import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException; |
aoqi@0 | 38 | import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
aoqi@0 | 39 | |
aoqi@0 | 40 | |
aoqi@0 | 41 | |
aoqi@0 | 42 | public class IntEncodingAlgorithm extends IntegerEncodingAlgorithm { |
aoqi@0 | 43 | |
aoqi@0 | 44 | public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { |
aoqi@0 | 45 | if (octetLength % INT_SIZE != 0) { |
aoqi@0 | 46 | throw new EncodingAlgorithmException(CommonResourceBundle.getInstance(). |
aoqi@0 | 47 | getString("message.lengthNotMultipleOfInt", new Object[]{Integer.valueOf(INT_SIZE)})); |
aoqi@0 | 48 | } |
aoqi@0 | 49 | |
aoqi@0 | 50 | return octetLength / INT_SIZE; |
aoqi@0 | 51 | } |
aoqi@0 | 52 | |
aoqi@0 | 53 | public int getOctetLengthFromPrimitiveLength(int primitiveLength) { |
aoqi@0 | 54 | return primitiveLength * INT_SIZE; |
aoqi@0 | 55 | } |
aoqi@0 | 56 | |
aoqi@0 | 57 | public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException { |
aoqi@0 | 58 | int[] data = new int[getPrimtiveLengthFromOctetLength(length)]; |
aoqi@0 | 59 | decodeFromBytesToIntArray(data, 0, b, start, length); |
aoqi@0 | 60 | |
aoqi@0 | 61 | return data; |
aoqi@0 | 62 | } |
aoqi@0 | 63 | |
aoqi@0 | 64 | public final Object decodeFromInputStream(InputStream s) throws IOException { |
aoqi@0 | 65 | return decodeFromInputStreamToIntArray(s); |
aoqi@0 | 66 | } |
aoqi@0 | 67 | |
aoqi@0 | 68 | |
aoqi@0 | 69 | public void encodeToOutputStream(Object data, OutputStream s) throws IOException { |
aoqi@0 | 70 | if (!(data instanceof int[])) { |
aoqi@0 | 71 | throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotIntArray")); |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | final int[] idata = (int[])data; |
aoqi@0 | 75 | |
aoqi@0 | 76 | encodeToOutputStreamFromIntArray(idata, s); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | |
aoqi@0 | 79 | |
aoqi@0 | 80 | public final Object convertFromCharacters(char[] ch, int start, int length) { |
aoqi@0 | 81 | final CharBuffer cb = CharBuffer.wrap(ch, start, length); |
aoqi@0 | 82 | final List integerList = new ArrayList(); |
aoqi@0 | 83 | |
aoqi@0 | 84 | matchWhiteSpaceDelimnatedWords(cb, |
aoqi@0 | 85 | new WordListener() { |
aoqi@0 | 86 | public void word(int start, int end) { |
aoqi@0 | 87 | String iStringValue = cb.subSequence(start, end).toString(); |
aoqi@0 | 88 | integerList.add(Integer.valueOf(iStringValue)); |
aoqi@0 | 89 | } |
aoqi@0 | 90 | } |
aoqi@0 | 91 | ); |
aoqi@0 | 92 | |
aoqi@0 | 93 | return generateArrayFromList(integerList); |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | public final void convertToCharacters(Object data, StringBuffer s) { |
aoqi@0 | 97 | if (!(data instanceof int[])) { |
aoqi@0 | 98 | throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotIntArray")); |
aoqi@0 | 99 | } |
aoqi@0 | 100 | |
aoqi@0 | 101 | final int[] idata = (int[])data; |
aoqi@0 | 102 | |
aoqi@0 | 103 | convertToCharactersFromIntArray(idata, s); |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | |
aoqi@0 | 107 | public final void decodeFromBytesToIntArray(int[] idata, int istart, byte[] b, int start, int length) { |
aoqi@0 | 108 | final int size = length / INT_SIZE; |
aoqi@0 | 109 | for (int i = 0; i < size; i++) { |
aoqi@0 | 110 | idata[istart++] = ((b[start++] & 0xFF) << 24) | |
aoqi@0 | 111 | ((b[start++] & 0xFF) << 16) | |
aoqi@0 | 112 | ((b[start++] & 0xFF) << 8) | |
aoqi@0 | 113 | (b[start++] & 0xFF); |
aoqi@0 | 114 | } |
aoqi@0 | 115 | } |
aoqi@0 | 116 | |
aoqi@0 | 117 | public final int[] decodeFromInputStreamToIntArray(InputStream s) throws IOException { |
aoqi@0 | 118 | final List integerList = new ArrayList(); |
aoqi@0 | 119 | final byte[] b = new byte[INT_SIZE]; |
aoqi@0 | 120 | |
aoqi@0 | 121 | while (true) { |
aoqi@0 | 122 | int n = s.read(b); |
aoqi@0 | 123 | if (n != 4) { |
aoqi@0 | 124 | if (n == -1) { |
aoqi@0 | 125 | break; |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | while(n != 4) { |
aoqi@0 | 129 | final int m = s.read(b, n, INT_SIZE - n); |
aoqi@0 | 130 | if (m == -1) { |
aoqi@0 | 131 | throw new EOFException(); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | n += m; |
aoqi@0 | 134 | } |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | final int i = ((b[0] & 0xFF) << 24) | |
aoqi@0 | 138 | ((b[1] & 0xFF) << 16) | |
aoqi@0 | 139 | ((b[2] & 0xFF) << 8) | |
aoqi@0 | 140 | (b[3] & 0xFF); |
aoqi@0 | 141 | integerList.add(Integer.valueOf(i)); |
aoqi@0 | 142 | } |
aoqi@0 | 143 | |
aoqi@0 | 144 | return generateArrayFromList(integerList); |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | |
aoqi@0 | 148 | public final void encodeToOutputStreamFromIntArray(int[] idata, OutputStream s) throws IOException { |
aoqi@0 | 149 | for (int i = 0; i < idata.length; i++) { |
aoqi@0 | 150 | final int bits = idata[i]; |
aoqi@0 | 151 | s.write((bits >>> 24) & 0xFF); |
aoqi@0 | 152 | s.write((bits >>> 16) & 0xFF); |
aoqi@0 | 153 | s.write((bits >>> 8) & 0xFF); |
aoqi@0 | 154 | s.write(bits & 0xFF); |
aoqi@0 | 155 | } |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) { |
aoqi@0 | 159 | encodeToBytesFromIntArray((int[])array, astart, alength, b, start); |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | public final void encodeToBytesFromIntArray(int[] idata, int istart, int ilength, byte[] b, int start) { |
aoqi@0 | 163 | final int iend = istart + ilength; |
aoqi@0 | 164 | for (int i = istart; i < iend; i++) { |
aoqi@0 | 165 | final int bits = idata[i]; |
aoqi@0 | 166 | b[start++] = (byte)((bits >>> 24) & 0xFF); |
aoqi@0 | 167 | b[start++] = (byte)((bits >>> 16) & 0xFF); |
aoqi@0 | 168 | b[start++] = (byte)((bits >>> 8) & 0xFF); |
aoqi@0 | 169 | b[start++] = (byte)(bits & 0xFF); |
aoqi@0 | 170 | } |
aoqi@0 | 171 | } |
aoqi@0 | 172 | |
aoqi@0 | 173 | |
aoqi@0 | 174 | public final void convertToCharactersFromIntArray(int[] idata, StringBuffer s) { |
aoqi@0 | 175 | final int end = idata.length - 1; |
aoqi@0 | 176 | for (int i = 0; i <= end; i++) { |
aoqi@0 | 177 | s.append(Integer.toString(idata[i])); |
aoqi@0 | 178 | if (i != end) { |
aoqi@0 | 179 | s.append(' '); |
aoqi@0 | 180 | } |
aoqi@0 | 181 | } |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | |
aoqi@0 | 185 | public final int[] generateArrayFromList(List array) { |
aoqi@0 | 186 | int[] idata = new int[array.size()]; |
aoqi@0 | 187 | for (int i = 0; i < idata.length; i++) { |
aoqi@0 | 188 | idata[i] = ((Integer)array.get(i)).intValue(); |
aoqi@0 | 189 | } |
aoqi@0 | 190 | |
aoqi@0 | 191 | return idata; |
aoqi@0 | 192 | } |
aoqi@0 | 193 | } |