KickJava   Java API By Example, From Geeks To Geeks.

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


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