Search in sources :

Example 41 with DecoderException

use of org.apache.commons.codec.DecoderException in project platform_external_apache-http by android.

the class QuotedPrintableCodec method decodeQuotedPrintable.

/**
 * Decodes an array quoted-printable characters into an array of original bytes. Escaped characters are converted
 * back to their original representation.
 *
 * <p>
 * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in
 * RFC 1521.
 * </p>
 *
 * @param bytes
 *                  array of quoted-printable characters
 * @return array of original bytes
 * @throws DecoderException
 *                  Thrown if quoted-printable decoding is unsuccessful
 */
public static final byte[] decodeQuotedPrintable(byte[] bytes) throws DecoderException {
    if (bytes == null) {
        return null;
    }
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    for (int i = 0; i < bytes.length; i++) {
        int b = bytes[i];
        if (b == ESCAPE_CHAR) {
            try {
                int u = Character.digit((char) bytes[++i], 16);
                int l = Character.digit((char) bytes[++i], 16);
                if (u == -1 || l == -1) {
                    throw new DecoderException("Invalid quoted-printable encoding");
                }
                buffer.write((char) ((u << 4) + l));
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new DecoderException("Invalid quoted-printable encoding");
            }
        } else {
            buffer.write(b);
        }
    }
    return buffer.toByteArray();
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

DecoderException (org.apache.commons.codec.DecoderException)41 IOException (java.io.IOException)16 CharSequenceX (com.samourai.wallet.util.CharSequenceX)8 MnemonicException (org.bitcoinj.crypto.MnemonicException)8 JSONException (org.json.JSONException)7 DecryptionException (com.samourai.wallet.crypto.DecryptionException)6 ArrayList (java.util.ArrayList)6 Intent (android.content.Intent)5 ProgressDialog (android.app.ProgressDialog)4 HD_Wallet (com.samourai.wallet.hd.HD_Wallet)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HashMap (java.util.HashMap)4 List (java.util.List)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3 Cipher (javax.crypto.Cipher)3 AddressFormatException (org.bitcoinj.core.AddressFormatException)3