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 $
34d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *
35d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * @deprecated Please use {@link java.net.URL#openConnection} instead.
36d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
37d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *     for further details.
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
39d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath@Deprecated
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class BinaryCodec implements BinaryDecoder, BinaryEncoder {
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /*
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * tried to avoid using ArrayUtils to minimize dependencies while using these empty arrays - dep is just not worth
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * it.
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Empty char array. */
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final char[] EMPTY_CHAR_ARRAY = new char[0];
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Empty byte array. */
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Mask for bit 0 of a byte. */
52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final int BIT_0 = 1;
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Mask for bit 1 of a byte. */
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final int BIT_1 = 0x02;
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Mask for bit 2 of a byte. */
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final int BIT_2 = 0x04;
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Mask for bit 3 of a byte. */
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final int BIT_3 = 0x08;
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Mask for bit 4 of a byte. */
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final int BIT_4 = 0x10;
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Mask for bit 5 of a byte. */
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final int BIT_5 = 0x20;
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Mask for bit 6 of a byte. */
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final int BIT_6 = 0x40;
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Mask for bit 7 of a byte. */
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final int BIT_7 = 0x80;
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
75069490a5ca2fd1988d29daf45d892f47ad665115The 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};
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Converts an array of raw binary data into an array of ascii 0 and 1 characters.
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param raw
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  the raw binary data to convert
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return 0 and 1 ascii character bytes one for each bit of the argument
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see org.apache.commons.codec.BinaryEncoder#encode(byte[])
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public byte[] encode(byte[] raw) {
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return toAsciiBytes(raw);
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Converts an array of raw binary data into an array of ascii 0 and 1 chars.
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param raw
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  the raw binary data to convert
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return 0 and 1 ascii character chars one for each bit of the argument
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws EncoderException
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  if the argument is not a byte[]
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see org.apache.commons.codec.Encoder#encode(java.lang.Object)
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public Object encode(Object raw) throws EncoderException {
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (!(raw instanceof byte[])) {
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new EncoderException("argument not a byte array");
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return toAsciiChars((byte[]) raw);
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Decodes a byte array where each byte represents an ascii '0' or '1'.
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param ascii
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  each byte represents an ascii '0' or '1'
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws DecoderException
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  if argument is not a byte[], char[] or String
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see org.apache.commons.codec.Decoder#decode(java.lang.Object)
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public Object decode(Object ascii) throws DecoderException {
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (ascii == null) {
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return EMPTY_BYTE_ARRAY;
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (ascii instanceof byte[]) {
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return fromAscii((byte[]) ascii);
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (ascii instanceof char[]) {
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return fromAscii((char[]) ascii);
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (ascii instanceof String) {
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return fromAscii(((String) ascii).toCharArray());
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        throw new DecoderException("argument not a byte array");
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Decodes a byte array where each byte represents an ascii '0' or '1'.
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param ascii
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  each byte represents an ascii '0' or '1'
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see org.apache.commons.codec.Decoder#decode(Object)
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public byte[] decode(byte[] ascii) {
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return fromAscii(ascii);
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Decodes a String where each char of the String represents an ascii '0' or '1'.
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param ascii
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  String of '0' and '1' characters
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see org.apache.commons.codec.Decoder#decode(Object)
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public byte[] toByteArray(String ascii) {
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (ascii == null) {
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return EMPTY_BYTE_ARRAY;
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return fromAscii(ascii.toCharArray());
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // ------------------------------------------------------------------------
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    //
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // static codec operations
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    //
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    // ------------------------------------------------------------------------
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Decodes a byte array where each char represents an ascii '0' or '1'.
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param ascii
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  each char represents an ascii '0' or '1'
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the raw encoded binary where each bit corresponds to a char in the char array argument
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static byte[] fromAscii(char[] ascii) {
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (ascii == null || ascii.length == 0) {
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return EMPTY_BYTE_ARRAY;
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // get length/8 times bytes with 3 bit shifts to the right of the length
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        byte[] l_raw = new byte[ascii.length >> 3];
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        /*
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * loop.
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         */
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            for (int bits = 0; bits < BITS.length; ++bits) {
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (ascii[jj - bits] == '1') {
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    l_raw[ii] |= BITS[bits];
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                }
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return l_raw;
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Decodes a byte array where each byte represents an ascii '0' or '1'.
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param ascii
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  each byte represents an ascii '0' or '1'
196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument
197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static byte[] fromAscii(byte[] ascii) {
199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (ascii == null || ascii.length == 0) {
200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return EMPTY_BYTE_ARRAY;
201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // get length/8 times bytes with 3 bit shifts to the right of the length
203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        byte[] l_raw = new byte[ascii.length >> 3];
204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        /*
205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * loop.
207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         */
208069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
209069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            for (int bits = 0; bits < BITS.length; ++bits) {
210069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (ascii[jj - bits] == '1') {
211069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    l_raw[ii] |= BITS[bits];
212069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                }
213069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
214069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
215069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return l_raw;
216069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
217069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
218069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
219069490a5ca2fd1988d29daf45d892f47ad665115The 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
220069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * char.
221069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
222069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param raw
223069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  the raw binary data to convert
224069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return an array of 0 and 1 character bytes for each bit of the argument
225069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see org.apache.commons.codec.BinaryEncoder#encode(byte[])
226069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
227069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static byte[] toAsciiBytes(byte[] raw) {
228069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (raw == null || raw.length == 0) {
229069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return EMPTY_BYTE_ARRAY;
230069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
231069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // get 8 times the bytes with 3 bit shifts to the left of the length
232069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        byte[] l_ascii = new byte[raw.length << 3];
233069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        /*
234069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
235069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * loop.
236069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         */
237069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
238069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            for (int bits = 0; bits < BITS.length; ++bits) {
239069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if ((raw[ii] & BITS[bits]) == 0) {
240069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    l_ascii[jj - bits] = '0';
241069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                } else {
242069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    l_ascii[jj - bits] = '1';
243069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                }
244069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
245069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
246069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return l_ascii;
247069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
248069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
249069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
250069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Converts an array of raw binary data into an array of ascii 0 and 1 characters.
251069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
252069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param raw
253069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  the raw binary data to convert
254069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return an array of 0 and 1 characters for each bit of the argument
255069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see org.apache.commons.codec.BinaryEncoder#encode(byte[])
256069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
257069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static char[] toAsciiChars(byte[] raw) {
258069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (raw == null || raw.length == 0) {
259069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return EMPTY_CHAR_ARRAY;
260069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
261069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // get 8 times the bytes with 3 bit shifts to the left of the length
262069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        char[] l_ascii = new char[raw.length << 3];
263069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        /*
264069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
265069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * loop.
266069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         */
267069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
268069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            for (int bits = 0; bits < BITS.length; ++bits) {
269069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if ((raw[ii] & BITS[bits]) == 0) {
270069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    l_ascii[jj - bits] = '0';
271069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                } else {
272069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    l_ascii[jj - bits] = '1';
273069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                }
274069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
275069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
276069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return l_ascii;
277069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
278069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
279069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
280069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Converts an array of raw binary data into a String of ascii 0 and 1 characters.
281069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
282069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param raw
283069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  the raw binary data to convert
284069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return a String of 0 and 1 characters representing the binary data
285069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see org.apache.commons.codec.BinaryEncoder#encode(byte[])
286069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
287069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static String toAsciiString(byte[] raw) {
288069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return new String(toAsciiChars(raw));
289069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
290069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project}
291