KickJava   Java API By Example, From Geeks To Geeks.

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


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
43 import java.io.EOFException JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.io.OutputStream JavaDoc;
47 import java.nio.CharBuffer JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.List JavaDoc;
50
51 import org.jvnet.fastinfoset.EncodingAlgorithmException;
52 import com.sun.xml.fastinfoset.CommonResourceBundle;
53
54
55 /**
56  *
57  * An encoder for handling boolean values. Suppports the builtin BOOLEAN encoder.
58  *
59  * @author Alan Hudson
60  * @author Paul Sandoz
61  *
62  * @version
63  *
64  */

65
66 public class BooleanEncodingAlgorithm extends BuiltInEncodingAlgorithm {
67     
68     /** Table for setting a particular bit of a byte */
69     private static final int[] BIT_TABLE = {
70         1 << 7,
71         1 << 6,
72         1 << 5,
73         1 << 4,
74         1 << 3,
75         1 << 2,
76         1 << 1,
77         1 << 0};
78                 
79     public int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException {
80         // Cannot determine the number of boolean values from just the octet length
81
throw new UnsupportedOperationException JavaDoc();
82     }
83
84     public int getOctetLengthFromPrimitiveLength(int primitiveLength) {
85         if (primitiveLength < 5) {
86             return 1;
87         } else {
88             final int div = primitiveLength / 8;
89             return (div == 0) ? 2 : 1 + div;
90         }
91     }
92                 
93     public final Object JavaDoc decodeFromBytes(byte[] b, int start, int length) throws EncodingAlgorithmException {
94         final int blength = getPrimtiveLengthFromOctetLength(length, b[start]);
95         boolean[] data = new boolean[blength];
96
97         decodeFromBytesToBooleanArray(data, 0, blength, b, start, length);
98         return data;
99     }
100                 
101     public final Object JavaDoc decodeFromInputStream(InputStream JavaDoc s) throws IOException JavaDoc {
102         final List JavaDoc booleanList = new ArrayList JavaDoc();
103
104         int value = s.read();
105         if (value == -1) {
106             throw new EOFException JavaDoc();
107         }
108         final int unusedBits = (value >> 4) & 0xFF;
109                    
110         int bitPosition = 4;
111         int bitPositionEnd = 8;
112         int valueNext = 0;
113         do {
114             valueNext = s.read();
115             if (valueNext == -1) {
116                 bitPositionEnd -= unusedBits;
117             }
118             
119             while(bitPosition < bitPositionEnd) {
120                 booleanList.add(
121                         new Boolean JavaDoc((value & BIT_TABLE[bitPosition++]) > 0));
122             }
123             
124             value = valueNext;
125         } while (value != -1);
126         
127         return generateArrayFromList(booleanList);
128     }
129                 
130     public void encodeToOutputStream(Object JavaDoc data, OutputStream JavaDoc s) throws IOException JavaDoc {
131         if (!(data instanceof boolean[])) {
132             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.dataNotBoolean"));
133         }
134
135         boolean array[] = (boolean[])data;
136         final int alength = array.length;
137         
138         final int mod = (alength + 4) % 8;
139         final int unusedBits = (mod == 0) ? 0 : 8 - mod;
140         
141         int bitPosition = 4;
142         int value = unusedBits << 4;
143         int astart = 0;
144         while (astart < alength) {
145             if (array[astart++]) {
146                 value |= BIT_TABLE[bitPosition];
147             }
148             
149             if (++bitPosition == 8) {
150                 s.write(value);
151                 bitPosition = value = 0;
152             }
153         }
154         
155         if (bitPosition != 8) {
156             s.write(value);
157         }
158     }
159                                 
160     public final Object JavaDoc convertFromCharacters(char[] ch, int start, int length) {
161         if (length == 0) {
162             return new boolean[0];
163         }
164
165         final CharBuffer JavaDoc cb = CharBuffer.wrap(ch, start, length);
166         final List JavaDoc booleanList = new ArrayList JavaDoc();
167
168         matchWhiteSpaceDelimnatedWords(cb,
169             new WordListener() {
170                 public void word(int start, int end) {
171                     if (cb.charAt(start) == 't') {
172                         booleanList.add(Boolean.TRUE);
173                     } else {
174                         booleanList.add(Boolean.FALSE);
175                     }
176                 }
177             }
178         );
179
180         return generateArrayFromList(booleanList);
181     }
182
183     public final void convertToCharacters(Object JavaDoc data, StringBuffer JavaDoc s) {
184         if (data == null) {
185             return;
186         }
187
188         final boolean[] value = (boolean[]) data;
189         if (value.length == 0) {
190             return;
191         }
192
193         // Insure conservately as all false
194
s.ensureCapacity(value.length * 5);
195
196         final int end = value.length - 1;
197         for (int i = 0; i <= end; i++) {
198             if (value[i]) {
199                 s.append("true");
200             } else {
201                 s.append("false");
202             }
203             if (i != end) {
204                 s.append(' ');
205             }
206         }
207     }
208
209     public int getPrimtiveLengthFromOctetLength(int octetLength, int firstOctet) throws EncodingAlgorithmException {
210         final int unusedBits = (firstOctet >> 4) & 0xFF;
211         if (octetLength == 1) {
212            if (unusedBits > 3) {
213                throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits4"));
214            }
215            return 4 - unusedBits;
216         } else {
217            if (unusedBits > 7) {
218                throw new EncodingAlgorithmException(CommonResourceBundle.getInstance().getString("message.unusedBits8"));
219            }
220            return octetLength * 8 - 4 - unusedBits;
221         }
222     }
223     
224     public final void decodeFromBytesToBooleanArray(boolean[] bdata, int bstart, int blength, byte[] b, int start, int length) {
225         int value = b[start++] & 0xFF;
226         int bitPosition = 4;
227         final int bend = bstart + blength;
228         while (bstart < bend) {
229             if (bitPosition == 8) {
230                 value = b[start++] & 0xFF;
231                 bitPosition = 0;
232             }
233             
234             bdata[bstart++] = (value & BIT_TABLE[bitPosition++]) > 0;
235         }
236     }
237                 
238     public void encodeToBytes(Object JavaDoc array, int astart, int alength, byte[] b, int start) {
239         if (!(array instanceof boolean[])) {
240             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.dataNotBoolean"));
241         }
242
243         encodeToBytesFromBooleanArray((boolean[])array, astart, alength, b, start);
244     }
245     
246     public void encodeToBytesFromBooleanArray(boolean[] array, int astart, int alength, byte[] b, int start) {
247         final int mod = (alength + 4) % 8;
248         final int unusedBits = (mod == 0) ? 0 : 8 - mod;
249         
250         int bitPosition = 4;
251         int value = unusedBits << 4;
252         final int aend = astart + alength;
253         while (astart < aend) {
254             if (array[astart++]) {
255                 value |= BIT_TABLE[bitPosition];
256             }
257             
258             if (++bitPosition == 8) {
259                 b[start++] = (byte)value;
260                 bitPosition = value = 0;
261             }
262         }
263         
264         if (bitPosition > 0) {
265             b[start] = (byte)value;
266         }
267     }
268
269
270     /**
271      *
272      * Generate a boolean array from a list of Booleans.
273      *
274      * @param array The array
275      *
276      */

277     private final boolean[] generateArrayFromList(List JavaDoc array) {
278         boolean[] bdata = new boolean[array.size()];
279         for (int i = 0; i < bdata.length; i++) {
280             bdata[i] = ((Boolean JavaDoc)array.get(i)).booleanValue();
281         }
282
283         return bdata;
284     }
285            
286 }
287
288
Popular Tags