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

QCodec

public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder

Similar to the Quoted-Printable content-transfer-encoding defined in RFC 1521 and designed to allow text containing mostly ASCII characters to be decipherable on an ASCII terminal without decoding.

RFC 1522 describes techniques to allow the encoding of non-ASCII text in various portions of a RFC 822 [2] message header, in a manner which is unlikely to confuse existing message handling software.

see
MIME (Multipurpose Internet Mail Extensions) Part Two: Message Header Extensions for Non-ASCII Text
author
Apache Software Foundation
since
1.3
version
$Id: QCodec.java,v 1.6 2004/05/24 00:24:32 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 1522.
private static byte
BLANK
private static byte
UNDERSCORE
private boolean
encodeBlanks
Constructors Summary
public QCodec()
Default constructor.


           
      
        super();
    
public QCodec(String charset)
Constructor which allows for the selection of a default charset

param
charset the default string charset to use.
see
JRE character encoding names

        super();
        this.charset = charset;
    
Methods Summary
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 A decoder exception is thrown if a failure condition is encountered during the decode process.

        if (pObject == null) {
            return null;
        } else if (pObject instanceof String) {
            return decode((String) pObject);
        } else {
            throw new DecoderException("Objects of type "
                + pObject.getClass().getName()
                + " cannot be decoded using Q codec");
        }
    
public java.lang.Stringdecode(java.lang.String pString)
Decodes a quoted-printable string into its original form. 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 A decoder exception is thrown if a failure condition is encountered during the decode process.

        if (pString == null) {
            return null;
        }
        try {
            return decodeText(pString);
        } catch (UnsupportedEncodingException e) {
            throw new DecoderException(e.getMessage());
        }
    
protected byte[]doDecoding(byte[] bytes)

        if (bytes == null) {
            return null;
        }
        boolean hasUnderscores = false;
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] == UNDERSCORE) {
                hasUnderscores = true;
                break;
            }
        }
        if (hasUnderscores) {
            byte[] tmp = new byte[bytes.length];
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                if (b != UNDERSCORE) {
                    tmp[i] = b;
                } else {
                    tmp[i] = BLANK;
                }
            }
            return QuotedPrintableCodec.decodeQuotedPrintable(tmp);
        } 
        return QuotedPrintableCodec.decodeQuotedPrintable(bytes);       
    
protected byte[]doEncoding(byte[] bytes)

        if (bytes == null) {
            return null;
        }
        byte[] data = QuotedPrintableCodec.encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
        if (this.encodeBlanks) {
            for (int i = 0; i < data.length; i++) {
                if (data[i] == BLANK) {
                    data[i] = UNDERSCORE;
                }
            }
        }
        return data;
    
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.

param
pString string to convert to quoted-printable form
param
charset the charset for pString
return
quoted-printable string
throws
EncoderException thrown if a failure condition is encountered during the encoding process.

        if (pString == null) {
            return null;
        }
        try {
            return encodeText(pString, charset);
        } catch (UnsupportedEncodingException e) {
            throw new EncoderException(e.getMessage());
        }
    
public java.lang.Stringencode(java.lang.String pString)
Encodes a string into its quoted-printable form using the default charset. Unsafe characters are escaped.

param
pString string to convert to quoted-printable form
return
quoted-printable string
throws
EncoderException thrown if a failure condition is encountered during the encoding process.

        if (pString == null) {
            return null;
        }
        return encode(pString, getDefaultCharset());
    
public java.lang.Objectencode(java.lang.Object pObject)
Encodes an object into its quoted-printable form using the default charset. Unsafe characters are escaped.

param
pObject object to convert to quoted-printable form
return
quoted-printable object
throws
EncoderException thrown if a failure condition is encountered during the encoding process.

        if (pObject == null) {
            return null;
        } else if (pObject instanceof String) {
            return encode((String) pObject);
        } else {
            throw new EncoderException("Objects of type "
                + pObject.getClass().getName()
                + " cannot be encoded using Q codec");
        }
    
public java.lang.StringgetDefaultCharset()
The default charset used for string decoding and encoding.

return
the default string charset.

        return this.charset;
    
protected java.lang.StringgetEncoding()

        return "Q";
    
public booleanisEncodeBlanks()
Tests if optional tranformation of SPACE characters is to be used

return
true if SPACE characters are to be transformed, false otherwise

        return this.encodeBlanks;
    
public voidsetEncodeBlanks(boolean b)
Defines whether optional tranformation of SPACE characters is to be used

param
b true if SPACE characters are to be transformed, false otherwise

        this.encodeBlanks = b;