日期:2014-05-20 浏览次数:20819 次
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
public abstract class SendTools {
public static class FileMessage{
private String fileName;
private String fileData;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileData() {
return fileData;
}
public void setFileData(String fileData) {
this.fileData = fileData;
}
}
public static class FileMessageEncoder{
private final Charset charset = Charset.forName("UTF-8");
public byte[] encode(FileMessage msg){
byte[] fileName = msg.getFileName().getBytes(charset);
byte[] fileData = msg.getFileData().getBytes(charset);
byte[] message = new byte[25+fileName.length+fileData.length];
long len = message.length-8;
message[0]=(byte)((len&0xFF00000000000000L)>>56);
message[1]=(byte)((len&0x00FF000000000000L)>>48);
message[2]=(byte)((len&0x0000FF0000000000L)>>40);
message[3]=(byte)((len&0x000000FF00000000L)>>32);
message[4]=(byte)((len&0x00000000FF000000L)>>24);
message[5]=(byte)((len&0x0000000000FF0000L)>>16);
message[6]=(byte)((len&0x000000000000FF00L)>>8 );
message[7]=(byte)((len&0x00000000000000FFL)>>0 );
message[8]=(byte)(fileName.length&0x00FF);
System.arraycopy(fileName, 0, message, 9, fileName.length);