FileDocCategorySizeDatePackage
QuotedPrintableCodec.javaAPI DocAndroid 1.5 API14498Wed May 06 22:41:10 BST 2009org.apache.commons.codec.net

QuotedPrintableCodec

public class QuotedPrintableCodec extends Object implements StringEncoder, BinaryEncoder, StringDecoder, BinaryDecoder

Codec for the Quoted-Printable section of RFC 1521 .

The Quoted-Printable encoding is intended to represent data that largely consists of octets that correspond to printable characters in the ASCII character set. It encodes the data in such a way that the resulting octets are unlikely to be modified by mail transport. If the data being encoded are mostly ASCII text, the encoded form of the data remains largely recognizable by humans. A body which is entirely ASCII may also be encoded in Quoted-Printable to ensure the integrity of the data should the message pass through a character- translating, and/or line-wrapping gateway.

Note:

Rules #3, #4, and #5 of the quoted-printable spec are not implemented yet because the complete quoted-printable spec does not lend itself well into the byte[] oriented codec framework. Complete the codec once the steamable codec framework is ready. The motivation behind providing the codec in a partial form is that it can already come in handy for those applications that do not require quoted-printable line formatting (rules #3, #4, #5), for instance Q codec.

see
RFC 1521 MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
author
Apache Software Foundation
since
1.3
version
$Id: QuotedPrintableCodec.java,v 1.7 2004/04/09 22:21:07 ggregory Exp $

Fields Summary
private String
charset
The default charset used for string decoding and encoding.
private static final BitSet
PRINTABLE_CHARS
BitSet of printable characters as defined in RFC 1521.
private static byte
ESCAPE_CHAR
private static byte
TAB
private static byte
SPACE
Constructors Summary
public QuotedPrintableCodec()
Default constructor.

    // Static initializer for printable chars collection
     
        // alpha characters
        for (int i = 33; i <= 60; i++) {
            PRINTABLE_CHARS.set(i);
        }
        for (int i = 62; i <= 126; i++) {
            PRINTABLE_CHARS.set(i);
        }
        PRINTABLE_CHARS.set(TAB);
        PRINTABLE_CHARS.set(SPACE);
    
        super();
    
public QuotedPrintableCodec(String charset)
Constructor which allows for the selection of a default charset

param
charset the default string charset to use.

        super();
        this.charset = charset;
    
Methods Summary
public java.lang.Stringdecode(java.lang.String pString)
Decodes a quoted-printable string into its original form using the default string charset. Escaped characters are converted back to their original representation.

param
pString quoted-printable string to convert into its original form
return
original string
throws
DecoderException Thrown if quoted-printable decoding is unsuccessful
throws
UnsupportedEncodingException Thrown if charset is not supported
see
#getDefaultCharset()

        if (pString == null) {
            return null;
        }
        try {
            return decode(pString, getDefaultCharset());
        } catch (UnsupportedEncodingException e) {
            throw new DecoderException(e.getMessage());
        }
    
public java.lang.Objectdecode(java.lang.Object pObject)
Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original representation.

param
pObject quoted-printable object to convert into its original form
return
original object
throws
DecoderException Thrown if quoted-printable decoding is not applicable to objects of this type if decoding is unsuccessful

        if (pObject == null) {
            return null;
        } else if (pObject instanceof byte[]) {
            return decode((byte[]) pObject);
        } else if (pObject instanceof String) {
            return decode((String) pObject);
        } else {
            throw new DecoderException("Objects of type "
                + pObject.getClass().getName()
                + " cannot be quoted-printable decoded");
        }
    
public byte[]decode(byte[] bytes)
Decodes an array of quoted-printable characters into an array of original bytes. Escaped characters are converted back to their original representation.

This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in RFC 1521.

param
bytes array of quoted-printable characters
return
array of original bytes
throws
DecoderException Thrown if quoted-printable decoding is unsuccessful

        return decodeQuotedPrintable(bytes);
    
public java.lang.Stringdecode(java.lang.String pString, java.lang.String charset)
Decodes a quoted-printable string into its original form using the specified string charset. Escaped characters are converted back to their original representation.

param
pString quoted-printable string to convert into its original form
param
charset the original string charset
return
original string
throws
DecoderException Thrown if quoted-printable decoding is unsuccessful
throws
UnsupportedEncodingException Thrown if charset is not supported

        if (pString == null) {
            return null;
        }
        return new String(decode(pString.getBytes(StringEncodings.US_ASCII)), charset);
    
public static final byte[]decodeQuotedPrintable(byte[] bytes)
Decodes an array quoted-printable characters into an array of original bytes. Escaped characters are converted back to their original representation.

This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in RFC 1521.

param
bytes array of quoted-printable characters
return
array of original bytes
throws
DecoderException Thrown if quoted-printable decoding is unsuccessful

        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();
    
public java.lang.Objectencode(java.lang.Object pObject)
Encodes an object into its quoted-printable safe form. Unsafe characters are escaped.

param
pObject string to convert to a quoted-printable form
return
quoted-printable object
throws
EncoderException Thrown if quoted-printable encoding is not applicable to objects of this type or if encoding is unsuccessful

        if (pObject == null) {
            return null;
        } else if (pObject instanceof byte[]) {
            return encode((byte[]) pObject);
        } else if (pObject instanceof String) {
            return encode((String) pObject);
        } else {
            throw new EncoderException("Objects of type "
                + pObject.getClass().getName()
                + " cannot be quoted-printable encoded");
        }
    
public java.lang.Stringencode(java.lang.String pString, java.lang.String charset)
Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped.

This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in RFC 1521 and is suitable for encoding binary data and unformatted text.

param
pString string to convert to quoted-printable form
param
charset the charset for pString
return
quoted-printable string
throws
UnsupportedEncodingException Thrown if the charset is not supported

        if (pString == null) {
            return null;
        }
        return new String(encode(pString.getBytes(charset)), StringEncodings.US_ASCII);
    
public byte[]encode(byte[] bytes)
Encodes an array of bytes into an array of quoted-printable 7-bit characters. Unsafe characters are escaped.

This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in RFC 1521 and is suitable for encoding binary data and unformatted text.

param
bytes array of bytes to be encoded
return
array of bytes containing quoted-printable data

        return encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
    
public java.lang.Stringencode(java.lang.String pString)
Encodes a string into its quoted-printable form using the default string charset. Unsafe characters are escaped.

This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in RFC 1521 and is suitable for encoding binary data.

param
pString string to convert to quoted-printable form
return
quoted-printable string
throws
EncoderException Thrown if quoted-printable encoding is unsuccessful
see
#getDefaultCharset()

        if (pString == null) {
            return null;
        }
        try {
            return encode(pString, getDefaultCharset());
        } catch (UnsupportedEncodingException e) {
            throw new EncoderException(e.getMessage());
        }
    
private static final voidencodeQuotedPrintable(int b, java.io.ByteArrayOutputStream buffer)
Encodes byte into its quoted-printable representation.

param
b byte to encode
param
buffer the buffer to write to

        buffer.write(ESCAPE_CHAR);
        char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
        char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
        buffer.write(hex1);
        buffer.write(hex2);
    
public static final byte[]encodeQuotedPrintable(java.util.BitSet printable, byte[] bytes)
Encodes an array of bytes into an array of quoted-printable 7-bit characters. Unsafe characters are escaped.

This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in RFC 1521 and is suitable for encoding binary data and unformatted text.

param
printable bitset of characters deemed quoted-printable
param
bytes array of bytes to be encoded
return
array of bytes containing quoted-printable data

        if (bytes == null) {
            return null;
        }
        if (printable == null) {
            printable = PRINTABLE_CHARS;
        }
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        for (int i = 0; i < bytes.length; i++) {
            int b = bytes[i];
            if (b < 0) {
                b = 256 + b;
            }
            if (printable.get(b)) {
                buffer.write(b);
            } else {
                encodeQuotedPrintable(b, buffer);
            }
        }
        return buffer.toByteArray();
    
public java.lang.StringgetDefaultCharset()
Returns the default charset used for string decoding and encoding.

return
the default string charset.

        return this.charset;