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