📄 entityhead.java
字号:
package com.pub.backserver;
import org.apache.mina.common.ByteBuffer;
public class EntityHead implements IEntity {
//Black List
public static final int QUERY_BLACK = 0x00000001;
public static final int QUERY_BLACK_RESP = 0x80000001;
public static final int INSERT_BLACK = 0x00000010;
public static final int INSERT_BLACK_RESP = 0x80000010;
public static final int DELETE_BLACK = 0x00000100;
public static final int DELETE_BLACK_RESP = 0x80000100;
//Mobile Phase
public static final int QUERY_PHASE = 0x00000002;
public static final int QUERY_PHASE_RESP = 0x80000002;
public static final int INSERT_PHASE = 0x00000020;
public static final int INSERT_PHASE_RESP = 0x80000020;
public static final int DELETE_PHASE = 0x00000200;
public static final int DELETE_PHASE_RESP = 0x80000200;
//Product Order
public static final int QUERY_ORDER = 0x00000003;
public static final int QUERY_ORDER_RESP = 0x80000003;
public static final int INSERT_ORDER = 0x00000030;
public static final int INSERT_ORDER_RESP = 0x80000030;
public static final int DELETE_ORDER = 0x00000300;
public static final int DELETE_ORDER_RESP = 0x80000300;
public final static int HeaderLength = 8;
private int totalLength = HeaderLength; // 消息的总长度(字节)
private int commandID; // 命令ID
public EntityHead(int totalLength) {
this.totalLength = totalLength;
}
public EntityHead(int totalLength, int commandID) {
this.totalLength = totalLength;
this.commandID = commandID;
}
public int readPackage(ByteBuffer inBuf) throws Exception {
totalLength = inBuf.getInt();
commandID = inBuf.getInt();
return 0;
}
public int writePackage(ByteBuffer outBuf) throws Exception {
outBuf.putInt(totalLength);
outBuf.putInt(commandID);
return 0;
}
public int readPackage(java.nio.ByteBuffer inBuf) throws Exception {
totalLength = inBuf.getInt();
commandID = inBuf.getInt();
return 0;
}
public int writePackage(java.nio.ByteBuffer outBuf) throws Exception {
outBuf.putInt(totalLength);
outBuf.putInt(commandID);
return 0;
}
public int getHeaderLength() {
return HeaderLength;
}
public int getPackageLength() {
return HeaderLength;
}
public int getCommandID() {
return commandID;
}
public int getTotalLength() {
return totalLength;
}
public void setCommandID(int commandID) {
this.commandID = commandID;
}
public void setTotalLength(int totalLength) {
this.totalLength = totalLength;
}
static protected void str2bytebuffer(ByteBuffer buf, String s, int len) {
if (buf.remaining() <= len - 1)
return;
if (s == null) {
s = "";
}
if (s.length() < len) {
for (int i = 0; i < s.length(); i++) {
buf.put((byte) s.charAt(i));
}
for (int i = s.length(); i < len; i++) {
buf.put((byte) 0);
}
} else {
for (int i = 0; i < len; i++) {
buf.put((byte) s.charAt(i));
}
}
}
static protected String bytebuffer2str(ByteBuffer buf, int len) {
if (buf.remaining() <= len - 1)
return null;
String r = new String();
boolean flag = true;
for (int i = 0; i < len; i++) {
byte temp;
if ((temp = buf.get()) != '\0' && flag) {
r += (char) temp;
} else {
flag = false;
}
}
return r;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -