📄 protocolbuffer.java
字号:
package com.sxforever.protocol.cmpp30;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* <p>Title: sxforever</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: Hengyuan_COM</p>
*
* @author shanying
* @version 1.0
*/
public class ProtocolBuffer {
protected byte[] bytes = null;
protected int size = 0;
protected int position = 0;
protected static final byte BYNARY_ZERO =0;
protected static final byte ASCII_ZERO =0x00;
static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
private ProtocolBuffer(int size)
{
bytes = new byte[size];
this.size = size;
}
private ProtocolBuffer(byte[] bytes)
{
this.bytes = bytes;
this.size = bytes.length;
}
public ProtocolBuffer appendByte(byte b)
{
checkSize(1);
bytes[position] = b;
position = position+1;
return this ;
}
public ProtocolBuffer appendBytes(byte[] bs)
{
checkSize(bs.length);
System.arraycopy(bs,0,bytes,position,bs.length);
position = position+bs.length;
return this;
}
public ProtocolBuffer appendInt(int i)
{
checkSize(4);
byte[] bs = TypesTools.int2byte(i);
System.arraycopy(bs,0,bytes,position,bs.length);
position = position+4;
return this;
}
public ProtocolBuffer appendString(String str)
{
return this.appendString(str,str.getBytes().length);
}
/**
* append str,if str.length less than expectedLength,than ,append "0" to the str
* @param str
* @param expectedLength
*/
public ProtocolBuffer appendString(String str,int expectedLength)
{
byte[] bs = str.getBytes();
if(bs.length>expectedLength)
{
throw new IllegalArgumentException(str+" 长度过长");
}
checkSize(bs.length);
System.arraycopy(bs,0,bytes,position,bs.length);
int currentIndex = position+bs.length;
position = position+expectedLength;
//是否需要下面的补0操作,bytes[i]可能自动为0
int left = expectedLength-bs.length;
for(int i=currentIndex;i<position;i++)
{
bytes[i] = BYNARY_ZERO;
}
return this;
}
public void appendAsciiO(int length)
{
checkSize(length);
for(int i=0;i<length;i++)
{
bytes[position+i] = ASCII_ZERO;
}
position = position+length;
}
public void appendBynary0(int length)
{
checkSize(length);
for(int i=0;i<length;i++)
{
bytes[position+i] = BYNARY_ZERO;
}
position = position+length;
}
public byte readByte()
{
checkSize(1);
byte b = bytes[position];
position = position+1;
return b;
}
public byte[] readBytes(int length)
{
checkSize(length);
byte[] bs = new byte[length];
System.arraycopy(bytes,position,bs,0,bs.length);
position = position+length;
return bs;
}
public String readString(int expectedLength)
{
// if(expectedLength<0)
// {
// expectedLength = expectedLength+256;
// }
checkSize(expectedLength);
String str = new String(bytes,position,expectedLength).trim();//trim()??
position = position +expectedLength;
return str;
}
/**
* 读取17个字节的日期,参考smpp3.3 valid_time的定义
* @return
*/
public Date readDateString()
{
checkSize(17);
String str = new String(bytes,position,17).trim();
try
{
Date d = sdf.parse(str);
position = position+17;
return d;
}
catch(Exception e)
{
throw new IllegalArgumentException("读取时间错误");
}
}
public int readInt()
{
checkSize(4);
byte[] bs =new byte[4];
System.arraycopy(bytes,position,bs,0,4);
position = position+4;
return TypesTools.byte2int(bs);
}
/**
* 参考smpp3.3 valid_time的定义
* @param d
*/
public void appendStringDate(Date d)
{
checkSize(17);
String date = sdf.format(d);
this.appendString(date,17);
}
public byte[] toBytes()
{
return bytes;
}
public static ProtocolBuffer create(int size)
{
return new ProtocolBuffer(size);
}
public static ProtocolBuffer create(byte[] bytes)
{
return new ProtocolBuffer(bytes);
}
private void checkSize(int length)
{
if(size<(position+length)) throw new IllegalArgumentException("超过了Buffer长度"+"from "+position +" length "+length);
else return ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -