KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > algorithm > ShortEncodingAlgorithm


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.algorithm;
41
42 import java.io.EOFException JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.OutputStream JavaDoc;
46 import java.nio.CharBuffer JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.List JavaDoc;
49 import org.jvnet.fastinfoset.EncodingAlgorithmException;
50 import com.sun.xml.fastinfoset.CommonResourceBundle;
51
52
53 /**
54  * An encoder for handling Short values. Suppports the builtin SHORT encoder.
55  *
56  * @author Alan Hudson
57  * @version
58  */

59 public class ShortEncodingAlgorithm extends IntegerEncodingAlgorithm {
60
61     public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
62         if (octetLength % SHORT_SIZE != 0) {
63             throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().
64                     getString("message.lengthNotMultipleOfShort", new Object JavaDoc[]{new Integer JavaDoc(SHORT_SIZE)}));
65         }
66
67         return octetLength / SHORT_SIZE;
68     }
69
70     public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
71         return primitiveLength * SHORT_SIZE;
72     }
73
74     public final Object JavaDoc decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
75         short[] data = new short[getPrimtiveLengthFromOctetLength(length)];
76         decodeFromBytesToShortArray(data, 0, b, start, length);
77
78         return data;
79     }
80
81     public final Object JavaDoc decodeFromInputStream(InputStream JavaDoc s) throws IOException JavaDoc {
82         return decodeFromInputStreamToShortArray(s);
83     }
84
85
86     public void encodeToOutputStream(Object JavaDoc data, OutputStream JavaDoc s) throws IOException JavaDoc {
87         if (!(data instanceof short[])) {
88             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.dataNotShortArray"));
89         }
90
91         final short[] idata = (short[])data;
92
93         encodeToOutputStreamFromShortArray(idata, s);
94     }
95
96
97     public final Object JavaDoc convertFromCharacters(char[] ch, int start, int length) {
98         final CharBuffer JavaDoc cb = CharBuffer.wrap(ch, start, length);
99         final List JavaDoc shortList = new ArrayList JavaDoc();
100
101         matchWhiteSpaceDelimnatedWords(cb,
102                 new WordListener() {
103             public void word(int start, int end) {
104                 String JavaDoc iStringValue = cb.subSequence(start, end).toString();
105                 shortList.add(Short.valueOf(iStringValue));
106             }
107         }
108         );
109
110         return generateArrayFromList(shortList);
111     }
112
113     public final void convertToCharacters(Object JavaDoc data, StringBuffer JavaDoc s) {
114         if (!(data instanceof short[])) {
115             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.dataNotShortArray"));
116         }
117
118         final short[] idata = (short[])data;
119
120         convertToCharactersFromShortArray(idata, s);
121     }
122
123
124     public final void decodeFromBytesToShortArray(short[] sdata, int istart, byte[] b, int start, int length) {
125         final int size = length / SHORT_SIZE;
126         for (int i = 0; i < size; i++) {
127             sdata[istart++] = (short) (((b[start++] & 0xFF) << 8) |
128                     (b[start++] & 0xFF));
129         }
130     }
131
132     public final short[] decodeFromInputStreamToShortArray(InputStream JavaDoc s) throws IOException JavaDoc {
133         final List JavaDoc shortList = new ArrayList JavaDoc();
134         final byte[] b = new byte[SHORT_SIZE];
135
136         while (true) {
137             int n = s.read(b);
138             if (n != 2) {
139                 if (n == -1) {
140                     break;
141                 }
142
143                 while(n != 2) {
144                     final int m = s.read(b, n, SHORT_SIZE - n);
145                     if (m == -1) {
146                         throw new EOFException JavaDoc();
147                     }
148                     n += m;
149                 }
150             }
151
152             final int i = ((b[0] & 0xFF) << 8) |
153                     (b[1] & 0xFF);
154             shortList.add(new Short JavaDoc((short)i));
155         }
156
157         return generateArrayFromList(shortList);
158     }
159
160
161     public final void encodeToOutputStreamFromShortArray(short[] idata, OutputStream JavaDoc s) throws IOException JavaDoc {
162         for (int i = 0; i < idata.length; i++) {
163             final int bits = idata[i];
164             s.write((bits >>> 8) & 0xFF);
165             s.write(bits & 0xFF);
166         }
167     }
168
169     public final void encodeToBytes(Object JavaDoc array, int astart, int alength, byte[] b, int start) {
170         encodeToBytesFromShortArray((short[])array, astart, alength, b, start);
171     }
172
173     public final void encodeToBytesFromShortArray(short[] sdata, int istart, int ilength, byte[] b, int start) {
174         final int iend = istart + ilength;
175         for (int i = istart; i < iend; i++) {
176             final short bits = sdata[i];
177             b[start++] = (byte)((bits >>> 8) & 0xFF);
178             b[start++] = (byte)(bits & 0xFF);
179         }
180     }
181
182
183     public final void convertToCharactersFromShortArray(short[] sdata, StringBuffer JavaDoc s) {
184         final int end = sdata.length - 1;
185         for (int i = 0; i <= end; i++) {
186             s.append(Short.toString(sdata[i]));
187             if (i != end) {
188                 s.append(' ');
189             }
190         }
191     }
192
193
194     public final short[] generateArrayFromList(List JavaDoc array) {
195         short[] sdata = new short[array.size()];
196         for (int i = 0; i < sdata.length; i++) {
197             sdata[i] = ((Short JavaDoc)array.get(i)).shortValue();
198         }
199
200         return sdata;
201     }
202 }
203
Popular Tags