⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quotedprintable.java

📁 利用SyncML开发客户端程序的中间件
💻 JAVA
字号:
/*
 * Copyright (C) 2006 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.funambol.util;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;


/**
 * A class containing static methods to perform decoding from <b>quoted
 * printable</b> content transfer encoding and to encode into
 */
public class QuotedPrintable {

    private static byte HT = 0x09;      // \t
    private static byte LF = 0x0A;      // \n
    private static byte CR = 0x0D;      // \r

    /**
     * A method to decode quoted printable encoded data.
     * It overrides the same input byte array to save memoty. Can be done
     * because the result is surely smaller than the input.
     * 
     * @param qp
     *         a byte array to decode.
     * @return the length of the decoded array.
     */
    public static int decode(byte [] qp) {
        int qplen = qp.length;
        int retlen = 0;

        for (int i=0; i < qplen; i++) {
            // Handle encoded chars
            if (qp[i] == '=') {
                if (qplen - i > 2) {
                    // The sequence can be complete, check it
                    if (qp[i+1] == CR && qp[i+2] == LF) {
                        // soft line break, ignore it
                        i += 2;
                        continue;

                    } else if (isHexDigit(qp[i+1]) && isHexDigit(qp[i+2]) ) {
                        // convert the number into an integer, taking
                        // the ascii digits stored in the array.
                        qp[retlen++]=(byte)(getHexValue(qp[i+1])*16
                                       + getHexValue(qp[i+2]));
                        
                        i += 2;
                        continue;

                    } else {
                        Log.error("decode: Invalid sequence = " + qp[i+1] + qp[i+2]);
                    }
                }
                // In all wrong cases leave the original bytes
                // (see RFC 2045). They can be incomplete sequence,
                // or a '=' followed by non hex digit.
            }

            // RFC 2045 says to exclude control characters mistakenly
            // present (unencoded) in the encoded stream.
            // As an exception, we keep unencoded tabs (0x09)
            if( (qp[i] >= 0x20 && qp[i] <= 0x7f) ||
                 qp[i] == HT || qp[i] == CR || qp[i] == LF) {
                qp[retlen++] = qp[i];
            }
        }

        return retlen;
    }

    private static boolean isHexDigit(byte b) {
        return ( (b>=0x30 && b<=0x39) || (b>=0x41&&b<=0x46) );
    }

    private static byte getHexValue(byte b) {
        return (byte)Character.digit((char)b, 16);
    }

    public static String decode(byte[] qp, String enc) {
        int len=decode(qp);
        try {
            return new String(qp, 0, len, enc);
        } catch (UnsupportedEncodingException e) {
            Log.error("qp.decode: "+ enc + " not supported. " + e.toString());
            return new String(qp, 0, len);
        }
    }

    /**
     * A method to encode data in quoted printable
     * 
     * @param content
     *            The string to be encoded
     * @return the encoded string.
     * @throws Exception
     *
    public static byte[] encode(String content, String enc) throws Exception {
        // TODO: to be implemented (has to return a String)
        throw new Exception("This method is not implemented!");
    }
     */
    /**
     * A method to encode data in quoted printable
     * 
     * @param content
     *            The string to be encoded
     * @return the encoded string.
     * @throws Exception
     *
    public static byte[] encode(byte[] content) throws Exception {
        // TODO: to be implemented (has to return a String)
        throw new Exception("This method is not implemented!");
    }
    */

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -