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

📄 mimeheaders.java

📁 封装了SQL、Socket、WAP、MIME等功能的通用组件
💻 JAVA
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi
// Source File Name:   InternetHeaders.java

package org.lazybug.mime;

import javax.mail.MessagingException;
import com.sun.mail.util.LineInputStream;
import com.sun.mail.util.LineOutputStream;
import java.io.IOException;
import java.io.*;
import java.util.*;
import org.lazybug.util.Base64;
import org.lazybug.util.Tools;
import org.lazybug.util.BytesUtile;

// Referenced classes of package javax.mail.internet:
//            hdr, matchEnum

public class MimeHeaders
{
    /**/
    private Vector hList;
    /**/
    private Hashtable hMap;
    /*内容类型*/
    protected ContentType contentType;
    /*Content-Disposition*/
    protected ContentDisposition contentDisposition;

    public MimeHeaders()
    {
        hMap = new Hashtable();
        hList = new Vector();
    }
    /**
     * 头信息解码
     * @param in InputStream
     * @throws MessagingException
     */
    protected void decode(InputStream in) throws IOException
    {
//System.out.println("in.available()="+in.available());
        boolean bWaitEol = false;
        boolean bDblEol = false;
        int ch;
        while( in.available() > 0 )
        {
            ch = in.read();
            BytesUtile.write(ch);
            if( bWaitEol && ch == 0x0a)
            {
                if( bDblEol ) break;
                bDblEol = true;
                continue;
            }
            else if( ch == 0x0d )
            {
                bWaitEol = true;
                continue;
            }

            bDblEol = false;
            bWaitEol = false;
        }

        byte buf[] = null;
        buf = BytesUtile.getPayload();
            //com.kehaoinfo.util.Tools.printb(this.payload);
        java.io.BufferedReader reader =
            new BufferedReader(new InputStreamReader(
            new ByteArrayInputStream(buf, 0, buf.length), "UTF-8"));

    //StringBuffer sb = new StringBuffer();
        //System.out.println("\n----------------------------------------\n");
        do
        {
            String line = reader.readLine();
            //com.kehaoinfo.util.Tools.printb(line.getBytes());
           // System.out.println("line("+line.length()+"): "+line);
            if( line == null || line.length() <= 0 )
            {
                break;
            }
            //if( line.indexOf("Subject:") != -1 )
           // {
             //   System.out.println("Check subject!"+line);
             //   com.kehaoinfo.util.Tools.printb(line.getBytes());
            //}
            addHeaderLine(line);

            /*if( sb.length() < 100 ){
                sb.append(line);
                sb.append("\r\n");
            }*/
        }
        while(true);
//System.out.println("in.available()="+in.available());
        reader.close();

        Object keyCt = "";
        Object keyCd = "";
        for(int i = 0; i < hList.size(); i++ )
        {
            String key =  hList.get(i).toString();
            if( key.equalsIgnoreCase("Content-Type") )
            {
                keyCt = key;
                this.setContentType(hMap.get(keyCt).toString());
            }
            else if( key.equalsIgnoreCase("Content-Desposition") )
            {
                keyCd = key;
                this.setContentDisposition(hMap.get(keyCd).toString());
            }
            //else if( key.equalsIgnoreCase("Subject") )
            //{
                //String s = hMap.get(key).toString();
                //s = new String(s.getBytes(), "UTF-8");
                //hMap.put(key, s);
                //System.out.println("SUBJECT::"+s);
            //}
        }

        if( keyCt.equals("") )
        {
            //System.out.println("++++++++++++++\n"+new String(buf, "UTF-8"));
            //System.out.println("\nException:"+sb.toString());
            throw new MimeException("Content type unknown.");
        }
        hList.remove(keyCt);
        hMap.remove(keyCt);
        hList.remove(keyCd);
        hMap.remove(keyCd);
    }


