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