001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.algorithm;
040:
041: import java.io.EOFException;
042: import java.io.IOException;
043: import java.io.InputStream;
044: import java.io.OutputStream;
045: import java.nio.CharBuffer;
046: import java.util.ArrayList;
047: import java.util.List;
048: import org.jvnet.fastinfoset.EncodingAlgorithmException;
049: import com.sun.xml.fastinfoset.CommonResourceBundle;
050:
051: /**
052: * An encoder for handling Short values. Suppports the builtin SHORT encoder.
053: *
054: * @author Alan Hudson
055: * @author Paul Sandoz
056: */
057: public class ShortEncodingAlgorithm extends IntegerEncodingAlgorithm {
058:
059: public final int getPrimtiveLengthFromOctetLength(int octetLength)
060: throws EncodingAlgorithmException {
061: if (octetLength % SHORT_SIZE != 0) {
062: throw new EncodingAlgorithmException(
063: CommonResourceBundle.getInstance()
064: .getString(
065: "message.lengthNotMultipleOfShort",
066: new Object[] { Integer
067: .valueOf(SHORT_SIZE) }));
068: }
069:
070: return octetLength / SHORT_SIZE;
071: }
072:
073: public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
074: return primitiveLength * SHORT_SIZE;
075: }
076:
077: public final Object decodeFromBytes(byte[] b, int start, int length)
078: throws EncodingAlgorithmException {
079: short[] data = new short[getPrimtiveLengthFromOctetLength(length)];
080: decodeFromBytesToShortArray(data, 0, b, start, length);
081:
082: return data;
083: }
084:
085: public final Object decodeFromInputStream(InputStream s)
086: throws IOException {
087: return decodeFromInputStreamToShortArray(s);
088: }
089:
090: public void encodeToOutputStream(Object data, OutputStream s)
091: throws IOException {
092: if (!(data instanceof short[])) {
093: throw new IllegalArgumentException(CommonResourceBundle
094: .getInstance().getString(
095: "message.dataNotShortArray"));
096: }
097:
098: final short[] idata = (short[]) data;
099:
100: encodeToOutputStreamFromShortArray(idata, s);
101: }
102:
103: public final Object convertFromCharacters(char[] ch, int start,
104: int length) {
105: final CharBuffer cb = CharBuffer.wrap(ch, start, length);
106: final List shortList = new ArrayList();
107:
108: matchWhiteSpaceDelimnatedWords(cb, new WordListener() {
109: public void word(int start, int end) {
110: String iStringValue = cb.subSequence(start, end)
111: .toString();
112: shortList.add(Short.valueOf(iStringValue));
113: }
114: });
115:
116: return generateArrayFromList(shortList);
117: }
118:
119: public final void convertToCharacters(Object data, StringBuffer s) {
120: if (!(data instanceof short[])) {
121: throw new IllegalArgumentException(CommonResourceBundle
122: .getInstance().getString(
123: "message.dataNotShortArray"));
124: }
125:
126: final short[] idata = (short[]) data;
127:
128: convertToCharactersFromShortArray(idata, s);
129: }
130:
131: public final void decodeFromBytesToShortArray(short[] sdata,
132: int istart, byte[] b, int start, int length) {
133: final int size = length / SHORT_SIZE;
134: for (int i = 0; i < size; i++) {
135: sdata[istart++] = (short) (((b[start++] & 0xFF) << 8) | (b[start++] & 0xFF));
136: }
137: }
138:
139: public final short[] decodeFromInputStreamToShortArray(InputStream s)
140: throws IOException {
141: final List shortList = new ArrayList();
142: final byte[] b = new byte[SHORT_SIZE];
143:
144: while (true) {
145: int n = s.read(b);
146: if (n != 2) {
147: if (n == -1) {
148: break;
149: }
150:
151: while (n != 2) {
152: final int m = s.read(b, n, SHORT_SIZE - n);
153: if (m == -1) {
154: throw new EOFException();
155: }
156: n += m;
157: }
158: }
159:
160: final int i = ((b[0] & 0xFF) << 8) | (b[1] & 0xFF);
161: shortList.add(Short.valueOf((short) i));
162: }
163:
164: return generateArrayFromList(shortList);
165: }
166:
167: public final void encodeToOutputStreamFromShortArray(short[] idata,
168: OutputStream s) throws IOException {
169: for (int i = 0; i < idata.length; i++) {
170: final int bits = idata[i];
171: s.write((bits >>> 8) & 0xFF);
172: s.write(bits & 0xFF);
173: }
174: }
175:
176: public final void encodeToBytes(Object array, int astart,
177: int alength, byte[] b, int start) {
178: encodeToBytesFromShortArray((short[]) array, astart, alength,
179: b, start);
180: }
181:
182: public final void encodeToBytesFromShortArray(short[] sdata,
183: int istart, int ilength, byte[] b, int start) {
184: final int iend = istart + ilength;
185: for (int i = istart; i < iend; i++) {
186: final short bits = sdata[i];
187: b[start++] = (byte) ((bits >>> 8) & 0xFF);
188: b[start++] = (byte) (bits & 0xFF);
189: }
190: }
191:
192: public final void convertToCharactersFromShortArray(short[] sdata,
193: StringBuffer s) {
194: final int end = sdata.length - 1;
195: for (int i = 0; i <= end; i++) {
196: s.append(Short.toString(sdata[i]));
197: if (i != end) {
198: s.append(' ');
199: }
200: }
201: }
202:
203: public final short[] generateArrayFromList(List array) {
204: short[] sdata = new short[array.size()];
205: for (int i = 0; i < sdata.length; i++) {
206: sdata[i] = ((Short) array.get(i)).shortValue();
207: }
208:
209: return sdata;
210: }
211: }
|