📄 messageconnection.java
字号:
/*
* JVending - J2ME MMS Client
*
* Distributed under Apache style software license included with the source code.
*/
package org.jvending.messaging;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.BinaryMessage;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.microedition.io.Connection;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
//this class only supports multipart messages
import org.jvending.messaging.mms.MultimediaEncoder;
import org.jvending.messaging.MultimediaMessage;
import org.jvending.messaging.mms.MultimediaParser;
import org.jvending.messaging.mime.MimeParser;
import org.jvending.messaging.mms.HeaderFieldParser;
import org.jvending.messaging.io.PeekInputStream;
import org.jvending.messaging.MultimediaMessage;
import org.jvending.messaging.mms.ContentType;
import org.jvending.messaging.mime.MultipartEntry;
import org.jvending.messaging.mime.MimeMessage;
/**
* @author Shane Isbell
* @version 1.0.0a
* @created 04/03/27
*/
public class MessageConnection implements Connection {
public static final String BINARY_MESSAGE = "binary";
public static final String TEXT_MESSAGE = "text";
public static final String MULTIPART_MIXED_MESSAGE = "vnd.wap.multipart.mixed";
private String urlConnection;
private ServerSocketConnection serverConnection;
private MessageConnection() { }
public static MessageConnection getInstance() {
MessageConnection mc = new MessageConnection();
return mc;
}
public void setUrlConnection(String urlConnection) {
if(urlConnection != null) this.urlConnection = urlConnection;
}
public Message newMessage(String type) throws IllegalArgumentException {
if(type.equals(MULTIPART_MIXED_MESSAGE)) return new MultimediaMessage();
else throw new IllegalArgumentException();
}
public Message newMessage(String type, String address) throws IllegalArgumentException {
if(type.equals(MULTIPART_MIXED_MESSAGE)) {
MultimediaMessage mm = new MultimediaMessage();
mm.setAddress(address);
return mm;
} else throw new IllegalArgumentException("Unsupported Content Type");
}
public int numberOfSegments(Message message) {
return 1;//don't care
}
public Message receive() throws IOException, InterruptedIOException, SecurityException {
InputStream is = null;
SocketConnection socketConnection = null;
javax.wireless.messaging.MessageConnection conn = null;
ByteArrayInputStream bais = null;
MultimediaMessage mm =null;
HttpConnection httpConnection = null;
if(urlConnection.startsWith("http:")) {
httpConnection = (HttpConnection) Connector.open(urlConnection);
is = httpConnection.openInputStream();
} else if(urlConnection.startsWith("sms:")) {
BinaryMessage bm = null;
conn = (javax.wireless.messaging.MessageConnection) Connector.open("sms://:1234");
bm = (BinaryMessage) conn.receive();
byte[] data = bm.getPayloadData();
is = (InputStream) new ByteArrayInputStream(data);
} else {
throw new IOException("Unrecognized Message Type");
}
PeekInputStream pis = new PeekInputStream(is);
mm = new MultimediaMessage();
MultimediaParser mp = new MultimediaParser();
mp.parse(pis, mm);
is.close();
if(bais != null) bais.close();
if(httpConnection != null) httpConnection.close();
if(socketConnection != null) socketConnection.close();
if(serverConnection != null) serverConnection.close();
if(conn != null) conn.close();
return mm;
}
public void send(Message message) throws IOException, InterruptedException,
SecurityException, IllegalArgumentException, NullPointerException {
//not implemented
}
public void setMessageListener(MessageListener messageListener) throws SecurityException, IOException {
}
public void close() throws IOException {
serverConnection.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -