Example usage for org.apache.commons.codec.net QuotedPrintableCodec decodeQuotedPrintable

List of usage examples for org.apache.commons.codec.net QuotedPrintableCodec decodeQuotedPrintable

Introduction

In this page you can find the example usage for org.apache.commons.codec.net QuotedPrintableCodec decodeQuotedPrintable.

Prototype

public static final byte[] decodeQuotedPrintable(byte[] bytes) throws DecoderException 

Source Link

Document

Decodes an array quoted-printable characters into an array of original bytes.

Usage

From source file:android.syncml.pim.VDataBuilder.java

public void propertyValues(Collection<String> values) {
    curPropNode.propValue_vector = values;
    curPropNode.propValue = listToString(values);
    //decode value string to propValue_byts
    if (curPropNode.paraMap.containsKey("ENCODING")) {
        if (curPropNode.paraMap.getAsString("ENCODING").equalsIgnoreCase("BASE64")) {
            curPropNode.propValue_byts = Base64.decodeBase64(curPropNode.propValue.replaceAll(" ", "")
                    .replaceAll("\t", "").replaceAll("\r\n", "").getBytes());
        }//  w  ww.  ja v  a  2 s  .  co m
        if (curPropNode.paraMap.getAsString("ENCODING").equalsIgnoreCase("QUOTED-PRINTABLE")) {
            try {
                curPropNode.propValue_byts = QuotedPrintableCodec.decodeQuotedPrintable(
                        curPropNode.propValue.replaceAll("= ", " ").replaceAll("=\t", "\t").getBytes());
                curPropNode.propValue = new String(curPropNode.propValue_byts);
            } catch (Exception e) {
                System.out.println("=Decode quoted-printable exception.");
                e.printStackTrace();
            }
        }
    }
    curVNode.propList.add(curPropNode);
}

From source file:a_vcard.android.syncml.pim.VDataBuilder.java

private String handleOneValue(String value, String targetCharset, String encoding) {
    if (encoding != null) {
        if (encoding.equals("BASE64") || encoding.equals("B")) {
            // Assume BASE64 is used only when the number of values is 1.
            mCurrentPropNode.propValue_bytes = Base64.decodeBase64(value.getBytes());
            return value;
        } else if (encoding.equals("QUOTED-PRINTABLE")) {
            String quotedPrintable = value.replaceAll("= ", " ").replaceAll("=\t", "\t");
            String[] lines;//from   w  ww .jav  a2  s. c o m
            if (mStrictLineBreakParsing) {
                lines = quotedPrintable.split("\r\n");
            } else {
                StringBuilder builder = new StringBuilder();
                int length = quotedPrintable.length();
                ArrayList<String> list = new ArrayList<String>();
                for (int i = 0; i < length; i++) {
                    char ch = quotedPrintable.charAt(i);
                    if (ch == '\n') {
                        list.add(builder.toString());
                        builder = new StringBuilder();
                    } else if (ch == '\r') {
                        list.add(builder.toString());
                        builder = new StringBuilder();
                        if (i < length - 1) {
                            char nextCh = quotedPrintable.charAt(i + 1);
                            if (nextCh == '\n') {
                                i++;
                            }
                        }
                    } else {
                        builder.append(ch);
                    }
                }
                String finalLine = builder.toString();
                if (finalLine.length() > 0) {
                    list.add(finalLine);
                }
                lines = list.toArray(new String[0]);
            }
            StringBuilder builder = new StringBuilder();
            for (String line : lines) {
                if (line.endsWith("=")) {
                    line = line.substring(0, line.length() - 1);
                }
                builder.append(line);
            }
            byte[] bytes;
            try {
                bytes = builder.toString().getBytes(mSourceCharset);
            } catch (UnsupportedEncodingException e1) {
                Log.e(LOG_TAG, "Failed to encode: charset=" + mSourceCharset);
                bytes = builder.toString().getBytes();
            }

            try {
                bytes = QuotedPrintableCodec.decodeQuotedPrintable(bytes);
            } catch (DecoderException e) {
                Log.e(LOG_TAG, "Failed to decode quoted-printable: " + e);
                return "";
            }

            try {
                return new String(bytes, targetCharset);
            } catch (UnsupportedEncodingException e) {
                Log.e(LOG_TAG, "Failed to encode: charset=" + targetCharset);
                return new String(bytes);
            }
        }
        // Unknown encoding. Fall back to default.
    }
    return encodeString(value, targetCharset);
}

From source file:android.pim.vcard.VNodeBuilder.java

private String handleOneValue(String value, String targetCharset, String encoding) {
    if (encoding != null) {
        encoding = encoding.toUpperCase();
        if (encoding.equals("BASE64") || encoding.equals("B")) {
            // Assume BASE64 is used only when the number of values is 1.
            mCurrentPropNode.propValue_bytes = Base64.decodeBase64(value.getBytes());
            return value;
        } else if (encoding.equals("QUOTED-PRINTABLE")) {
            String quotedPrintable = value.replaceAll("= ", " ").replaceAll("=\t", "\t");
            String[] lines;//from   w w  w  .  j  a  v  a 2s  .  c o  m
            if (mStrictLineBreakParsing) {
                lines = quotedPrintable.split("\r\n");
            } else {
                StringBuilder builder = new StringBuilder();
                int length = quotedPrintable.length();
                ArrayList<String> list = new ArrayList<String>();
                for (int i = 0; i < length; i++) {
                    char ch = quotedPrintable.charAt(i);
                    if (ch == '\n') {
                        list.add(builder.toString());
                        builder = new StringBuilder();
                    } else if (ch == '\r') {
                        list.add(builder.toString());
                        builder = new StringBuilder();
                        if (i < length - 1) {
                            char nextCh = quotedPrintable.charAt(i + 1);
                            if (nextCh == '\n') {
                                i++;
                            }
                        }
                    } else {
                        builder.append(ch);
                    }
                }
                String finalLine = builder.toString();
                if (finalLine.length() > 0) {
                    list.add(finalLine);
                }
                lines = list.toArray(new String[0]);
            }
            StringBuilder builder = new StringBuilder();
            for (String line : lines) {
                if (line.endsWith("=")) {
                    line = line.substring(0, line.length() - 1);
                }
                builder.append(line);
            }
            byte[] bytes;
            try {
                bytes = builder.toString().getBytes(mSourceCharset);
            } catch (UnsupportedEncodingException e1) {
                Log.e(LOG_TAG, "Failed to encode: charset=" + mSourceCharset);
                bytes = builder.toString().getBytes();
            }

            try {
                bytes = QuotedPrintableCodec.decodeQuotedPrintable(bytes);
            } catch (DecoderException e) {
                Log.e(LOG_TAG, "Failed to decode quoted-printable: " + e);
                return "";
            }

            try {
                return new String(bytes, targetCharset);
            } catch (UnsupportedEncodingException e) {
                Log.e(LOG_TAG, "Failed to encode: charset=" + targetCharset);
                return new String(bytes);
            }
        }
        // Unknown encoding. Fall back to default.
    }
    return encodeString(value, targetCharset);
}

From source file:android.pim.vcard.VCardEntryConstructor.java

