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

📄 mimeparser.java

📁 Messaging- MIDP的是一个彩信客户端
💻 JAVA
字号:
/*
 *  JVending - J2ME MMS Client
 *  Copyright (C) 2004  Shane Isbell
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.jvending.messaging.impl.mms;

import org.jvending.messaging.impl.io.PeekInputStream;

import org.jvending.messaging.MultimediaMessage;
import org.jvending.messaging.MultipartEntry;
import org.jvending.messaging.impl.util.MmsDecodingUtility;
import org.jvending.messaging.MimeMessage;

import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;

/**
 * @author Shane Isbell
 * @version 1.0.0a
 * @created 04/03/27
 */ 

class MimeParser {

    //Multimediamessage
    public void parse(PeekInputStream pis, Object targetObject) {
        MultimediaMessage mmm = (MultimediaMessage) targetObject;
        MimeMessage mm = (MimeMessage) new MimeMessageImpl();
        MultipartEntryAssembler mea = new MultipartEntryAssembler(pis);

        try {
            long x = MmsDecodingUtility.readUintvarInteger(pis).longValue();
            mm.setNumberOfEntries(x);
            MultipartEntry me = null;
            for (int i = 0; i < x; i++) {
                me = mea.nextEntry();
                mm.setEntry(me);             
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
        mmm.setMimeMessage(mm);
        
    }


    private class MultipartEntryAssembler {

        private PeekInputStream dis;

        MultipartEntryAssembler(InputStream is) {
            this.setInputStream(is);
        }

        MultipartEntryAssembler(PeekInputStream is) {
            this.dis = is;
        }


        public boolean hasMoreElements() {
            return true;
        }

        private byte[] readHeaders(PeekInputStream dis, int length) throws IOException {
            byte[] b = new byte[length];
            byte k = 0;int i = 0;
            while(i < length) {
                k =  (byte) dis.readByte();
                b[i++] = k;
            }
            return b;
        }

        private byte[] readContent(PeekInputStream dis, int length) throws IOException {
            byte[] data = new byte[length];

            byte k = 0; int i = 0;
         //   dis.read(data, 0, length);

            while(i < length) {//FIX ME:
                k = (byte) dis.readByte();
                data[i] = k;
                i++;
            }

            return data;
        }

        public MultipartEntry nextEntry() throws NoSuchElementException {
            MultipartEntry me = new MultipartEntry();
            try {
                  Long headerLength = MmsDecodingUtility.readUintvarInteger(dis);
                  me.setHeaderLength((int) headerLength.longValue());

                  Long dataLength = MmsDecodingUtility.readUintvarInteger(dis);
                  me.setDataLength((int) dataLength.longValue());

                  int len = ((int) headerLength.longValue()) - 1;

                  int contentType = (dis.readUnsignedByte() & 0x7f);
                  me.setContentType(contentType);

                  byte[] headers = readHeaders(dis, len);
                  me.setHeaders(headers);
                  me.setData(readContent(dis, (int) dataLength.longValue()));
            } catch(IOException e) {
                e.printStackTrace();
            }

            return me;

        }

        public void setInputStream(InputStream is) {
            dis = new PeekInputStream(is);
        }
    }        
}

⌨️ 快捷键说明

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