JBoss.orgCommunity Documentation

Chapter 4. Protocol

4.1. Supported encoding rules
4.2. API
4.3. Examples

Mobicents ASN Library supports following the encoding rules:

Mobicents ASN Library is stream oriented. The user accesses ASN primitives by means of stream objects capable of proper decoding and encoding.

The following classes deserve explanation:

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);
    }
}