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

RFC1522Codec

public abstract class RFC1522Codec extends Object

Implements methods common to all codecs defined in RFC 1522.

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: RFC1522Codec.java,v 1.2 2004/04/09 22:21:43 ggregory Exp $

Fields Summary
Constructors Summary
Methods Summary
protected java.lang.StringdecodeText(java.lang.String text)
Applies an RFC 1522 compliant decoding scheme to the given string of text. This method processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete class to perform the specific deconding.

param
text a string to decode
throws
DecoderException thrown if there is an error conidition during the Decoding process.
throws
UnsupportedEncodingException thrown if charset specified in the "encoded-word" header is not supported

        if (text == null) {
            return null;
        }
        if ((!text.startsWith("=?")) || (!text.endsWith("?="))) {
            throw new DecoderException("RFC 1522 violation: malformed encoded content");
        }
        int termnator = text.length() - 2;
        int from = 2;
        int to = text.indexOf("?", from);
        if ((to == -1) || (to == termnator)) {
            throw new DecoderException("RFC 1522 violation: charset token not found");
        }
        String charset = text.substring(from, to);
        if (charset.equals("")) {
            throw new DecoderException("RFC 1522 violation: charset not specified");
        }
        from = to + 1;
        to = text.indexOf("?", from);
        if ((to == -1) || (to == termnator)) {
            throw new DecoderException("RFC 1522 violation: encoding token not found");
        }
        String encoding = text.substring(from, to);
        if (!getEncoding().equalsIgnoreCase(encoding)) {
            throw new DecoderException("This codec cannot decode " + 
                encoding + " encoded content");
        }
        from = to + 1;
        to = text.indexOf("?", from);
        byte[] data = text.substring(from, to).getBytes(StringEncodings.US_ASCII);
        data = doDecoding(data); 
        return new String(data, charset);
    
protected abstract byte[]doDecoding(byte[] bytes)
Decodes an array of bytes using the defined encoding scheme

param
bytes Data to be decoded
return
a byte array that contains decoded data
throws
DecoderException A decoder exception is thrown if a Decoder encounters a failure condition during the decode process.

protected abstract byte[]doEncoding(byte[] bytes)
Encodes an array of bytes using the defined encoding scheme

param
bytes Data to be encoded
return
A byte array containing the encoded data
throws
EncoderException thrown if the Encoder encounters a failure condition during the encoding process.

protected java.lang.StringencodeText(java.lang.String text, java.lang.String charset)
Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset. This method constructs the "encoded-word" header common to all the RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete class to perform the specific enconding.

param
text a string to encode
param
charset a charset to be used
return
RFC 1522 compliant "encoded-word"
throws
EncoderException thrown if there is an error conidition during the Encoding process.
throws
UnsupportedEncodingException thrown if charset is not supported
see
JRE character encoding names

        if (text == null) {
            return null;
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append("=?"); 
        buffer.append(charset); 
        buffer.append('?"); 
        buffer.append(getEncoding()); 
        buffer.append('?");
        byte [] rawdata = doEncoding(text.getBytes(charset)); 
        buffer.append(new String(rawdata, StringEncodings.US_ASCII));
        buffer.append("?="); 
        return buffer.toString();
    
protected abstract java.lang.StringgetEncoding()
Returns the codec name (referred to as encoding in the RFC 1522)

return
name of the codec