1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/* 2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Copyright 2001-2004 The Apache Software Foundation. 3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * you may not use this file except in compliance with the License. 6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * You may obtain a copy of the License at 7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * See the License for the specific language governing permissions and 14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * limitations under the License. 15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.commons.codec.binary; 18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.codec.BinaryDecoder; 20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.codec.BinaryEncoder; 21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.codec.DecoderException; 22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.codec.EncoderException; 23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/** 25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Translates between byte arrays and strings of "0"s and "1"s. 26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <b>TODO:</b> may want to add more bit vector functions like and/or/xor/nand. 28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <B>TODO:</b> also might be good to generate boolean[] 29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * from byte[] et. cetera. 30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Apache Software Foundation 32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 1.3 33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @version $Id $ 34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class BinaryCodec implements BinaryDecoder, BinaryEncoder { 36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /* 37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * tried to avoid using ArrayUtils to minimize dependencies while using these empty arrays - dep is just not worth 38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * it. 39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Empty char array. */ 41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final char[] EMPTY_CHAR_ARRAY = new char[0]; 42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Empty byte array. */ 44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; 45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Mask for bit 0 of a byte. */ 47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final int BIT_0 = 1; 48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Mask for bit 1 of a byte. */ 50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final int BIT_1 = 0x02; 51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Mask for bit 2 of a byte. */ 53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final int BIT_2 = 0x04; 54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Mask for bit 3 of a byte. */ 56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final int BIT_3 = 0x08; 57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Mask for bit 4 of a byte. */ 59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final int BIT_4 = 0x10; 60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Mask for bit 5 of a byte. */ 62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final int BIT_5 = 0x20; 63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Mask for bit 6 of a byte. */ 65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final int BIT_6 = 0x40; 66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Mask for bit 7 of a byte. */ 68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final int BIT_7 = 0x80; 69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private static final int[] BITS = {BIT_0, BIT_1, BIT_2, BIT_3, BIT_4, BIT_5, BIT_6, BIT_7}; 71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Converts an array of raw binary data into an array of ascii 0 and 1 characters. 74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param raw 76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the raw binary data to convert 77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return 0 and 1 ascii character bytes one for each bit of the argument 78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) 79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public byte[] encode(byte[] raw) { 81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return toAsciiBytes(raw); 82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Converts an array of raw binary data into an array of ascii 0 and 1 chars. 86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param raw 88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the raw binary data to convert 89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return 0 and 1 ascii character chars one for each bit of the argument 90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws EncoderException 91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * if the argument is not a byte[] 92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see org.apache.commons.codec.Encoder#encode(java.lang.Object) 93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public Object encode(Object raw) throws EncoderException { 95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (!(raw instanceof byte[])) { 96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new EncoderException("argument not a byte array"); 97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return toAsciiChars((byte[]) raw); 99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Decodes a byte array where each byte represents an ascii '0' or '1'. 103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param ascii 105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * each byte represents an ascii '0' or '1' 106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument 107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws DecoderException 108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * if argument is not a byte[], char[] or String 109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see org.apache.commons.codec.Decoder#decode(java.lang.Object) 110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public Object decode(Object ascii) throws DecoderException { 112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (ascii == null) { 113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return EMPTY_BYTE_ARRAY; 114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (ascii instanceof byte[]) { 116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return fromAscii((byte[]) ascii); 117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (ascii instanceof char[]) { 119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return fromAscii((char[]) ascii); 120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (ascii instanceof String) { 122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return fromAscii(((String) ascii).toCharArray()); 123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new DecoderException("argument not a byte array"); 125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Decodes a byte array where each byte represents an ascii '0' or '1'. 129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param ascii 131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * each byte represents an ascii '0' or '1' 132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument 133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see org.apache.commons.codec.Decoder#decode(Object) 134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public byte[] decode(byte[] ascii) { 136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return fromAscii(ascii); 137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Decodes a String where each char of the String represents an ascii '0' or '1'. 141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param ascii 143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * String of '0' and '1' characters 144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument 145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see org.apache.commons.codec.Decoder#decode(Object) 146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public byte[] toByteArray(String ascii) { 148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (ascii == null) { 149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return EMPTY_BYTE_ARRAY; 150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return fromAscii(ascii.toCharArray()); 152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // ------------------------------------------------------------------------ 155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // 156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // static codec operations 157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // 158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // ------------------------------------------------------------------------ 159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Decodes a byte array where each char represents an ascii '0' or '1'. 161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param ascii 163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * each char represents an ascii '0' or '1' 164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return the raw encoded binary where each bit corresponds to a char in the char array argument 165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public static byte[] fromAscii(char[] ascii) { 167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (ascii == null || ascii.length == 0) { 168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return EMPTY_BYTE_ARRAY; 169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // get length/8 times bytes with 3 bit shifts to the right of the length 171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project byte[] l_raw = new byte[ascii.length >> 3]; 172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /* 173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the 174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * loop. 175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) { 177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project for (int bits = 0; bits < BITS.length; ++bits) { 178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (ascii[jj - bits] == '1') { 179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project l_raw[ii] |= BITS[bits]; 180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return l_raw; 184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Decodes a byte array where each byte represents an ascii '0' or '1'. 188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param ascii 190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * each byte represents an ascii '0' or '1' 191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument 192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public static byte[] fromAscii(byte[] ascii) { 194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (ascii == null || ascii.length == 0) { 195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return EMPTY_BYTE_ARRAY; 196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // get length/8 times bytes with 3 bit shifts to the right of the length 198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project byte[] l_raw = new byte[ascii.length >> 3]; 199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /* 200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the 201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * loop. 202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) { 204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project for (int bits = 0; bits < BITS.length; ++bits) { 205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (ascii[jj - bits] == '1') { 206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project l_raw[ii] |= BITS[bits]; 207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return l_raw; 211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Converts an array of raw binary data into an array of ascii 0 and 1 character bytes - each byte is a truncated 215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * char. 216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param raw 218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the raw binary data to convert 219069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return an array of 0 and 1 character bytes for each bit of the argument 220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) 221069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 222069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public static byte[] toAsciiBytes(byte[] raw) { 223069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (raw == null || raw.length == 0) { 224069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return EMPTY_BYTE_ARRAY; 225069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 226069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // get 8 times the bytes with 3 bit shifts to the left of the length 227069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project byte[] l_ascii = new byte[raw.length << 3]; 228069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /* 229069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the 230069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * loop. 231069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 232069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { 233069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project for (int bits = 0; bits < BITS.length; ++bits) { 234069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if ((raw[ii] & BITS[bits]) == 0) { 235069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project l_ascii[jj - bits] = '0'; 236069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } else { 237069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project l_ascii[jj - bits] = '1'; 238069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 239069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 240069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 241069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return l_ascii; 242069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 243069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 244069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 245069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Converts an array of raw binary data into an array of ascii 0 and 1 characters. 246069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 247069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param raw 248069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the raw binary data to convert 249069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return an array of 0 and 1 characters for each bit of the argument 250069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) 251069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 252069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public static char[] toAsciiChars(byte[] raw) { 253069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (raw == null || raw.length == 0) { 254069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return EMPTY_CHAR_ARRAY; 255069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 256069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project // get 8 times the bytes with 3 bit shifts to the left of the length 257069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project char[] l_ascii = new char[raw.length << 3]; 258069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /* 259069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the 260069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * loop. 261069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 262069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { 263069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project for (int bits = 0; bits < BITS.length; ++bits) { 264069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if ((raw[ii] & BITS[bits]) == 0) { 265069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project l_ascii[jj - bits] = '0'; 266069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } else { 267069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project l_ascii[jj - bits] = '1'; 268069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 269069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 270069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 271069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return l_ascii; 272069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 273069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 274069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 275069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Converts an array of raw binary data into a String of ascii 0 and 1 characters. 276069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 277069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param raw 278069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the raw binary data to convert 279069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return a String of 0 and 1 characters representing the binary data 280069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) 281069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 282069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public static String toAsciiString(byte[] raw) { 283069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return new String(toAsciiChars(raw)); 284069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 285069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} 286