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/A/**
325N/A * An encoder for handling Short values. Suppports the builtin SHORT encoder.
325N/A *
325N/A * @author Alan Hudson
325N/A * @author Paul Sandoz
325N/A */
325N/Apublic class ShortEncodingAlgorithm extends IntegerEncodingAlgorithm {
325N/A
325N/A public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
325N/A if (octetLength % SHORT_SIZE != 0) {
325N/A throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
325N/A getString("message.lengthNotMultipleOfShort", new Object[]{Integer.valueOf(SHORT_SIZE)}));
325N/A }
325N/A
325N/A return octetLength / SHORT_SIZE;
325N/A }
325N/A
325N/A public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
325N/A return primitiveLength * SHORT_SIZE;
325N/A }
325N/A
325N/A public final Object decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
325N/A short[] data = new short[getPrimtiveLengthFromOctetLength(length)];
325N/A decodeFromBytesToShortArray(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 decodeFromInputStreamToShortArray(s);
325N/A }
325N/A
325N/A
325N/A public void encodeToOutputStream(Object data, OutputStream s) throws IOException {
325N/A if (!(data instanceof short[])) {
325N/A throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotShortArray"));
325N/A }
325N/A
325N/A final short[] idata = (short[])data;
325N/A
325N/A encodeToOutputStreamFromShortArray(idata, s);
325N/A }
325N/A
325N/A
325N/A public final Object convertFromCharacters(char[] ch, int start, int length) {
325N/A final CharBuffer cb = CharBuffer.wrap(ch, start, length);
325N/A final List shortList = new ArrayList();
325N/A
325N/A matchWhiteSpaceDelimnatedWords(cb,
325N/A new WordListener() {
325N/A public void word(int start, int end) {
325N/A String iStringValue = cb.subSequence(start, end).toString();
325N/A shortList.add(Short.valueOf(iStringValue));
325N/A }
325N/A }
325N/A );
325N/A
325N/A return generateArrayFromList(shortList);
325N/A }
325N/A
325N/A public final void convertToCharacters(Object data, StringBuffer s) {
325N/A if (!(data instanceof short[])) {
325N/A throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotShortArray"));
325N/A }
325N/A
325N/A final short[] idata = (short[])data;
325N/A
325N/A convertToCharactersFromShortArray(idata, s);
325N/A }
325N/A
325N/A
325N/A public final void decodeFromBytesToShortArray(short[] sdata, int istart, byte[] b, int start, int length) {
325N/A final int size = length / SHORT_SIZE;
325N/A for (int i = 0; i < size; i++) {
325N/A sdata[istart++] = (short) (((b[start++] & 0xFF) << 8) |
325N/A (b[start++] & 0xFF));
325N/A }
325N/A }
325N/A
325N/A public final short[] decodeFromInputStreamToShortArray(InputStream s) throws IOException {
325N/A final List shortList = new ArrayList();
325N/A final byte[] b = new byte[SHORT_SIZE];
325N/A
325N/A while (true) {
325N/A int n = s.read(b);
325N/A if (n != 2) {
325N/A if (n == -1) {
325N/A break;
325N/A }
325N/A
325N/A while(n != 2) {
325N/A final int m = s.read(b, n, SHORT_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 int i = ((b[0] & 0xFF) << 8) |
325N/A (b[1] & 0xFF);
325N/A shortList.add(Short.valueOf((short)i));
325N/A }
325N/A
325N/A return generateArrayFromList(shortList);
325N/A }
325N/A
325N/A
325N/A public final void encodeToOutputStreamFromShortArray(short[] idata, OutputStream s) throws IOException {
325N/A for (int i = 0; i < idata.length; i++) {
325N/A final int bits = idata[i];
325N/A s.write((bits >>> 8) & 0xFF);
325N/A s.write(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 encodeToBytesFromShortArray((short[])array, astart, alength, b, start);
325N/A }
325N/A
325N/A public final void encodeToBytesFromShortArray(short[] sdata, int istart, int ilength, byte[] b, int start) {
325N/A final int iend = istart + ilength;
325N/A for (int i = istart; i < iend; i++) {
325N/A final short bits = sdata[i];
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 convertToCharactersFromShortArray(short[] sdata, StringBuffer s) {
325N/A final int end = sdata.length - 1;
325N/A for (int i = 0; i <= end; i++) {
325N/A s.append(Short.toString(sdata[i]));
325N/A if (i != end) {
325N/A s.append(' ');
325N/A }
325N/A }
325N/A }
325N/A
325N/A
325N/A public final short[] generateArrayFromList(List array) {
325N/A short[] sdata = new short[array.size()];
325N/A for (int i = 0; i < sdata.length; i++) {
325N/A sdata[i] = ((Short)array.get(i)).shortValue();
325N/A }
325N/A
325N/A return sdata;
325N/A }
325N/A}