1 16 17 package org.apache.commons.codec.binary; 18 19 import org.apache.commons.codec.BinaryDecoder; 20 import org.apache.commons.codec.BinaryEncoder; 21 import org.apache.commons.codec.DecoderException; 22 import org.apache.commons.codec.EncoderException; 23 24 35 public class BinaryCodec implements BinaryDecoder, BinaryEncoder { 36 40 41 private static final char[] EMPTY_CHAR_ARRAY = new char[0]; 42 43 44 private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; 45 46 47 private static final int BIT_0 = 1; 48 49 50 private static final int BIT_1 = 0x02; 51 52 53 private static final int BIT_2 = 0x04; 54 55 56 private static final int BIT_3 = 0x08; 57 58 59 private static final int BIT_4 = 0x10; 60 61 62 private static final int BIT_5 = 0x20; 63 64 65 private static final int BIT_6 = 0x40; 66 67 68 private static final int BIT_7 = 0x80; 69 70 private static final int[] BITS = {BIT_0, BIT_1, BIT_2, BIT_3, BIT_4, BIT_5, BIT_6, BIT_7}; 71 72 80 public byte[] encode(byte[] raw) { 81 return toAsciiBytes(raw); 82 } 83 84 94 public Object encode(Object raw) throws EncoderException { 95 if (!(raw instanceof byte[])) { 96 throw new EncoderException("argument not a byte array"); 97 } 98 return toAsciiChars((byte[]) raw); 99 } 100 101 111 public Object decode(Object ascii) throws DecoderException { 112 if (ascii == null) { 113 return EMPTY_BYTE_ARRAY; 114 } 115 if (ascii instanceof byte[]) { 116 return fromAscii((byte[]) ascii); 117 } 118 if (ascii instanceof char[]) { 119 return fromAscii((char[]) ascii); 120 } 121 if (ascii instanceof String ) { 122 return fromAscii(((String ) ascii).toCharArray()); 123 } 124 throw new DecoderException("argument not a byte array"); 125 } 126 127 135 public byte[] decode(byte[] ascii) { 136 return fromAscii(ascii); 137 } 138 139 147 public byte[] toByteArray(String ascii) { 148 if (ascii == null) { 149 return EMPTY_BYTE_ARRAY; 150 } 151 return fromAscii(ascii.toCharArray()); 152 } 153 154 166 public static byte[] fromAscii(char[] ascii) { 167 if (ascii == null || ascii.length == 0) { 168 return EMPTY_BYTE_ARRAY; 169 } 170 byte[] l_raw = new byte[ascii.length >> 3]; 172 176 for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) { 177 for (int bits = 0; bits < BITS.length; ++bits) { 178 if (ascii[jj - bits] == '1') { 179 l_raw[ii] |= BITS[bits]; 180 } 181 } 182 } 183 return l_raw; 184 } 185 186 193 public static byte[] fromAscii(byte[] ascii) { 194 if (ascii == null || ascii.length == 0) { 195 return EMPTY_BYTE_ARRAY; 196 } 197 byte[] l_raw = new byte[ascii.length >> 3]; 199 203 for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) { 204 for (int bits = 0; bits < BITS.length; ++bits) { 205 if (ascii[jj - bits] == '1') { 206 l_raw[ii] |= BITS[bits]; 207 } 208 } 209 } 210 return l_raw; 211 } 212 213 222 public static byte[] toAsciiBytes(byte[] raw) { 223 if (raw == null || raw.length == 0) { 224 return EMPTY_BYTE_ARRAY; 225 } 226 byte[] l_ascii = new byte[raw.length << 3]; 228 232 for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { 233 for (int bits = 0; bits < BITS.length; ++bits) { 234 if ((raw[ii] & BITS[bits]) == 0) { 235 l_ascii[jj - bits] = '0'; 236 } else { 237 l_ascii[jj - bits] = '1'; 238 } 239 } 240 } 241 return l_ascii; 242 } 243 244 252 public static char[] toAsciiChars(byte[] raw) { 253 if (raw == null || raw.length == 0) { 254 return EMPTY_CHAR_ARRAY; 255 } 256 char[] l_ascii = new char[raw.length << 3]; 258 262 for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { 263 for (int bits = 0; bits < BITS.length; ++bits) { 264 if ((raw[ii] & BITS[bits]) == 0) { 265 l_ascii[jj - bits] = '0'; 266 } else { 267 l_ascii[jj - bits] = '1'; 268 } 269 } 270 } 271 return l_ascii; 272 } 273 274 282 public static String toAsciiString(byte[] raw) { 283 return new String (toAsciiChars(raw)); 284 } 285 } 286 | Popular Tags |