You are here: Recent News » Documentation Pages List » AMF3 Specification » Parsing Integers

 

Parsing Integers

Integer-data is probably the single most used item in AMF3. The implementation of it is a little complicated so this page includes a reference implementation of parsing the AMF3 integer-data type:

private int readAMF3Integer() throws IOException {
    int n = 0;
    int b = in.readUnsignedByte();
    int result = 0;

    while ((b & 0x80) != 0 && n < 3) {
        result <<= 7;
        result |= (b & 0x7f);
        b = in.readUnsignedByte();
        n++;
    }
    if (n < 3) {
        result <<= 7;
        result |= b;
    } else {
    	/* Use all 8 bits from the 4th byte */
    	result <<= 8;
    	result |= b;

    	/* Check if the integer should be negative */
    	if (result > AMF3_MAX_VALUE) {
    		/* and extend the sign bit */
    		result -= (1 << 29);
    	}
    }

    return result;
}

documentation/amf3/parsing_integers.txt · Last modified: 2007/11/06 21:13 by aaronsmith