KickJava   Java API By Example, From Geeks To Geeks.

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


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