JBoss.orgCommunity Documentation
JBoss Communications ASN Library supports following encoding rules:
BER
JBoss Communications ASN Library
is stream oriented. User accesses
ASN
primitives by means of stream objects capable of proper decoding and
encoding.
Following classes deserve explanation:
org.mobicents.protocols.asn.Tag
- this class defines static values which are part of header(Tag).
Example value are tag value for Integer, BitString, etc.
org.mobicents.protocols.asn.BERStatics
- this class defines some static values which are specific for BER
encoding - like real encoding schemes(NR1,NR2...).
org.mobicents.protocols.asn.External
- this is special class which is used to represent "External" type.
Its special ASN type to say "anything" can be used.
Input and output stream - simpel classes which are core of this library. They allow to read/write chunks of data.
Simple decode integer primitive example:
// integer -128
byte[] data = new byte[] { 0x2, 0x1, (byte) 0x80 }; //encoded form
ByteArrayInputStream baIs = new ByteArrayInputStream(data);
AsnInputStream asnIs = new AsnInputStream(baIs);
int tag = asnIs.readTag();
if(Tag.INTEGER==tag)
{
long value = asnIs.readInteger();
//do somethin
}
Simple encode Real primitive example:
AsnOutputStream output = new AsnOutputStream();
output.writeReal(-3145.156d, BERStatics.REAL_NR1);
Complex example how to decode some constructed data structure:
// mandatory
private Long invokeId;
// optional
private Long linkedId;
// mandatory
private OperationCode operationCode;
// optional
private Parameter parameter;
public void doDecoding( AsnInputStream ais )
{
int len = ais.readLength();
if (len == 0x80) {
throw new ParseException("Unspiecified length is not supported.");
}
byte[] data = new byte[len];
if (len != ais.read(data)) {
throw new ParseException("Not enough data read.");
}
AsnInputStream localAis = new AsnInputStream(new ByteArrayInputStream(data));
int tag = localAis.readTag();
if (tag != _TAG_IID) {
throw new ParseException("Expected InvokeID tag, found: " + tag);
}
this.invokeId = localAis.readInteger();
if (localAis.available() <= 0) {
return;
}
tag = localAis.readTag();
if (tag == Tag.SEQUENCE) {
// sequence of OperationCode
len = localAis.readLength();
if (len == 0x80) {
throw new ParseException("Unspiecified length is not supported.");
}
data = new byte[len];
int tlen = localAis.read(data);
if (len != tlen) {
throw new ParseException("Not enough data read. Expected: " + len + ", actaul: " + tlen);
}
AsnInputStream sequenceStream = new AsnInputStream(new ByteArrayInputStream(data));
tag = sequenceStream.readTag();
if (tag == OperationCode._TAG_GLOBAL || tag == OperationCode._TAG_LOCAL) {
this.operationCode = TcapFactory.createOperationCode(tag, sequenceStream);
} else {
throw new ParseException("Expected Global|Local operation code.");
}
if (sequenceStream.available() > 0) {
tag = sequenceStream.readTag();
this.parameter = TcapFactory.createParameter(tag, sequenceStream);
} else {
throw new ParseException("Not enought data to decode Parameter part of result!");
}
} else {
throw new ParseException("Expected SEQUENCE tag for OperationCode and Parameter part, found: " + tag);
}
}