📄 msghead.java
字号:
/**
*
*/
package com.aceway.vas.sjcraw.cbgp201;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import com.aceway.vas.sjcraw.cbgp201.common.DataFormat;
import com.aceway.vas.sjcraw.cbgp201.common.MsgInfo;
/**
* @标题: 华为彩铃平台接口规范
* @说明:
* @版权: Copyright(c) 2007
* @公司: 北京汉铭信通科技有限公司
* @部门: 增值业务部
* @作者: 武达
* @Jun 5, 2007
*/
public class MsgHead {
private String commandLength;
private String category;
private String subCommand;
private int opcode;
private String commandStatus;
private String souAddr;
private String desAddr;
private String seqNo;
private String linkId;
private ByteBuffer headBuffer;
public ByteBuffer getHeadBuffer() {
return headBuffer;
}
public MsgHead(byte[] bytes){
headBuffer = ByteBuffer.allocate(MsgInfo.LEN_HEAD);
headBuffer.put(bytes);
headBuffer.flip();
headBuffer.position(0);
//读取信息
try {
this.commandLength = new String(headBuffer.array(), 0, 4, "gbk");
this.category = new String(headBuffer.array(), 4, 2, "gbk");
this.subCommand = new String(headBuffer.array(), 6, 2, "gbk");
this.opcode = headBuffer.getInt(8);
this.commandStatus = new String(headBuffer.array(), 12, 4, "gbk");
this.souAddr = new String(headBuffer.array(), 16, 6, "gbk");
this.desAddr = new String(headBuffer.array(), 22, 6, "gbk");
this.seqNo = new String(headBuffer.array(), 28, 10, "gbk");
this.linkId = new String(headBuffer.array(), 38, 20, "gbk");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public MsgHead(String commandLength, int operCode, String commandStatus, String souAddr,
String desAddr, String seqNo, String linkId){
this.headBuffer = ByteBuffer.allocate(MsgInfo.LEN_HEAD);
this.commandLength = DataFormat.makeInteger(commandLength.trim(), 4);
this.category = DataFormat.makeInteger(((int)operCode >> 24 & 0xff), 2);
this.subCommand = DataFormat.makeInteger(((int)operCode >> 16 & 0x00ff), 2);
this.opcode = (int)operCode & 0x0000ffff;
this.commandStatus=DataFormat.makeString(commandStatus,4);
this.souAddr = DataFormat.makeString(souAddr, 6);
this.desAddr = DataFormat.makeString(desAddr, 6);
this.seqNo = DataFormat.makeString(seqNo, 10);
this.linkId = DataFormat.makeString(linkId, 20);
try {
this.headBuffer.position(0);
this.headBuffer.put(this.commandLength.getBytes("gbk"));
this.headBuffer.position(4);
this.headBuffer.put(this.category.getBytes("gbk"));
this.headBuffer.position(6);
this.headBuffer.put(this.subCommand.getBytes("gbk"));
this.headBuffer.position(8);
this.headBuffer.put(DataFormat.int2bytes(this.opcode));
this.headBuffer.position(12);
this.headBuffer.put(this.commandStatus.getBytes("gbk"));
this.headBuffer.position(16);
this.headBuffer.put(this.souAddr.getBytes("gbk"));
this.headBuffer.position(22);
this.headBuffer.put(this.desAddr.getBytes("gbk"));
this.headBuffer.position(28);
this.headBuffer.put(this.seqNo.getBytes("gbk"));
this.headBuffer.position(38);
this.headBuffer.put(this.linkId.getBytes("gbk"));
this.headBuffer.flip();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public String getCategory() {
return category;
}
public String getCommandLength() {
return commandLength;
}
public String getCommandStatus() {
return commandStatus;
}
public String getSeqNo() {
return seqNo;
}
public String getDesAddr() {
return desAddr;
}
public String getLinkId() {
return linkId;
}
public String getSouAddr() {
return souAddr;
}
public String getSubCommand() {
return subCommand;
}
public void setCategory(String category) {
this.category = category;
}
public void setCommandLength(String commandLength) {
this.commandLength = commandLength;
}
public void setCommandStatus(String commandStatus) {
this.commandStatus = commandStatus;
}
public void setSeqNo(String seqNo) {
this.seqNo = seqNo;
}
public void setDesAddr(String desAddr) {
this.desAddr = desAddr;
}
public void setLinkId(String linkId) {
this.linkId = linkId;
}
public int getOpcode() {
return opcode;
}
public void setOpcode(int opcode) {
this.opcode = opcode;
}
public void setSouAddr(String souAddr) {
this.souAddr = souAddr;
}
public void setSubCommand(String subCommand) {
this.subCommand = subCommand;
}
public static void main(String[] args){
/*
command_length 4 String 此域为字符形式表示的十进制数字,表示消息包(包括消息头和消息体的总长)的长度,最大长度为9999字节
category 2 String 01:业务处理接口 06:管理接口 08:网管接口
sub_command 2 String 01:业务请求 02:业务请求应答
opcode 4 Integer 操作码,具体参考8.1.1
command_status 4 String 此域表示一个命令的成功与失败,如失败,指示引起失败的错误类型等信息,取值参照附录8.1.2。它在请求中必须设为0。
sou_addr 6 String 此域为消息源地址,表示消息发起的原始发起方地址描述字符串。该字符串为特征字符串。
des_addr 6 String 此域为消息目的地址,表示消息希望到达的终端地址描述字符串。该字符串为特征字符串
seq_no 10 String 此域为字符形式表示的十进制数字,表示一个序列号,由消息发起者设定,应答者对应给回此序列号。每发起一次请求,该序列号加1。序列号范围:0000000000-9999999999,循环使用。
LinkID 20 String 交易标识,用于唯一标识一次完整的业务。该域为字符形式表示的十进制数字,由消息发起者设定,应答者对应给回此序列号。每发起一次业务,该序列号加1。
*/
String str = "1111080201015555666666777777888888888899999999999999999999";
// ByteBuffer buff = ByteBuffer.allocate(58);
// buff.put("00580802".getBytes());
// buff.putInt(0x0101);
// buff.put("5555666666777777888888888899999999999999999999".getBytes());
// buff.flip();
// MsgHead mh = new MsgHead(str.getBytes());
// System.out.println(mh.getCommandLength());
// System.out.println(mh.getCategory());
// System.out.println(mh.getSubCommand());
// System.out.println(mh.getOpcode());
// System.out.println(mh.getCommandStatus());
// System.out.println(mh.getSouAddr());
// System.out.println(mh.getDesAddr());
// System.out.println(mh.getSeqNo());
// System.out.println(mh.getLinkId());
System.out.println("--------------------------------");
MsgHead mh2 = new MsgHead("58",0x08020201,"0","","","88888888","999999999");
System.out.println(mh2.getCommandLength());
System.out.println(mh2.getCategory());
System.out.println(mh2.getSubCommand());
System.out.println(mh2.getOpcode());
System.out.println(mh2.getCommandStatus());
System.out.println(mh2.getSouAddr());
System.out.println(mh2.getDesAddr());
System.out.println(mh2.getSeqNo());
System.out.println(mh2.getLinkId());
int a = 0x08020101;
System.out.println(513 == 0x0201);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -