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

📄 mimedecoder.java

📁 使用java编写的手机邮件系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            else {                /**                 * Emergency stop.                 */                s = "";            }        }        /**         * Finally, store the updated values of the begin and end counters. At this         * point they denote the beginning and end of the part's body, respectively.         */        this.begin = begin;        this.end = end;    }    /**     * Gets a line from the message that this MimeDecoder is working on. This     * is an internal method that was introduced to reduce the number of type     * casts necessary when accessing the underlying message.     */    private String getLine(int index) {        return (String)lines.elementAt(index);    }    /**     * Returns the number of lines in this MIME part's body.     *     * @see #getBodyLine(int)     * @see #getBodyBytes()     */    public int getBodyLineCount() {        return end - begin;    }    /**     * Returns a line of this MIME part's body its index.     *     * @see #getBodyLineCount()     * @see #getBodyBytes()     */    public String getBodyLine(int index) throws ArrayIndexOutOfBoundsException {        if ((index < 0) || (index >= end - begin)) {            throw new ArrayIndexOutOfBoundsException(index);        }        return (String)lines.elementAt(begin + index);    }    /**     * Returns the binary data contained in this MIME part. This method should     * only be invoked on MIME parts whose encoding is "base64", otherwise the     * result will be more or less arbitrary.     */    public byte[] getBodyBytes() {        /**         * The implementation is rather straightforward: Walk through the body         * lines one by one, have them decoded and append the result the an         * ByteArrayOutputStream that creates the resulting array.         */        ByteArrayOutputStream bos = new ByteArrayOutputStream();        for (int i = 0; i < getBodyLineCount(); i++) {            decode(getBodyLine(i), bos);        }        return bos.toByteArray();    }    /**     * Returns the number of MIME parts that constitute this MIME part. If the     * result is zero, this MIME part is a leaf in the tree-like hierarchy of     * MIME parts, and its contents can be retrieved by either the getBodyLine()     * or the getBodyBytes() methods. Otherwise the part is an inner node in the     * part hierarchy, and contains one or more subordinate MIME parts which can     * be accessed using the getPart() method.     */    public int getPartCount() {        if (parts == null) {            return 0;        }        else {            return parts.size() - 1;        }    }    /**     * Returns a subordinate part of this MIME part. This method should only be     * called if the getPartCount() method returns a value greater than zero,     * meaning that this MIME part is an inner node in the hierarchy of parts     * and has one ore more subordinate MIME parts. Note that this method acts     * as some kind of factory method for MimeDecoders that do not work on the     * top-level of a message, so the MimeDecoder objects are created on-demand     * and do not pollute the limited J2ME heap if they're not really needed.     */    public MimeDecoder getPart(int index) {        if ((index < 0) || (index >= getPartCount())) {            throw new ArrayIndexOutOfBoundsException(index);        }        int i = ((Integer)parts.elementAt(index)).intValue();        int j = ((Integer)parts.elementAt(index + 1)).intValue();        return new MimeDecoder(this, i + 1, j);    }    /**     * Returns the type of this MIME part, which is deduced from the     * "Content-Type:" header field. Frequent header types that do work     * well in a J2ME environment are "text/plain", which means plain ASCII     * text, and "image/png" which means that the part holds a PNG image,     * probably encoded in base64 (the latter information can be retrieved     * by the getEncoding() method).     *     * @see #getEncoding()     * @see #getName()     */    public String getType() {        return type;    }    /**     * Returns the filename of this MIME part, which is deduced from the "name="     * sub-field of the "Content-Type:" header field. This value will probably     * only hold a non-null value for binary attachments, for example images.     *     * @see #getType()     * @see #getEncoding()     */    public String getName() {        return name;    }    /**     * Returns the encoding of this MIME part, which is deduced from the     * "Content-Transfer-Encoding:" header field. The encoding tells us how data     * is to be retrieved from the MIME part. If the value is null, the part's     * contents can be retrieved line by line using the getBodyLine() method.     * If the encoding is "base64", the part's data should be retrieved using the     * getBodyBytes() method. Other encodings are currently not supported by the     * MimeDecoder itself.     */    public String getEncoding() {        return encoding;    }    /**     * Returns the name contained in a name/value pair string. The name is     * everything up to, but not including, the first "=" sign in the string. If     * no "=" can be found, null is returned, assuming the string doesn't hold a     * name at all.     * <p>     * Note that this method is different from the getStringName() method     * of the Message class due to the different separator character.     *     * @see #getStringValue(java.lang.String)     * @see Message#getStringName(java.lang.String)     */    public static String getStringName(String s) {        int p = s.indexOf('=');        if (p == -1) {            return null;        }        else {            return s.substring(0, p);        }    }    /**     * Returns the value contained in a name/value pair string. The value is     * everything following, but not including, the first "=" sign in the string.     * If no "=" can be found, the whole string is returned, assuming it holds     * only a value, but not name at all. The method unquotes values enclosed in     * double quotes automatically.     * <p>     * Note that this method is different from the getStringValue() method     * of the Message class due to the different separator character.     *     * @see #getStringName(java.lang.String)     * @see Message#getStringValue(java.lang.String)     */    public static String getStringValue(String s) {        String value;        int p = s.indexOf('=');        if (p == -1) {            value = s;        }        else {            value = s.substring(p + 1);        }        /**         * Get rid of leading and trailing spaces.         */        value = value.trim();        /**         * Unquote result, if necessary.         */        if ((value.length() > 1) && (value.charAt(0) == '"') && ((value.charAt(value.length() - 1) == '"'))) {            value = value.substring(1, value.length() - 1);        }        return value;    }    /**     * Does a base64 decoding for a single character.     * <p>     * Contains code from Stefan Haustein's KObjects library (www.kobjects.org)     * used by permission.     */    private static int decode(char c) {        if (c >= 'A' && c <= 'Z')            return ((int) c) - 65;        else if (c >= 'a' && c <= 'z')            return ((int) c) - 97 + 26;        else if (c >= '0' && c <= '9')            return ((int) c) - 48 + 26 + 26;        else switch (c) {            case '+': return 62;            case '/': return 63;            case '=': return 0;            default:  throw new RuntimeException("Illegal MIME character '" + c + "'");        }    }    /**     * Does a base64 decoding for a whole String and writes the resulting bytes     * into a stream.     * <p>     * Contains code from Stefan Haustein's KObjects library (www.kobjects.org)     * used by permission.     */    private static void decode(String s, ByteArrayOutputStream bos) {        int i = 0;        int len = s.length();        while (true) {            while (i < len && s.charAt(i) <= ' ') i++;            if (i == len) break;            int tri = (decode(s.charAt(i)) << 18)                + (decode(s.charAt(i+1)) << 12)                + (decode(s.charAt(i+2)) << 6)                + (decode(s.charAt(i+3)));            bos.write((tri >> 16) & 255);            if (s.charAt(i+2) == '=') break;            bos.write((tri >> 8) & 255);            if (s.charAt(i+3) == '=') break;            bos.write(tri & 255);            i += 4;        }    }}

⌨️ 快捷键说明

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