📄 mimemessage.java
字号:
/*
* @(#)MimeMessage.java
*
* Copyright (c) 2001-2002, JangHo Hwang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the JangHo Hwang nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: MimeMessage.java,v 2.0 2004/12/21 16:15:08 chenzs Exp $
*/
package vitular.msnp.msg;
import java.io.*;
import java.util.*;
import vitular.msnp.util.*;
/**
* MIME 锟睫斤拷锟斤拷锟?; 锟斤拷锟较达拷 努锟斤拷锟斤拷锟教达拷.
*
* @author Jang-Ho Hwang, rath@linuxkorea.co.kr
* @version $Id: MimeMessage.java,v 2.0 2004/12/21 16:15:08 chenzs Exp $
*/
public class MimeMessage {
public static final byte KIND_PROFILE = 0;
public static final byte KIND_TYPING_USER = 1;
public static final byte KIND_MESSAGE = 2;
public static final byte KIND_FILE_TRANSFER = 3;
public static final byte KIND_MAIL_NOTIFY = 4;
public static final byte KIND_UNKNOWN = 10;
public static final String STR_PROFILE = "text/x-msmsgsprofile";
public static final String STR_TYPING_USER = "text/x-msmsgscontrol";
public static final String STR_MESSAGE = "text/plain";
public static final String STR_FILE_TRANSFER = "text/x-msmsgsinvite";
public static final String STR_MAIL_NOTIFY =
"text/x-msmsgsinitialemailnotification";
private byte kind = 0;
private String message = null;
private String ef = "";
private String fn = "宋体";
private int fontColor = 0x0;
private Hashtable prop = new Hashtable();
public MimeMessage() {
}
/**
* 锟街撅拷锟斤拷 锟睫斤拷锟斤拷 锟斤拷锟斤拷锟? MIME 锟睫斤拷锟斤拷 锟轿斤拷锟较斤拷锟斤拷 锟斤拷锟窖达拷.
*/
public MimeMessage(String message) {
setMessage(message);
// this.fontColor = getRandomColor();
}
/**
* 锟斤拷 MimeMessage锟斤拷 type; 锟斤拷券锟斤拷锟截达拷.
* 锟斤拷锟斤拷锟? 锟轿憋拷锟轿斤拷 锟睫达拷 profile d锟斤拷(KIND_PROFILE),
* Typing 锟竭匡拷 锟斤拷鄣谴锟? typing d锟斤拷(KIND_TYPING_USER),
* 锟轿斤拷锟斤拷飘 锟睫斤拷锟斤拷 锟斤拷鄣啥锟? 锟斤拷锟角达拷 d锟斤拷(KIND_MESSAGE) 锟斤拷8锟斤拷
* 锟斤拷锟斤拷锟角撅拷锟街达拷.
* 锟斤拷锟斤拷 锟斤拷 锟斤拷 锟斤拷 锟斤拷锟斤拷 锟睫斤拷锟斤拷 锟斤拷 锟斤拷锟斤拷 KIND_UNKNOWN锟斤拷
* 锟斤拷券锟斤拷 锟斤拷锟教达拷.
*/
public byte getKind() {
return this.kind;
}
public void setKind(byte kind) {
this.kind = kind;
}
public String getFontName() {
return this.fn;
}
public void setFontName(String fn) {
this.fn = fn;
}
public int getFontColor() {
return this.fontColor;
}
public void setFontColor(int c) {
this.fontColor = c;
}
/**
* 锟斤拷锟斤拷习锟斤拷锟? 锟较达拷 锟睫斤拷锟斤拷 锟斤拷d锟窖达拷.
* 锟劫硷拷 锟斤拷锟斤拷锟斤拷锟?, 锟斤拷 锟睫家碉拷锟? KIND_MESSAGE锟斤拷 锟斤拷锟斤拷锟? 锟斤拷炜★拷锟?
* 锟斤拷锟饺达拷.
*/
public void setMessage(String msg) {
this.message = msg;
}
/**
* KIND_MESSAGE ~锟斤拷锟斤拷 锟斤拷 锟斤拷锟斤拷锟斤拷 锟轿斤拷锟斤拷飘 锟睫斤拷锟斤拷 锟斤拷锟斤拷;
* 锟斤拷锟?4碌锟? 锟斤拷锟饺达拷. 锟斤拷锟斤拷 KIND_UNKNOWN锟教匡拷锟劫革拷,
* raw 锟斤拷锟斤拷锟斤拷 锟斤拷眉 锟斤拷锟节匡拷锟斤拷 锟斤拷券锟斤拷 锟斤拷锟教达拷.
*/
public String getMessage() {
return this.message;
}
/**
* 锟街撅拷锟斤拷 line; 锟侥斤拷锟较匡拷 property锟斤拷 锟较筹拷 锟竭帮拷锟窖达拷.
* 锟斤拷锟斤拷 锟侥斤拷锟斤拷 锟斤拷 锟斤拷俑锟? false锟斤拷 锟斤拷锟斤拷锟斤拷 锟斤拷锟教帮拷,
* d锟斤拷锟斤拷锟斤拷 锟斤拷锟?; 锟斤拷锟斤拷锟? 锟街促革拷, property锟斤拷 锟竭帮拷锟斤拷 锟斤拷 锟斤拷
* true锟斤拷 锟斤拷券锟斤拷 锟斤拷锟教达拷.
*/
protected boolean addProperty(String line) {
int i0 = line.indexOf(": ");
if (i0 == -1) {
return false;
}
String key = line.substring(0, i0);
String value = line.substring(i0 + 2);
prop.put(key, value);
return true;
}
/**
* 锟絯锟斤拷锟狡硷拷锟? 锟斤拷d锟窖达拷.
*
* @param key 锟絯锟斤拷锟狡? 虐.
* @param value 锟絯锟斤拷锟狡? 锟斤拷.
*/
public void setProperty(String key, String value) {
this.prop.put(key, value);
}
/**
* 锟斤拷锟斤拷 锟斤拷d锟角撅拷锟街达拷 锟絯锟斤拷锟狡硷拷锟?; 锟斤拷锟? 锟街达拷 Properties 锟斤拷眉锟斤拷 锟斤拷n锟铰达拷.
*/
public Hashtable getProperties() {
return this.prop;
}
/**
* 漂d Mime header 锟斤拷; 锟斤拷n锟铰达拷.
*/
public String getProperty(String key) {
return prop.get(key).toString();
}
/**
* 锟街撅拷锟斤拷 key锟斤拷 锟斤拷锟斤拷 mime properties锟斤拷 x锟斤拷锟较达拷锟斤拷 锟斤拷锟斤拷 锟斤拷锟轿革拷 锟斤拷锟铰达拷.
*/
public boolean hasProperty(String key) {
return this.prop.containsKey(key);
}
/**
* UTF-8锟斤拷 锟斤拷锟斤拷锟斤拷 raw 锟斤拷锟斤拷锟斤拷 MIME 锟睫斤拷锟斤拷 锟睫撅拷 MIME 锟斤拷目锟? 锟斤拷锟斤拷
* 锟侥斤拷/锟叫硷拷锟较匡拷 锟斤拷锟斤拷锟酵革拷 盲锟斤拷锟?.
*
* @param raw String锟斤拷锟斤拷锟斤拷 MIME 锟斤拷眉 锟睫斤拷锟斤拷.
*/
public void parse(String raw) throws Exception {
BufferedReader br = new BufferedReader(new StringReader(raw));
addProperty(br.readLine()); // MIME-Version
addProperty(br.readLine()); // Content-Type
String contentType = prop.get("Content-Type").toString();
if (contentType.equals(STR_TYPING_USER)) {
this.kind = KIND_TYPING_USER;
addProperty(br.readLine());
}
else
if (contentType.startsWith(STR_PROFILE)) {
this.kind = KIND_PROFILE;
String buf = null;
while (true) {
buf = br.readLine();
if (buf == null) {
break;
}
if (!addProperty(buf)) {
break;
}
}
}
else
if (contentType.startsWith(STR_MAIL_NOTIFY)) {
this.kind = KIND_MAIL_NOTIFY;
String buf = null;
while (true) {
buf = br.readLine();
if (buf == null) {
break;
}
addProperty(buf);
}
}
else
if (contentType.startsWith(STR_FILE_TRANSFER)) {
/*
* FileTransferMessage锟斤拷 锟斤拷券锟斤拷 锟斤拷 锟街碉拷锟斤拷 锟窖达拷.
*/
this.kind = KIND_FILE_TRANSFER;
String buf = null;
while (true) {
buf = br.readLine();
if (buf == null) {
break;
}
if (buf.trim().length() == 0) {
continue;
}
if (!addProperty(buf)) {
break;
}
}
}
else
if (contentType.startsWith(STR_MESSAGE)) {
this.kind = KIND_MESSAGE;
addProperty(br.readLine());
br.readLine(); // Temporary skip line.
StringBuffer sb = new StringBuffer();
String buf = null;
while ( (buf = br.readLine()) != null) {
sb.append(buf);
/* 锟侥斤拷锟截硷拷 锟斤拷锟斤拷洗锟? 锟斤拷: \n锟教革拷 锟斤拷锟斤拷细锟斤拷锟? 锟斤拷却锟?.*/
sb.append("\n");
}
String str = sb.toString();
this.message = str.substring(0, str.length() - 1);
}
else {
this.kind = KIND_UNKNOWN;
System.out.println("UNKNOWN-Content: " + contentType);
this.message = raw;
System.out.println("Raw message: ");
System.out.println(raw);
}
br.close();
}
// private int getRandomColor() {
// //閲囩敤j2me闅忔満鏁板彂鐢熸柟娉?
// java.util.Random random = new java.util.Random();
// int r = random.nextInt();
// int g = random.nextInt();
// int b = random.nextInt();
//
// if (r > 0xa0) r = 0xa0;
// if (g > 0xa0) g = 0xa0;
// if (b > 0xa0) b = 0xa0;
// return (r << 16) + (g << 8) + b;
// }
private String getStringAsColor(int c) {
return Integer.toHexString(
( (c & 0xFF) << 16) | ( (c & 0xFF00) << 8) | (c & 0xFF0000));
}
public String getEffectCode() {
return this.ef;
}
public void setEffectCode(String ef) {
this.ef = ef;
}
/**
* Mime format; 锟斤拷锟斤拷锟? UTF-8 base锟斤拷 URL encoded锟斤拷 byte array锟斤拷
* 锟斤拷锟斤拷锟斤拷 锟截达拷.
*/
public byte[] getInstantMessageBytes() throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
sb.append(
"MIME-Version: 1.0" +
"\r\n" +
"Content-Type: text/plain; charset=UTF-8" +
"\r\n" +
"X-MMS-IM-Format: " +
"FN=");
sb.append(MimeUtility.getURLEncodedString(this.fn, "UTF-8"));
sb.append("; EF=");
sb.append(ef);
sb.append("; CO=");
sb.append(getStringAsColor(this.fontColor));
sb.append(
"; " +
"CS=0" +
"; " +
"PF=22" +
"\r\n\r\n");
sb.append(this.message);
return sb.toString().getBytes("UTF-8");
}
public byte[] getTypingMessageBytes() throws UnsupportedEncodingException {
StringBuffer sb = createMimeHeader(STR_TYPING_USER);
sb.append("TypingUser: ");
sb.append(message);
sb.append("\r\n\r\n");
return sb.toString().getBytes("UTF-8");
}
/**
* 漂d Content-Type; 锟斤拷锟斤拷锟? Mime header锟斤拷 mime message锟斤拷 锟斤拷锟斤拷锟截达拷.
*/
protected StringBuffer createMimeHeader(String header) {
StringBuffer sb = new StringBuffer();
sb.append("MIME-Version: 1.0\r\n" +
"Content-Type: ");
sb.append(header);
sb.append("\r\n");
return sb;
}
/**
* 锟斤拷锟斤拷 锟斤拷d锟斤拷 锟斤拷锟? Properties锟斤拷 buf锟斤拷 锟竭帮拷锟较帮拷, 锟斤拷锟斤拷锟? \r\n锟斤拷 锟窖癸拷 锟斤拷 锟劫匡拷锟街帮拷 锟饺达拷.
*/
protected StringBuffer fillMimeProperties(StringBuffer buf) {
for (Enumeration e = prop.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
String value = prop.get(key).toString();
buf.append(key);
buf.append(": ");
buf.append(value);
buf.append("\r\n");
}
buf.append("\r\n");
return buf;
}
/**
* 锟斤拷d锟斤拷 ~锟斤拷(Kind)锟斤拷 锟斤拷锟? 锟斤拷锟斤拷锟较帮拷 byte锟斤拷券锟斤拷锟截达拷.
*/
public byte[] getBytes() throws UnsupportedEncodingException {
switch (this.kind) {
case KIND_MESSAGE:
return getInstantMessageBytes();
case KIND_TYPING_USER:
return getTypingMessageBytes();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -