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();
}
Aggregations