aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.fastinfoset.algorithm; aoqi@0: aoqi@0: import java.io.EOFException; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.OutputStream; aoqi@0: import java.nio.CharBuffer; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.List; aoqi@0: import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException; aoqi@0: import com.sun.xml.internal.fastinfoset.CommonResourceBundle; aoqi@0: aoqi@0: aoqi@0: public class LongEncodingAlgorithm extends IntegerEncodingAlgorithm { aoqi@0: aoqi@0: public int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { aoqi@0: if (octetLength % LONG_SIZE != 0) { aoqi@0: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance(). aoqi@0: getString("message.lengthNotMultipleOfLong", new Object[]{Integer.valueOf(LONG_SIZE)})); aoqi@0: } aoqi@0: aoqi@0: return octetLength / LONG_SIZE; aoqi@0: } aoqi@0: aoqi@0: public int getOctetLengthFromPrimitiveLength(int primitiveLength) { aoqi@0: return primitiveLength * LONG_SIZE; aoqi@0: } aoqi@0: aoqi@0: public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException { aoqi@0: long[] data = new long[getPrimtiveLengthFromOctetLength(length)]; aoqi@0: decodeFromBytesToLongArray(data, 0, b, start, length); aoqi@0: aoqi@0: return data; aoqi@0: } aoqi@0: aoqi@0: public final Object decodeFromInputStream(InputStream s) throws IOException { aoqi@0: return decodeFromInputStreamToIntArray(s); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public void encodeToOutputStream(Object data, OutputStream s) throws IOException { aoqi@0: if (!(data instanceof long[])) { aoqi@0: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotLongArray")); aoqi@0: } aoqi@0: aoqi@0: final long[] ldata = (long[])data; aoqi@0: aoqi@0: encodeToOutputStreamFromLongArray(ldata, s); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public Object convertFromCharacters(char[] ch, int start, int length) { aoqi@0: final CharBuffer cb = CharBuffer.wrap(ch, start, length); aoqi@0: final List longList = new ArrayList(); aoqi@0: aoqi@0: matchWhiteSpaceDelimnatedWords(cb, aoqi@0: new WordListener() { aoqi@0: public void word(int start, int end) { aoqi@0: String lStringValue = cb.subSequence(start, end).toString(); aoqi@0: longList.add(Long.valueOf(lStringValue)); aoqi@0: } aoqi@0: } aoqi@0: ); aoqi@0: aoqi@0: return generateArrayFromList(longList); aoqi@0: } aoqi@0: aoqi@0: public void convertToCharacters(Object data, StringBuffer s) { aoqi@0: if (!(data instanceof long[])) { aoqi@0: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotLongArray")); aoqi@0: } aoqi@0: aoqi@0: final long[] ldata = (long[])data; aoqi@0: aoqi@0: convertToCharactersFromLongArray(ldata, s); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public final void decodeFromBytesToLongArray(long[] ldata, int istart, byte[] b, int start, int length) { aoqi@0: final int size = length / LONG_SIZE; aoqi@0: for (int i = 0; i < size; i++) { aoqi@0: ldata[istart++] = aoqi@0: ((long)(b[start++] & 0xFF) << 56) | aoqi@0: ((long)(b[start++] & 0xFF) << 48) | aoqi@0: ((long)(b[start++] & 0xFF) << 40) | aoqi@0: ((long)(b[start++] & 0xFF) << 32) | aoqi@0: ((long)(b[start++] & 0xFF) << 24) | aoqi@0: ((long)(b[start++] & 0xFF) << 16) | aoqi@0: ((long)(b[start++] & 0xFF) << 8) | aoqi@0: (long)(b[start++] & 0xFF); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final long[] decodeFromInputStreamToIntArray(InputStream s) throws IOException { aoqi@0: final List longList = new ArrayList(); aoqi@0: final byte[] b = new byte[LONG_SIZE]; aoqi@0: aoqi@0: while (true) { aoqi@0: int n = s.read(b); aoqi@0: if (n != LONG_SIZE) { aoqi@0: if (n == -1) { aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: while(n != LONG_SIZE) { aoqi@0: final int m = s.read(b, n, LONG_SIZE - n); aoqi@0: if (m == -1) { aoqi@0: throw new EOFException(); aoqi@0: } aoqi@0: n += m; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: final long l = aoqi@0: (((long) b[0] << 56) + aoqi@0: ((long) (b[1] & 0xFF) << 48) + aoqi@0: ((long) (b[2] & 0xFF) << 40) + aoqi@0: ((long) (b[3] & 0xFF) << 32) + aoqi@0: ((long) (b[4] & 0xFF) << 24) + aoqi@0: ((b[5] & 0xFF) << 16) + aoqi@0: ((b[6] & 0xFF) << 8) + aoqi@0: ((b[7] & 0xFF) << 0)); aoqi@0: aoqi@0: longList.add(Long.valueOf(l)); aoqi@0: } aoqi@0: aoqi@0: return generateArrayFromList(longList); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public final void encodeToOutputStreamFromLongArray(long[] ldata, OutputStream s) throws IOException { aoqi@0: for (int i = 0; i < ldata.length; i++) { aoqi@0: final long bits = ldata[i]; aoqi@0: s.write((int)((bits >>> 56) & 0xFF)); aoqi@0: s.write((int)((bits >>> 48) & 0xFF)); aoqi@0: s.write((int)((bits >>> 40) & 0xFF)); aoqi@0: s.write((int)((bits >>> 32) & 0xFF)); aoqi@0: s.write((int)((bits >>> 24) & 0xFF)); aoqi@0: s.write((int)((bits >>> 16) & 0xFF)); aoqi@0: s.write((int)((bits >>> 8) & 0xFF)); aoqi@0: s.write((int)(bits & 0xFF)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final void encodeToBytes(Object array, int astart, int alength, byte[] b, int start) { aoqi@0: encodeToBytesFromLongArray((long[])array, astart, alength, b, start); aoqi@0: } aoqi@0: aoqi@0: public final void encodeToBytesFromLongArray(long[] ldata, int lstart, int llength, byte[] b, int start) { aoqi@0: final int lend = lstart + llength; aoqi@0: for (int i = lstart; i < lend; i++) { aoqi@0: final long bits = ldata[i]; aoqi@0: b[start++] = (byte)((bits >>> 56) & 0xFF); aoqi@0: b[start++] = (byte)((bits >>> 48) & 0xFF); aoqi@0: b[start++] = (byte)((bits >>> 40) & 0xFF); aoqi@0: b[start++] = (byte)((bits >>> 32) & 0xFF); aoqi@0: b[start++] = (byte)((bits >>> 24) & 0xFF); aoqi@0: b[start++] = (byte)((bits >>> 16) & 0xFF); aoqi@0: b[start++] = (byte)((bits >>> 8) & 0xFF); aoqi@0: b[start++] = (byte)(bits & 0xFF); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public final void convertToCharactersFromLongArray(long[] ldata, StringBuffer s) { aoqi@0: final int end = ldata.length - 1; aoqi@0: for (int i = 0; i <= end; i++) { aoqi@0: s.append(Long.toString(ldata[i])); aoqi@0: if (i != end) { aoqi@0: s.append(' '); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public final long[] generateArrayFromList(List array) { aoqi@0: long[] ldata = new long[array.size()]; aoqi@0: for (int i = 0; i < ldata.length; i++) { aoqi@0: ldata[i] = ((Long)array.get(i)).longValue(); aoqi@0: } aoqi@0: aoqi@0: return ldata; aoqi@0: } aoqi@0: }