FileDocCategorySizeDatePackage
QuotedPrintableEncoding.javaAPI DocphoneME MR2 API (J2ME)6074Wed May 02 18:00:28 BST 2007com.sun.kvem.midp.pim.formats

QuotedPrintableEncoding

public class QuotedPrintableEncoding extends Object
Interprets the Quoted-Printable encoding. The Quoted-Printable Encoding is defined in RFC 2045.

Fields Summary
Constructors Summary
Methods Summary
public static byte[]fromQuotedPrintable(java.lang.String sdata)
Converts a quoted-printable string to a byte array.

param
sdata input data to be converted
return
processed data from quoted printable

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream whitespaceAccumulator =
            new ByteArrayOutputStream();
        char[] data = sdata.toCharArray();
        byte[] linebreak = Configuration.getProperty("file.linebreak").
            getBytes();
        boolean followingEqualsSign = false;
        for (int i = 0; i < data.length; i++) {
            char currSym = data[i];
            if (followingEqualsSign) {
                switch (currSym) {
                    case '\r":
                        // RFC 2045: check for soft break (=CRLF)
                        if (i + 1 < data.length) {
                            if (data[i + 1] == '\n") {
                                // concatenate lines
                                i++;
                                break;
                            }
                        }
                    default: {
                        // inlined byte
                        if (i < data.length - 1) {
                            String charCode = sdata.substring(i, i + 2);
                            i ++;
                            try {
                                out.write(Integer.parseInt(charCode, 16));
                                break;
                            } catch (NumberFormatException nfe) {
                                // illegal data. write the data as is
                                out.write('=");
                                out.write(charCode.charAt(0));
                                out.write(charCode.charAt(1));
                            }
                        } else {
                            // '=' is the penultimate character, which is also
                            // illegal. write the data as is
                            out.write('=");
                            out.write(currSym);
                        }
                    }
                }
                followingEqualsSign = false;
            } else {
                try {
                    /**
                     * RFC 2045: Control characters other than TAB, or CR and LF
                     * as parts of CRLF pairs, must not appear. The same is true
                     * for octets with decimal values greater than 126.  If
                     * found in incoming quoted-printable data by a decoder, a
                     * robust implementation might exclude them from the
                     * decoded data.
                     */
                    if (((currSym < ' ") &&
                        (currSym != '\t") &&
                        (currSym != '\r")) ||
                        (currSym > 126)) { // ignore
                        continue;
                    }
                    switch (currSym) {
                        case '=": {
                            out.write(whitespaceAccumulator.toByteArray());
                            whitespaceAccumulator.reset();
                            followingEqualsSign = true;
                            break;
                        }
                        case '\t":
                        case ' ":
                            whitespaceAccumulator.write(currSym);
                            break;
                        case '\r": // must be a part of CRLF (RFC 2045)
                            if (i + 1 < data.length) {
                                if (data[i + 1] == '\n") {
                                    whitespaceAccumulator.reset();
                                    out.write(linebreak, 0, linebreak.length);
                                    i ++; // skip LF
                                }
                            }
                            break;
                        default: {
                            out.write(whitespaceAccumulator.toByteArray());
                            whitespaceAccumulator.reset();
                            out.write(currSym);
                        }
                    }
                } catch (IOException e) {
                    // the compiler claims that an IOException can occur
                    // on a call to toByteArray(). The javadocs and
                    // the ByteArrayOutputStream class file claim otherwise.

                }
            }
        }
        try {
            out.write(whitespaceAccumulator.toByteArray());
        } catch (IOException e) {
            // same story as above.
        }
        return out.toByteArray();