    public String getHeader(String field)
    {
        if( field.equalsIgnoreCase("Content-Type") )
        {
            if( this.contentType != null )
                return this.contentType.toString();
        }
        else if( field.equalsIgnoreCase("Content-Description") )
        {
            if( this.contentDisposition != null )
                return this.contentDisposition.toString();
        }

        if( hMap.containsKey(field) )
        {
            /*if(field.equalsIgnoreCase("Subject"))
            {
                String s  = hMap.get(field).toString();
                //System.out.println( "--------------getSubject-----------" );
                //System.out.println( s );
                if( s != null )
                {
                    try
                    {
                        s = new String( s.getBytes(), "UTF-8" );
                    }
                    catch( Exception e )
                    {
                        e.printStackTrace();
                    }
                }
                //System.out.println( s );
                return s;
            }*/
            return( String ) hMap.get( field );
        }
        else
        {
            for( int i = 0; i < hList.size(); i++ )
            {
                if(field.equalsIgnoreCase(hList.get(i).toString()))
                {
                    String s  = hMap.get(hList.get(i)).toString();
                    /*if( s != null && field.equalsIgnoreCase("Subject") )
                    {
                    System.out.println( "--------------getSubject-----------" );
                    System.out.println( s );

                        try
                        {
                            s = new String( s.getBytes(), "UTF-8" );
                        }
                        catch( Exception e )
                        {
                            e.printStackTrace();
                        }
                    System.out.println( s );
                    }*/
                    return s;
                }
            }
        }
        //System.out.println("getHeader("+field+"):"+hMap);
        return "";
    }

    public void setHeader(String field, String value)
    {
        if( field.equalsIgnoreCase("Content-Type") )
        {
            try{
                this.setContentType(value);
                return;
            }
            catch(MimeException e){}
        }
        else if( field.equalsIgnoreCase("Content-Description") )
        {
            try{
                this.setContentDisposition(value);
                return;
            }
            catch(MimeException e){}
        }

        for( int i = 0; i < hList.size(); i++ )
        {
            if(field.equalsIgnoreCase(hList.get(i).toString()))
            {
                hMap.put(hList.get(i), value);
                return;
            }
        }

        hMap.put(field, value);
        hList.add(field);
    }

    public void removeHeader(String field)
    {
        if( field.equalsIgnoreCase("Content-Type") )
        {
            this.contentType = null;
            return;
        }
        else if( field.equalsIgnoreCase("Content-Description") )
        {
            this.contentDisposition = null;
            return;
        }

        for( int i = 0; i < hList.size(); i++ )
        {
            if(field.equalsIgnoreCase(hList.get(i).toString()))
            {
                hMap.remove(field);
                hList.remove(field);
                return;
            }
        }

        hMap.remove(field);
        hList.remove(field);
    }

    public ContentType getContentType()
    {
        return contentType;
    }

    public ContentDisposition getContentDisposition()
    {
        return contentDisposition;
    }

    /**
     * 设置内容的类型
     * @param cd String
     * @throws MessagingException
     */
    public void setContentType(String ct) throws MimeException
    {
        if( ct == null || ct.length() == 0 )
        {
            throw new MimeException("Content type cannot null.");
        }
        contentType = new ContentType(ct);
    }
    /**
     * 设置内容的部署
     * @param cd String
     * @throws MessagingException
     */
    public void setContentDisposition(String cd) throws MimeException
    {
        if( cd == null || cd.length() == 0 || (
            cd.indexOf(ContentDisposition.CONTENT_DISPOSITION_ATTACHMENT) == -1 &&
            cd.indexOf(ContentDisposition.CONTENT_DISPOSITION_INLINE) == -1 ) )
        {
            throw new MimeException("Content Disposition invalid.");
        }
        contentDisposition = new ContentDisposition(cd);
    }

    protected void encodeHeaders(OutputStream out) throws IOException
    {
        if( this.contentType == null )
            throw new IOException("Failed to encode for content type unknown.");
        //StringBuffer sb = new StringBuffer();
        /*LineOutputStream lineoutputstream = null;
        if( out instanceof LineOutputStream )
            lineoutputstream = (LineOutputStream)out;
        else lineoutputstream = new LineOutputStream(out);*/
        //System.out.println("----------------------------------------------");
        //lineoutputstream.writeln("Content-Type: "+contentType.toString());
        out.write("Content-Type: ".getBytes());
        //sb.append("Content-Type: ");
        out.write(contentType.toString().getBytes());
        //sb.append(contentType.toString());
        //sb.append("\r\n");
        out.write(0x0d);
        out.write(0x0a);

//        writer.write("Content-Type: ");
//        writer.write(contentType.toString());
//        writer.write("\r\n");

        for( int i = 0; i < hList.size(); i++ )
        {
            String field = hList.get(i).toString();
            String value = hMap.get(field).toString();
            //lineoutputstream.writeln(field+": "+value);
            System.out.println(field+":"+value);
            out.write(field.getBytes());
            out.write(':');

            if( field.equalsIgnoreCase("Subject") )
            {
                out.write(value.getBytes("UTF-8"));
            }
            else
            {
                out.write(value.getBytes());
                /*sb.append( field );
                sb.append( ':' );
                sb.append( value );
                sb.append( "\r\n" );*/
            }
            out.write( 0x0d );
            out.write( 0x0a );

        }

        if( this.contentDisposition != null )
        {
            out.write(contentDisposition.toString().getBytes());
            out.write( 0x0d );
            out.write( 0x0a );

        }
            //lineoutputstream.writeln(this.contentDisposition.toString());
        //lineoutputstream.writeln();
    }

    private void addHeaderLine(String line)
    {
        Object field = null;
        char c = line.charAt(0);
        if( c == ' ' || c == '\t' )
        {
            field = hList.lastElement();
            String value = (String)hMap.get(field);
            hMap.put(field, value+"\r\n"+line);
        }
        else
        {
            int i = line.indexOf(58);
            if(i < 0)
            {
                field = line.trim();
                hList.add(field);
                hMap.put(field, "");
            }
            else
            {
                field = line.substring(0, i).trim();
                int j;
                for(j = i + 1; j < line.length(); j++)
                {
                    c = line.charAt(j);
                    if(c != ' ' && c != '\t' && c != '\r' && c != '\n')
                        break;
                }
//                System.out.println(field+","+line+"("+j+")");
                hMap.put(field, line.substring(j));
            }
            hList.add(field);
        }
    }
    /**
     * 得到头信息
     * @return String
     */
    public String getHeaders()
    {
        if( this.contentType == null ) return "";

        StringBuffer sb = new StringBuffer();
        sb.append("Content-Type:");
        sb.append(this.contentType.toString());
        sb.append("\r\n");

        for( int i = 0; i < hList.size(); i++ )
        {
            String field = hList.get(i).toString();
            sb.append(field);
            sb.append(":");
            sb.append(getHeader(field));
            sb.append("\r\n");
        }

        if( this.contentDisposition != null )
            sb.append(this.contentDisposition.toString());
        return sb.toString();
    }

    public String toString()
    {
        if( this.contentType == null ) return "";

        StringBuffer sb = new StringBuffer();
        sb.append("Content-Type:");
        sb.append(this.contentType.toString());
        sb.append("\r\n");
        for( int i = 0; i < hList.size(); i++ )
        {
            String field = hList.get(i).toString();
            sb.append(field);
            sb.append(":");
            sb.append(getHeader(field));
            sb.append("\r\n");
        }

        if( this.contentDisposition != null )
            sb.append(this.contentDisposition.toString());
        return sb.toString();
    }

    public static void main(String[] args)
    {
        File file = new File("D:\\work\\project\\isms_unicom\\proxy\\multipart_related_1.txt");
        try
        {
            FileInputStream in = new FileInputStream(file);
            MimeHeaders headers = new MimeHeaders();
            headers.decode(in);
            System.out.println(headers.getContentType().getParameter("bounDary"));
            System.out.println(headers.getContentType().getParameter("START"));
            System.out.println(headers.getHeader("FROM"));
            System.out.println(headers.getHeader("MIME-Version"));
            System.out.println(headers.getHeader("to"));
            System.out.println();

            System.out.println(headers.toString());
        }
        catch( FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch( IOException e)
        {
            e.printStackTrace();
        }
        catch( Exception e)
        {
            e.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

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