最近兩天被IoBuffer搞的頭暈目眩的,故單獨寫了個類測試。若是要學習mina框架,該博客有助於實現mina中的如何消息+附件的傳送。java
描述:模擬消息解析。apache
第一步:構建SmsObject對象框架
第二步:將對象設置到IoBuffer中;ide
第三步:從IoBuffer中將對象解析出來(解析結果包含附件);
經測試解析經過,注意代碼以下學習
package com.dmis.mina.dx3;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.mina.core.buffer.IoBuffer;測試
/**
* IoBuffer測試
*
* @author jrunner
*/
public class Test03 {
public static void main(String[] args) throws Exception {
System.out.println("第一步:構建SmsObject對象");
List fileInfos = new ArrayList();
FileInfo f = new FileInfo();
File file = new File("c:\\test.txt");
f.setContent(file);
f.setFileName(file.getName());
f.setFileNameLength(f.getFileName().length());spa
f.setContentLength((int) file.length());
fileInfos.add(f);
f = new FileInfo();
file = new File("c:\\FeiQ.exe");
f.setContent(file);
f.setFileName(file.getName());
f.setFileNameLength(f.getFileName().length());code
f.setContentLength((int) file.length());
fileInfos.add(f);
SmsObject sms = new SmsObject();
sms.setSender("158010122x");
sms.setReceiver("188696999x");
sms.setMessage("你好啊!");
sms.setFileInfos(fileInfos);
System.out.println("=========================================================");
System.out.println("第二步:將對象設置到IoBuffer中");
Charset cs = Charset.forName("UTF-8");
CharsetDecoder cd = cs.newDecoder();
CharsetEncoder ce = cs.newEncoder();
IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true);
buffer.putString("http://jrunner.blog.51cto.com", ce);// 狀態
buffer.putString(sms.getSender(), ce);// 發送者
buffer.putString(sms.getReceiver(), ce);// 接受者
buffer.putString(sms.getMessage(), ce);// 內容
buffer.putLong(89898989989898L);
for (int i = 0; i < sms.getFileInfos().size(); i++) {
FileInfo info = sms.getFileInfos().get(i);
buffer.putInt(info.getFileNameLength());// 設置文件名長度
buffer.putInt(info.getContentLength());// 設置文件大小
buffer.putString(info.getFileName(), ce);// 設置文件名
buffer.put(inputStreamToByte(new FileInputStream(info.getContent())));// 設置文件內容
}
System.out.println("第三步:從IoBuffer中將對象解析出來");
buffer.flip();
System.out.println(buffer.limit());
System.out.println(buffer.getString("http://jrunner.blog.51cto.com".getBytes().length+sms.getSender().getBytes().length+sms.getReceiver().getBytes().length + sms.getMessage().length() * 3, cd));
System.out.println(buffer.getLong());
System.out.println("------------------");
for (int i = 0; i < sms.getFileInfos().size(); i++) {
System.out.println("解析文件中...");
int L1 = buffer.getInt();// 文件名稱長度
System.out.println("File Name Length:" + L1);
int L2 = buffer.getInt();// 文件內容長度
System.out.println("File Content Length:" + L2);
String fileName = buffer.getString(L1, cd);
System.out.println("FileName:" + fileName);
byte[] data = new byte[L2];
int k = 0;
while (buffer.hasRemaining()) {
data[k] = buffer.get();
k++;
if (k == L2) {
k = 0;
break;
}
}
File fs = new File("d:\\002_" + fileName);
OutputStream out = new FileOutputStream(fs);
out.write(data);
out.flush();
out.close();
}
}對象
public static byte[] inputStreamToByte(InputStream inStream) throws IOException {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
int rc;
while ((rc = inStream.read()) != -1) {
swapStream.write(rc);
}
byte[] in2b = swapStream.toByteArray();
return in2b;
}
}
======================================================
public class FileInfo {
private File content;
private String fileName;
private int contentLength;
private int fileNameLength;
}blog
public class SmsObject { private String sender;// 短信發送者 private String receiver;// 短信接受者 private String message;// 短信內容 private List<FileInfo> fileInfos; }