-----------------------------------------------------------------------------------
-----------------------七、消息:-------------------------------------------
-----------------------------------------------------------------------------------
public class Message {
private String flag;
private int totleCount;
private int curIndex;
private byte [] content;
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public int getTotleCount() {
return totleCount;
}
public void setTotleCount(int totleCount) {
this.totleCount = totleCount;
}
public int getCurIndex() {
return curIndex;
}
public void setCurIndex(int curIndex) {
this.curIndex = curIndex;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public Message()
{
}
public Message(String flag, int totleCount, int curIndex, byte[] content) {
this.flag = flag;
this.totleCount = totleCount;
this.curIndex = curIndex;
this.content = content;
}
}
-----------------------------------------------------------------------------------
----------------------八、工具类:-------------------------------------------
-----------------------------------------------------------------------------------
/**
* 工具类
*/
public class Util {
/**
* 将int转换成字节数组
*/
public static byte[] int2Bytes(int i)
{
byte[] bs = new byte[4];
bs[0] = (byte)(i >>> 24);
bs[1] = (byte)(i >>> 16);
bs[2] = (byte)(i >>> 8);
bs[3] = (byte)(i >>> 0);
return bs;
}
/**
* 将字节数组前4位,转换成int
*/
public static int bytes2Int(byte [] bs)
{
int i0 = (bs[0] & 0xff) <<24;
int i1 = (bs[1] & 0xff) <<16;
int i2 = (bs[2] & 0xff) <<8;
int i3 = (bs[3] & 0xff) <<0;
return i0 | i1 | i2 | i3;
}
/**
* 将int转换成字节数组
*/
public static byte[] long2Bytes(long l)
{
byte[] bs = new byte[8];
bs[0] = (byte)(l >>> 56);
bs[1] = (byte)(l >>> 48);
bs[2] = (byte)(l >>> 40);
bs[3] = (byte)(l >>> 32);
bs[4] = (byte)(l >>> 24);
bs[5] = (byte)(l >>> 16);
bs[6] = (byte)(l >>> 8);
bs[7] = (byte)(l >>> 0);
return bs;
}
/**
* 将字节数组前4位,转换成int
*/
public static long bytes2Long(byte [] bs)
{
long i0 = (bs[0] & 0xffL) <<56;
long i1 = (bs[1] & 0xffL) <<48;
long i2 = (bs[2] & 0xffL) <<40;
long i3 = (bs[3] & 0xffL) <<32;
long i4 = (bs[4] & 0xffL) <<24;
long i5 = (bs[5] & 0xffL) <<16;
long i6 = (bs[6] & 0xffL) <<8;
long i7 = (bs[7] & 0xffL) <<0;
return i0 | i1 | i2 | i3 | i4 | i5 | i6 | i7;
}
/**
* 将字节数组off位 + 4位, 转换成int
*/
public static int bytes2Int(byte [] bs , int offset)
{
int i0 = (bs[0 + offset] & 0xff) <<24;
int i1 = (bs[1 + offset] & 0xff) <<16;
int i2 = (bs[2 + offset] & 0xff) <<8;
int i3 = (bs[3 + offset] & 0xff) <<0;
return i0 | i1 | i2 | i3;
}
}