1 38 39 40 package com.sun.xml.fastinfoset.algorithm; 41 42 43 import java.io.EOFException ; 44 import java.io.IOException ; 45 import java.io.InputStream ; 46 import java.io.OutputStream ; 47 import java.nio.CharBuffer ; 48 import java.util.ArrayList ; 49 import java.util.List ; 50 51 import org.jvnet.fastinfoset.EncodingAlgorithmException; 52 import com.sun.xml.fastinfoset.CommonResourceBundle; 53 54 55 65 66 public class BooleanEncodingAlgorithm extends BuiltInEncodingAlgorithm { 67 68 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 throw new UnsupportedOperationException (); 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 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 decodeFromInputStream(InputStream s) throws IOException { 102 final List booleanList = new ArrayList (); 103 104 int value = s.read(); 105 if (value == -1) { 106 throw new EOFException (); 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 ((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 data, OutputStream s) throws IOException { 131 if (!(data instanceof boolean[])) { 132 throw new IllegalArgumentException (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 convertFromCharacters(char[] ch, int start, int length) { 161 if (length == 0) { 162 return new boolean[0]; 163 } 164 165 final CharBuffer cb = CharBuffer.wrap(ch, start, length); 166 final List booleanList = new ArrayList (); 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 data, StringBuffer 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 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 array, int astart, int alength, byte[] b, int start) { 239 if (!(array instanceof boolean[])) { 240 throw new IllegalArgumentException (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 277 private final boolean[] generateArrayFromList(List array) { 278 boolean[] bdata = new boolean[array.size()]; 279 for (int i = 0; i < bdata.length; i++) { 280 bdata[i] = ((Boolean )array.get(i)).booleanValue(); 281 } 282 283 return bdata; 284 } 285 286 } 287 288 | Popular Tags |