📄 lbpmessage.java
字号:
package ffcs.lbp;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
* <p>Title: 与收发模块的内部协议解析</p>
* <p>Description: Lbp内部协议解析基类</p>
* <p>Copyright: Copyright (c) 2008</p>
* <p>Company: 福富软件</p>
* @author chenxin
* @version 1.0
*/
public abstract class LbpMessage {
public static final int MAX_PACKAGE_SIZE = 2048;
public static final byte ASCII_NULL = (byte) 0;
private static final String US_ASCII = "US-ASCII";
/**
* 消息的总长度
* @return
*/
public abstract int getPackLength();
/**
* 消息的命令ID
* @return
*/
public abstract int getCommandId();
/**
* 消息包的序列号
* @return
*/
public abstract int getSequenceId();
/**
* 把buf消息转换成消息对象
* @param buf 字节缓冲区
* @return true 消息解析成功 false 消息解析不成功
* @throws ProtocolException 消息解析异常,如消息大小不对,或者消息包的格式错误等
*/
public abstract boolean readMsg(ByteBuffer buf) throws ProtocolException;
/**
* 把消息的各字段值写入缓冲区
* @param buf 字节缓冲区
* @return true 写入成功 false 写入失败
*/
public abstract boolean writeMsg(ByteBuffer buf);
/**
* 从srcBf拷贝字节到destByte中
* @param srcBf ByteBuffer 源缓冲区
* @param destByte byte[] 目的数组
* @param maxLength int 最大长度
* @param terminate byte 结束符
* @return int
*/
public static int copyToByte(ByteBuffer srcBf, byte[] destByte,
int maxLength, byte terminate) {
int index = 0;
if (srcBf == null && srcBf.remaining() < 1) {
return 0;
}
while (srcBf.remaining() > 0) {
byte b = srcBf.get();
destByte[index++] = b;
if (b == terminate || (index >= maxLength)) {
break;
}
}
return index;
}
/**
* 检查对象是否为空
* @param o
* @param name
*/
public static void checkNull(Object o, String name) {
if (o == null) {
throw new NullPointerException(name);
}
}
/**
* 返回消息简单信息
* 该方法似toString方法,但比toString方法返回的信息更简要,
* 主要用来调试用
* @return 返回字符串信息
*/
public String getSimpleInfo() {
return toString();
}
/**
*
* @param b
* @param offset
* @return
*/
public static String readCString(byte[] b, int offset) {
String s;
int p = offset;
while (b[p] != (byte) 0) {
p++;
}
try {
if (p > offset) {
s = new String(b, offset, p - offset, US_ASCII);
} else {
s = "";
}
} catch (java.io.UnsupportedEncodingException x) {
s = "";
}
return s;
}
/**
* 对字符串补指定的字符
* @param src
* @param length
* @param c
* @return
*/
public static String lpad(String src, int length, char c) {
if (src == null || src.length() >= length) {
return src;
}
String dest = src;
char[] temp = new char[length - src.length()];
for (int i = 0; i < length - src.length(); i++) {
temp[i] = c;
}
return (new String(temp) + dest);
}
/**
* Read a nul-terminated ASCII string from a ByteBuffer.
* @param bf
* @return
*/
public static String readCString(ByteBuffer bf) {
return readCString(bf, 256);
}
/**
* Read a nul-terminated ASCII string from a ByteBuffer.
* @param bf
* @param maxReadLength 最大读取数
* @return
*/
public static String readCString(ByteBuffer bf, int maxReadLength) {
if (bf == null && bf.remaining() < 1) {
return null;
}
byte[] data = new byte[maxReadLength];
int index = 0;
String s = null;
while (bf.remaining() > 0) {
if (index >= maxReadLength) {
break;
}
/*if (index >= maxReadLength) {
maxReadLength *= 2;
byte[] temp = new byte[maxReadLength];
System.arraycopy(data, 0, temp, 0, data.length);
data = temp;
}*/
data[index] = bf.get();
if (data[index] == ASCII_NULL) {
break;
}
index++;
}
try {
if(index>0){
s = new String(data, 0, index, US_ASCII);
}
} catch (java.io.UnsupportedEncodingException x) {
x.printStackTrace();
s = null;
}
return s;
}
/**
* 把写符串以"C-Octet String"方式(以NULL结束的ASCII字符串)写入ByteBuffer
* @param bf 要写入的ByteBuffer
* @param s 要写入的字符串
*/
public static void writeCString(ByteBuffer bf, String s) {
if (s != null) {
bf.put(s.getBytes());
}
bf.put(ASCII_NULL);
}
/**
* 把字符串以"Octet String"方式(不强制以0x00 结尾的定长字符串。当位数不足时,
在不明确注明的情况下,应左对齐,右补0x00。在
明确注明的情况下,以该字段的明确注明为准。)写入ByteBuffer
* @param bf 要写入的ByteBuffer
* @param s 要写入的字符串
*/
public static void writeOString(ByteBuffer bf, String s, int len) {
if (s == null) {
bf.put(new byte[len]);
return;
}
int l = s.getBytes().length;
bf.put(s.getBytes());
if (l < len) {
for (int i = 0; i < (len - l); i++)
bf.put(ASCII_NULL);
}
}
/**
* 把字符串以"Octet String"方式(不强制以0x00 结尾的定长字符串。当位数不足时,
在不明确注明的情况下,应左对齐,右补0x00。在
明确注明的情况下,以该字段的明确注明为准。)写入ByteBuffer
* @param bf 要写入的ByteBuffer
* @param s 要写入的字符串
*/
public static void writeOString(ByteBuffer bf, String s, int len,
byte msgFormat) {
String code = "";
if (msgFormat == (byte) 0)
code = US_ASCII;
else if (msgFormat == (byte) 15)
code = "GBK";
else if (msgFormat == (byte) 8)
code = "UTF-16";
else
code = "GBK";
int l = 0;
try {
l = s.getBytes(code).length;
bf.put(s.getBytes(code));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (l < len) {
for (int i = 0; i < (len - l); i++)
bf.put(ASCII_NULL);
}
}
/**
* 把字符串以"Octet String"方式(不强制以0x00 结尾的定长字符串。当位数不足时,
在不明确注明的情况下,应左对齐,右补0x00。在
明确注明的情况下,以该字段的明确注明为准。)读出ByteBuffer
* @param bf 要写入的ByteBuffer
* @param s 要写入的字符串
* modi by chenxin 2008.3.14 把循环改成从后往前循环
*/
public static String readOString(ByteBuffer bf, int len) {
String s = null;
byte[] data = new byte[len];
bf.get(data);
int index = data.length;
for (int i = data.length-1; i >0; i--) {
if (data[i] != ASCII_NULL) {
index = i;
break;
}
}
/* if (index == data.length)
index = len;*/
if(index>0){
s = new String(data, 0, index);
}
return s;
}
/**
* 把字符串以"Octet String"方式(不强制以0x00 结尾的定长字符串。当位数不足时,
在不明确注明的情况下,应左对齐,右补0x00。在
明确注明的情况下,以该字段的明确注明为准。)读出ByteBuffer
* @param bf 要写入的ByteBuffer
* @param s 要写入的字符串
* modi by chenxin 2008.3.14 把循环改成从后往前循环
*/
public static String readOString(ByteBuffer bf, int len, byte msgFormat) {
String s = null;
byte[] data = new byte[len];
bf.get(data);
int index = -1;
if (msgFormat != (byte) 8)
for (int i = data.length-1; i >0; i--) {
if (data[i] != ASCII_NULL) {
index = i;
break;
}
}
else
index = data.length;
String code = "";
if (msgFormat == (byte) 0)
code = US_ASCII;
else if (msgFormat == (byte) 15)
code = "GBK";
else if (msgFormat == (byte) 8)
code = "UTF-16";
else
code = "GBK";
try {
s = new String(data, 0, index, code);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return s;
}
public String toString() {
return super.toString();
}
/**
* Read an ASCII string from a byte array.
* @param b The byte array to read from.
* @param offset The offset into <code>b</code> to begin reading from.
* @param len The length of the string to read.
* @return A string decoded from <code>b</code> of length <code>len</code>.
* ASCII is used to convert the bytes into characters.
*/
public static String readString(byte[] b, int offset, int len) {
String s = "";
try {
if (len > 0) {
s = new String(b, offset, len, US_ASCII);
}
} catch (java.io.UnsupportedEncodingException x) {
}
return s;
}
public String strToHex(String val) {
if (val == null)
return "NULL";
byte b[] = val.getBytes();
return byte2hexString(b);
}
public static String byte2hexString(byte[] bytes) {
if (bytes == null) {
return null;
}
StringBuffer buf = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
if (((int) bytes[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString((int) bytes[i] & 0xff, 16));
if (i < (bytes.length - 1)) {
buf.append(' ');
}
}
return buf.toString();
}
public static String byte2hexString(ByteBuffer bufer) {
if (bufer == null) {
return null;
}
StringBuffer buf = new StringBuffer(bufer.remaining() * 2);
while (bufer.hasRemaining()) {
byte b = bufer.get();
if ((((int) b) & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString((int) b & 0xff, 16));
if (bufer.remaining() > 0) {
buf.append(' ');
}
}
return buf.toString();
}
public static String byteToHex(byte val) {
// if(val==null) return "NULL";
String stmp = (java.lang.Integer.toHexString(val & 0XFF));
if (stmp.length() == 1)
stmp = "0" + stmp;
return stmp;
}
public String intToHex(int val) {
String s = Integer.toHexString(val);
String tmp = "";
for (int i = 8; i > s.length(); i--)
tmp += "0";
return tmp + s;
}
public static int byteToUnsignInt(byte b) {
return (0x000000ff & b);
}
public static byte intToByte(int i) {
return ((byte) (0x000000ff & i));
}
public static int bytesToInt(byte[] b, int offset, int size) {
int num = 0;
int sw = 8 * (size - 1);
for (int loop = 0; loop < size; loop++) {
num |= ((int) b[offset + loop] & 0x00ff) << sw;
sw -= 8;
}
return num;
}
/**
* 按指定的时间格式转换系统当前的时间为字符串
* @param dateFormat 时间格式
* @return 时间字符串
*/
public static String dateConvertToStr(String dateFormat){
return dateConvertToStr(new Date(),dateFormat);
}
/**
* 把指定的时间根据指定的时间格式,转换为字符串
* @param date 时间
* @param dateFormat 时间格式
* @return 时间字符串
*/
public static String dateConvertToStr(Date date,String dateFormat){
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(date);
}
/**
* 单元测试
* @param args
* @throws Exception
*/
public static void main(String[] args)throws Exception{
String s = dateConvertToStr("yyyyMMddHHmmssSSS");
System.out.println(s);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -