📄 phspacket.java
字号:
package sms.PHS;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.io.IOException;
public abstract class PHSPacket {
private static final int BYTE_SIZE = 1;
private static final int INTEGER_SIZE = 4;
private static final int LONG_SIZE = 8;
private byte packetBytes[];
private int packetOffset;
/**
* init the packetBytes and packetOffset
*/
protected PHSPacket() {
packetBytes = null;
packetOffset = 0;
}
private void appendBytes(byte abyte0[]) throws PHSException {
if (abyte0 == null || abyte0.length <= 0) {
throw new PHSException("appendBytes : null byte array !");
}
int i = getLength(); //得到包的长度
byte abyte1[] = new byte[i + abyte0.length];
for (int j = 0; j < i; j++) {
abyte1[j] = packetBytes[j];
//System.out.println("==============appendBytes"+j+"========"+abyte1);
}
packetBytes = abyte1;
setBytes(i, abyte0);
}
protected void encodePacket(PHSIO cmppio) throws PHSException {
if (cmppio == null) {
throw new PHSException("encodePacket : null io !");
}
try {
// System.out.println(
// "==================encondePacket PHSPacket.java 48=======================");
cmppio.write(packetBytes);
}
catch (Exception ex) {
throw new PHSException(ex);
}
}
protected byte getByte() throws PHSException {
int i = packetOffset;
if (i < 0 || i + 1 > packetBytes.length) {
throw new PHSException("getByte : not proper offset !");
}
else {
//System.out.println("==PHSPacket.java ========getByte" + i);
byte byte0 = packetBytes[i];
packetOffset++;
return byte0;
}
}
protected byte[] getBytes(int lenabyte0) throws PHSException {
//new 2004-12-12
//lenabyte0=lenabyte0>0?lenabyte0:0;
//new 2004-12-12
byte abyte0[] = new byte[lenabyte0];
int i = packetOffset;
if (i < 0 || i + abyte0.length > packetBytes.length) {
throw new PHSException("getBytes : not proper offset !");
}
for (int j = 0; j < abyte0.length; j++) {
abyte0[j] = packetBytes[i + j];
}
packetOffset += abyte0.length;
return abyte0;
}
protected int getInteger() throws PHSException {
int i = packetOffset;
if (i < 0 || i + 4 > packetBytes.length) {
throw new PHSException("getInteger : not proper offset !");
}
int j = 0;
for (int k = 0; k < 4; k++) {
j <<= 8;
j |= packetBytes[i + k] & 0xff;
}
packetOffset += 4;
return j;
}
protected int getLength() {
if (packetBytes == null) {
return 0;
}
else {
return packetBytes.length;
}
}
protected long getLong() throws PHSException {
int i = packetOffset;
if (i < 0 || i + 8 > packetBytes.length) {
throw new PHSException("getLong : not proper offset !");
}
int j = 0;
for (int k = 0; k < 8; k++) {
j <<= 8;
j |= packetBytes[i + k] & 0xff;
}
packetOffset += 8;
return (long) j;
}
protected int getOffset() {
return packetOffset;
}
protected void getPacket(PHSIO cmppio, int i) throws PHSException {
byte abyte0[];
if (cmppio == null) {
throw new PHSException("getPacket : null io !");
}
abyte0 = null;
try {
//System.out.println(
// "===================getPacket==PHSPacket.java 133====================");
abyte0 = cmppio.read(i);
// System.out.println("===================getPacket==PHSPacket.java 135=============abyte0"+new String(abyte0).getBytes());
}
catch (IOException ex) {
throw new PHSException(ex);
}
appendBytes(abyte0);
return;
}
protected byte[] getPacketBytes() {
//System.out.println("=====================getPHSPacketBytes======PHSPacket.java 145==================");
return packetBytes;
}
protected int getPacketOffset() {
return packetOffset;
}
protected String getString(int i) throws PHSException {
byte abyte0[] = new byte[i];
abyte0 = getBytes(i);
return new String(abyte0);
}
public void insertByte(byte byte0) throws PHSException {
int i = packetBytes == null ? 0 : packetBytes.length;
byte abyte0[] = new byte[i + 1];
for (int j = 0; j < i; j++) {
abyte0[j + 1] = packetBytes[j];
}
packetBytes = abyte0;
setByte(0, byte0);
}
protected void insertBytes(byte abyte0[]) throws PHSException {
if (abyte0 == null || abyte0.length <= 0) {
throw new PHSException("insertBytes : null byte array !");
}
int i = getLength();
byte abyte1[] = new byte[i + abyte0.length];
for (int j = 0; j < i; j++) {
abyte1[j + abyte0.length] = packetBytes[j];
}
packetBytes = abyte1;
setBytes(0, abyte0);
}
protected void insertInteger(int i) throws PHSException {
int j = getLength();
byte abyte0[] = new byte[j + 4];
for (int k = 0; k < j; k++) {
abyte0[k + 4] = packetBytes[k];
}
packetBytes = abyte0;
setInteger(0, i);
}
protected void insertIntegers(int aint0[]) throws PHSException {
if (aint0 == null || aint0.length <= 0) {
throw new PHSException("insertBytes : null byte array !");
}
for (int i = aint0.length - 1; i >= 0; i--) {
insertInteger(aint0[i]);
}
}
protected void insertLong(long i) throws PHSException {
int j = getLength();
byte abyte0[] = new byte[j + 8];
for (int k = 0; k < j; k++) {
abyte0[k + 8] = packetBytes[k];
}
packetBytes = abyte0;
setLong(0, i);
}
protected void insertString(String str0, int LenStr0) throws PHSException {
if (str0 == null) {
throw new PHSException("insertString : null String !");
}
if (LenStr0 < 0) {
throw new PHSException("insertStrings : LenStr0 can't < 0 !");
}
byte abyte1[] = new byte[LenStr0];
for (int i = 0; i < abyte1.length; i++) {
abyte1[i] = 0;
}
byte strbyte[] = str0.getBytes();
int m = str0.length() <= LenStr0 ? str0.length() : LenStr0;
for (int j = 0; j < m; j++) {
abyte1[j] = strbyte[j];
}
insertBytes(abyte1);
}
protected void insertString(String str0) throws PHSException {
if (str0 == null) {
throw new PHSException("insertString : null String !");
}
else {
insertBytes(str0.getBytes());
return;
}
}
protected void insertStrings(String str0[], int LenStr0) throws PHSException {
if (LenStr0 < 0) {
throw new PHSException("insertStrings : LenStr0 can't < 0 !");
}
if (str0 == null || str0.length <= 0) {
throw new PHSException("insertStrings : null String array !");
}
for (int i = str0.length - 1; i >= 0; i--) {
insertString(str0[i], LenStr0);
}
}
private void setByte(int i, byte byte0) throws PHSException {
if (i < 0 || i + 1 > packetBytes.length) {
throw new PHSException(
"setByte : not proper offset !=================>PHSPacket.java 255");
}
else {
packetBytes[i] = byte0;
return;
}
}
private void setBytes(int i, byte abyte0[]) throws PHSException {
if (abyte0 == null || abyte0.length <= 0) {
throw new PHSException(
"setBytes :null byte array !=================>PHSPacket.java 265");
}
if (packetBytes == null || packetBytes.length <= 0) {
throw new PHSException(
"setBytes :null buffer !=================>PHSPacket.java 268");
}
if (i < 0 || i + abyte0.length > packetBytes.length) {
throw new PHSException(
"setBytes : not proper offset !=================>PHSPacket.java 271");
}
for (int j = 0; j < abyte0.length; j++) {
packetBytes[i + j] = abyte0[j];
}
}
private void setInteger(int i, int j) throws PHSException {
if (packetBytes == null || packetBytes.length <= 0) {
throw new PHSException(
"setBytes :null buffer !=================>PHSPacket.java 281");
}
if (i < 0 || i + 4 > packetBytes.length) {
throw new PHSException(
"setInteger : not proper offset !=================>PHSPacket.java 284");
}
for (int k = 0; k < 4; k++) {
packetBytes[ (i + 4) - k - 1] = (byte) j;
j >>= 8;
}
}
private void setLong(int i, long j) throws PHSException {
if (packetBytes == null || packetBytes.length <= 0) {
throw new PHSException(
"setLong :null buffer !=================>PHSPacket.java 295");
}
if (i < 0 || i + 8 > packetBytes.length) {
throw new PHSException(
"setLong : not proper offset !=================>PHSPacket.java 298");
}
for (int k = 0; k < 8; k++) {
packetBytes[ (i + 8) - k - 1] = (byte) (int) j;
j >>= 8;
}
}
protected void setPacketBytes(byte packetBytes[]) {
this.packetBytes = packetBytes;
}
protected void setPacketOffset(int packetOffset) {
this.packetOffset = packetOffset;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -