?? 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 + -