private String handleOneValue(String value, String charsetForDecodedBytes, String encoding) {
    if (encoding != null) {
        if (encoding.equals("BASE64") || encoding.equals("B")) {
            mCurrentProperty.setPropertyBytes(Base64.decodeBase64(value.getBytes()));
            return value;
        } else if (encoding.equals("QUOTED-PRINTABLE")) {
            // "= " -> " ", "=\t" -> "\t".
            // Previous code had done this replacement. Keep on the safe side.
            StringBuilder builder = new StringBuilder();
            int length = value.length();
            for (int i = 0; i < length; i++) {
                char ch = value.charAt(i);
                if (ch == '=' && i < length - 1) {
                    char nextCh = value.charAt(i + 1);
                    if (nextCh == ' ' || nextCh == '\t') {

                        builder.append(nextCh);
                        i++;/*from www  . j ava  2 s  .com*/
                        continue;
                    }
                }
                builder.append(ch);
            }
            String quotedPrintable = builder.toString();

            String[] lines;
            if (mStrictLineBreakParsing) {
                lines = quotedPrintable.split("\r\n");
            } else {
                builder = new StringBuilder();
                length = quotedPrintable.length();
                ArrayList<String> list = new ArrayList<String>();
                for (int i = 0; i < length; i++) {
                    char ch = quotedPrintable.charAt(i);
                    if (ch == '\n') {
                        list.add(builder.toString());
                        builder = new StringBuilder();
                    } else if (ch == '\r') {
                        list.add(builder.toString());
                        builder = new StringBuilder();
                        if (i < length - 1) {
                            char nextCh = quotedPrintable.charAt(i + 1);
                            if (nextCh == '\n') {
                                i++;
                            }
                        }
                    } else {
                        builder.append(ch);
                    }
                }
                String finalLine = builder.toString();
                if (finalLine.length() > 0) {
                    list.add(finalLine);
                }
                lines = list.toArray(new String[0]);
            }

            builder = new StringBuilder();
            for (String line : lines) {
                if (line.endsWith("=")) {
                    line = line.substring(0, line.length() - 1);
                }
                builder.append(line);
            }
            byte[] bytes;
            try {
                bytes = builder.toString().getBytes(mInputCharset);
            } catch (UnsupportedEncodingException e1) {
                Log.e(LOG_TAG, "Failed to encode: charset=" + mInputCharset);
                bytes = builder.toString().getBytes();
            }

            try {
                bytes = QuotedPrintableCodec.decodeQuotedPrintable(bytes);
            } catch (DecoderException e) {
                Log.e(LOG_TAG, "Failed to decode quoted-printable: " + e);
                return "";
            }

            try {
                return new String(bytes, charsetForDecodedBytes);
            } catch (UnsupportedEncodingException e) {
                Log.e(LOG_TAG, "Failed to encode: charset=" + charsetForDecodedBytes);
                return new String(bytes);
            }
        }
        // Unknown encoding. Fall back to default.
    }
    return encodeString(value, charsetForDecodedBytes);
}

From source file:android.pim.vcard.VCardDataBuilder.java

private String handleOneValue(String value, String targetCharset, String encoding) {
    if (encoding != null) {
        if (encoding.equals("BASE64") || encoding.equals("B")) {
            mCurrentProperty.setPropertyBytes(Base64.decodeBase64(value.getBytes()));
            return value;
        } else if (encoding.equals("QUOTED-PRINTABLE")) {
            // "= " -> " ", "=\t" -> "\t".
            // Previous code had done this replacement. Keep on the safe side.
            StringBuilder builder = new StringBuilder();
            int length = value.length();
            for (int i = 0; i < length; i++) {
                char ch = value.charAt(i);
                if (ch == '=' && i < length - 1) {
                    char nextCh = value.charAt(i + 1);
                    if (nextCh == ' ' || nextCh == '\t') {

                        builder.append(nextCh);
                        i++;//  w w w. j  a  v  a  2  s.  c om
                        continue;
                    }
                }
                builder.append(ch);
            }
            String quotedPrintable = builder.toString();

            String[] lines;
            if (mStrictLineBreakParsing) {
                lines = quotedPrintable.split("\r\n");
            } else {
                builder = new StringBuilder();
                length = quotedPrintable.length();
                ArrayList<String> list = new ArrayList<String>();
                for (int i = 0; i < length; i++) {
                    char ch = quotedPrintable.charAt(i);
                    if (ch == '\n') {
                        list.add(builder.toString());
                        builder = new StringBuilder();
                    } else if (ch == '\r') {
                        list.add(builder.toString());
                        builder = new StringBuilder();
                        if (i < length - 1) {
                            char nextCh = quotedPrintable.charAt(i + 1);
                            if (nextCh == '\n') {
                                i++;
                            }
                        }
                    } else {
                        builder.append(ch);
                    }
                }
                String finalLine = builder.toString();
                if (finalLine.length() > 0) {
                    list.add(finalLine);
                }
                lines = list.toArray(new String[0]);
            }

            builder = new StringBuilder();
            for (String line : lines) {
                if (line.endsWith("=")) {
                    line = line.substring(0, line.length() - 1);
                }
                builder.append(line);
            }
            byte[] bytes;
            try {
                bytes = builder.toString().getBytes(mSourceCharset);
            } catch (UnsupportedEncodingException e1) {
                Log.e(LOG_TAG, "Failed to encode: charset=" + mSourceCharset);
                bytes = builder.toString().getBytes();
            }

            try {
                bytes = QuotedPrintableCodec.decodeQuotedPrintable(bytes);
            } catch (DecoderException e) {
                Log.e(LOG_TAG, "Failed to decode quoted-printable: " + e);
                return "";
            }

            try {
                return new String(bytes, targetCharset);
            } catch (UnsupportedEncodingException e) {
                Log.e(LOG_TAG, "Failed to encode: charset=" + targetCharset);
                return new String(bytes);
            }
        }
        // Unknown encoding. Fall back to default.
    }
    return encodeString(value, targetCharset);
}

From source file:android.syncml.pim.vcard.VCardDataBuilder.java

private String handleOneValue(String value, String targetCharset, String encoding) {
    if (encoding != null) {
        if (encoding.equals("BASE64") || encoding.equals("B")) {
            mCurrentPropNode.propValue_bytes = Base64.decodeBase64(value.getBytes());
            return value;
        } else if (encoding.equals("QUOTED-PRINTABLE")) {
            // "= " -> " ", "=\t" -> "\t".
            // Previous code had done this replacement. Keep on the safe side.
            StringBuilder builder = new StringBuilder();
            int length = value.length();
            for (int i = 0; i < length; i++) {
                char ch = value.charAt(i);
                if (ch == '=' && i < length - 1) {
                    char nextCh = value.charAt(i + 1);
                    if (nextCh == ' ' || nextCh == '\t') {

                        builder.append(nextCh);
                        i++;/*  w  w  w .j a va  2s  .  c o m*/
                        continue;
                    }
                }
                builder.append(ch);
            }
            String quotedPrintable = builder.toString();

            String[] lines;
            if (mStrictLineBreakParsing) {
                lines = quotedPrintable.split("\r\n");
            } else {
                builder = new StringBuilder();
                length = quotedPrintable.length();
                ArrayList<String> list = new ArrayList<String>();
                for (int i = 0; i < length; i++) {
                    char ch = quotedPrintable.charAt(i);
                    if (ch == '\n') {
                        list.add(builder.toString());
                        builder = new StringBuilder();
                    } else if (ch == '\r') {
                        list.add(builder.toString());
                        builder = new StringBuilder();
                        if (i < length - 1) {
                            char nextCh = quotedPrintable.charAt(i + 1);
                            if (nextCh == '\n') {
                                i++;
                            }
                        }
                    } else {
                        builder.append(ch);
                    }
                }
                String finalLine = builder.toString();
                if (finalLine.length() > 0) {
                    list.add(finalLine);
                }
                lines = list.toArray(new String[0]);
            }

            builder = new StringBuilder();
            for (String line : lines) {
                if (line.endsWith("=")) {
                    line = line.substring(0, line.length() - 1);
                }
                builder.append(line);
            }
            byte[] bytes;
            try {
                bytes = builder.toString().getBytes(mSourceCharset);
            } catch (UnsupportedEncodingException e1) {
                Log.e(LOG_TAG, "Failed to encode: charset=" + mSourceCharset);
                bytes = builder.toString().getBytes();
            }

            try {
                bytes = QuotedPrintableCodec.decodeQuotedPrintable(bytes);
            } catch (DecoderException e) {
                Log.e(LOG_TAG, "Failed to decode quoted-printable: " + e);
                return "";
            }

            try {
                return new String(bytes, targetCharset);
            } catch (UnsupportedEncodingException e) {
                Log.e(LOG_TAG, "Failed to encode: charset=" + targetCharset);
                return new String(bytes);
            }
        }
        // Unknown encoding. Fall back to default.
    }
    return encodeString(value, targetCharset);
}

From source file:edu.stanford.muse.util.EmailUtils.java

private static void printBodyAndAttachmentsToMbox(String contents, EmailDocument ed, PrintWriter mbox,
        BlobStore blobStore) throws IOException, GeneralSecurityException {
    String frontier = "----=_Part_";
    List<Blob> attachments = null;
    if (ed != null)
        attachments = ed.attachments;/*from w w w . java2  s. c o  m*/
    boolean hasAttachments = !Util.nullOrEmpty(attachments) && blobStore != null;
    boolean isI18N = Util.isI18N(contents);

    if (!hasAttachments && !isI18N) {
        mbox.println();
        mbox.println(contents);
        mbox.println();
    } else {
        /*
         * This is a multi-part message in MIME format.
         * 
         * ------=_Part_
         * Content-Type: text/plain;
         * charset="iso-8859-1"
         * Content-Transfer-Encoding: 7bit
         */

        mbox.println("Content-Type: multipart/mixed; boundary=\"" + frontier + "\"\n"); // blank line

        mbox.println("This is a multi-part message in MIME format.\n"); // blank line

        mbox.println("--" + frontier);
        mbox.println("Content-Type: text/plain; charset=\"UTF-8\"");
        mbox.println("Content-Encoding: quoted-printable\n"); // need blank line after this
        try {
            byte encodedBytes[] = QuotedPrintableCodec.decodeQuotedPrintable(contents.getBytes());
            for (byte by : encodedBytes)
                mbox.print((char) by);
            mbox.println();
        } catch (DecoderException de) {
            log.warn("Exception trying to toString contents!" + de);
            mbox.println(contents);
            mbox.println();
        }
        //         mbox.println("--" + frontier + "--");

        // probably need to fix: other types of charset, encodings
        if (blobStore != null && attachments != null) {
            for (Blob b : attachments) {
                mbox.println("--" + frontier);
                mbox.println("Content-type: " + b.contentType);
                mbox.println("Content-transfer-encoding: base64");
                mbox.println("Content-Disposition: attachment;filename=\"" + b.filename + "\"\n");

                byte bytes[] = blobStore.getDataBytes(b);
                byte encodedBytes[] = Base64.encodeBase64(bytes, true);
                for (byte by : encodedBytes)
                    mbox.print((char) by);
            }
            // note: the --frontier-- line is needed only at the very end, after all attachments -- NOT after each attachment.
            // this used to be a bug.
            mbox.println("--" + frontier + "--\n");
        }
    }
}

From source file:android.pim.vcard.VCardUtils.java

/**
 * Unquotes given Quoted-Printable value. value must not be null.
 *//*from  w  w  w  .  ja va 2 s .c o  m*/
public static String parseQuotedPrintable(final String value, boolean strictLineBreaking, String sourceCharset,
        String targetCharset) {
    // "= " -> " ", "=\t" -> "\t".
    // Previous code had done this replacement. Keep on the safe side.
    final String quotedPrintable;
    {
        final StringBuilder builder = new StringBuilder();
        final int length = value.length();
        for (int i = 0; i < length; i++) {
            char ch = value.charAt(i);
            if (ch == '=' && i < length - 1) {
                char nextCh = value.charAt(i + 1);
                if (nextCh == ' ' || nextCh == '\t') {
                    builder.append(nextCh);
                    i++;
                    continue;
                }
            }
            builder.append(ch);
        }
        quotedPrintable = builder.toString();
    }

    String[] lines;
    if (strictLineBreaking) {
        lines = quotedPrintable.split("\r\n");
    } else {
        StringBuilder builder = new StringBuilder();
        final int length = quotedPrintable.length();
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < length; i++) {
            char ch = quotedPrintable.charAt(i);
            if (ch == '\n') {
                list.add(builder.toString());
                builder = new StringBuilder();
            } else if (ch == '\r') {
                list.add(builder.toString());
                builder = new StringBuilder();
                if (i < length - 1) {
                    char nextCh = quotedPrintable.charAt(i + 1);
                    if (nextCh == '\n') {
                        i++;
                    }
                }
            } else {
                builder.append(ch);
            }
        }
        final String lastLine = builder.toString();
        if (lastLine.length() > 0) {
            list.add(lastLine);
        }
        lines = list.toArray(new String[0]);
    }

    final StringBuilder builder = new StringBuilder();
    for (String line : lines) {
        if (line.endsWith("=")) {
            line = line.substring(0, line.length() - 1);
        }
        builder.append(line);
    }

    final String rawString = builder.toString();
    if (TextUtils.isEmpty(rawString)) {
        Log.w(LOG_TAG, "Given raw string is empty.");
    }

    byte[] rawBytes = null;
    try {
        rawBytes = rawString.getBytes(sourceCharset);
    } catch (UnsupportedEncodingException e) {
        Log.w(LOG_TAG, "Failed to decode: " + sourceCharset);
        rawBytes = rawString.getBytes();
    }

    byte[] decodedBytes = null;
    try {
        decodedBytes = QuotedPrintableCodec.decodeQuotedPrintable(rawBytes);
    } catch (DecoderException e) {
        Log.e(LOG_TAG, "DecoderException is thrown.");
        decodedBytes = rawBytes;
    }

    try {
        return new String(decodedBytes, targetCharset);
    } catch (UnsupportedEncodingException e) {
        Log.e(LOG_TAG, "Failed to encode: charset=" + targetCharset);
        return new String(decodedBytes);
    }
}