package {
import flash.utils.ByteArray;
public class BinaryPacket {
private var buffer:ByteArray;
private var tempBuffer:ByteArray = new ByteArray();//Used to compress number data types through a conversion to float
private var bitIndex:uint = 0;
private var packetErrors:uint = 0;
//Constructor: if no byteArray is passed in then it generates its own and assumes it's a new packet being created
public function BinaryPacket(buffer_:ByteArray = null) {
bitIndex = 0;
if(buffer_ == null){
buffer = new ByteArray();
}else{
buffer = buffer_;
}
}
//WRITE FUNCTIONS
//Writes an Event ID
public function WriteEventID(value:uint):void {
WriteDynamicUnsignedNbitInteger(value, 6);
}
//Writes a single bit either 0 or 1 onto the buffer
public function WriteBoolean(value:Boolean):void {
buffer[uint((bitIndex)/8)] |= int(value)<<(7-(bitIndex)%8); ++bitIndex; } //Writes an 8 bit unsigned byte onto the buffer public function WriteUnsignedByte(value:uint):void { var dataTypeSize:uint = 1;//byte size for(var copyBits:uint = 0; copyBits <>> ((dataTypeSize*8-1)-copyBits)) & 0x1)<<(7-(bitIndex+copyBits)%8); } bitIndex += dataTypeSize*8; } //Writes an 8 bit signed byte onto the buffer public function WriteSignedByte(value:int):void { var dataTypeSize:uint = 1;//byte size for(var copyBits:uint = 0; copyBits <>> ((dataTypeSize*8-1)-copyBits)) & 0x1)<<(7-(bitIndex+copyBits)%8); } bitIndex += dataTypeSize*8; } //Writes a 16 bit unsigned short onto the buffer public function WriteUnsignedShort(value:uint):void { var dataTypeSize:uint = 2;//byte size for(var copyBits:uint = 0; copyBits <>> ((dataTypeSize*8-1)-copyBits)) & 0x1)<<(7-(bitIndex+copyBits)%8); } bitIndex += dataTypeSize*8; } //Writes a 16 bit signed short onto the buffer public function WriteSignedShort(value:int):void { var dataTypeSize:uint = 2;//byte size for(var copyBits:uint = 0; copyBits <>> ((dataTypeSize*8-1)-copyBits)) & 0x1)<<(7-(bitIndex+copyBits)%8); } bitIndex += dataTypeSize*8; } //Writes a 32 bit unsigned integer onto the buffer public function WriteUnsignedInt(value:uint):void { var dataTypeSize:uint = 4;//byte size for(var copyBits:uint = 0; copyBits <>> ((dataTypeSize*8-1)-copyBits)) & 0x1)<<(7-(bitIndex+copyBits)%8); } bitIndex += dataTypeSize*8; } //Writes a 32 bit signed integer onto the buffer public function WriteSignedInt(value:int):void { var dataTypeSize:uint = 4;//byte size for(var copyBits:uint = 0; copyBits <>> ((dataTypeSize*8-1)-copyBits)) & 0x1)<<(7-(bitIndex+copyBits)%8); } bitIndex += dataTypeSize*8; } //Writes an n bit unsigned integer onto the buffer public function WriteCustomUnsignedInteger(value:uint, bits:uint):void { for(var copyBits:uint = 0; copyBits <>> ((bits-1)-copyBits)) & 0x1)<<(7-(bitIndex+copyBits)%8); } bitIndex += bits; } //Writes an n bit signed integer onto the buffer public function WriteCustomSignedInteger(value:int, bits:uint):void { for(var copyBits:uint = 0; copyBits <>> ((bits-1)-copyBits)) & 0x1)<<(7-(bitIndex+copyBits)%8); } bitIndex += bits; } //Writes a 32 bit floating point number onto the buffer public function WriteFloat(value:Number):void { tempBuffer.position = 0; tempBuffer.writeFloat(value); tempBuffer.position = 0; WriteUnsignedInt(tempBuffer.readInt()); } //Writes a 64 bit floating point number onto the buffer public function WriteDouble(value:Number):void { tempBuffer.position = 0; tempBuffer.writeDouble(value); tempBuffer.position = 0; WriteUnsignedInt(tempBuffer.readInt()); WriteUnsignedInt(tempBuffer.readInt()); } //Writes a number by writing intervals of 2^arrayBitInterval-1, the value on the interval of bits equal to 2^arrayBitInterval means to look onto the next interval //Example, let arrayBitInterval = 4 and value = 100, 2^4-1 is 15, so 100 % 15 is 6 sets of 4 bits set to 1111 with 10 left over so 1010 would be the 7th set. Good for small values that rarely go over 2^arrayBitInterval-1 public function WriteDynamicUnsignedInteger(value:uint, arrayBitInterval:uint = 4):void { var interval:uint = (0x1 << uint =" uint(Math.floor(value" uint =" 0;" copybits =" 0;" copybits =" 0;">> ((arrayBitInterval-1)-copyBits)) & 0x1)<<(7-(bitIndex+copyBits)%8); } bitIndex += arrayBitInterval; } //Writes a number by writing intervals of 2^arrayBitInterval-1, the value on the interval of bits equal to 2^arrayBitInterval means to look onto the next interval //The sign is copied first as 0 is negative and 1 is positive. The value is then copied as the absolute value //Example, let arrayBitInterval = 4 and value = -100, 2^4-1 is 15, so 100 % 15 is 6 sets of 4 bits set to 1111 with 10 left over so 1010 would be the 7th set. Good for small values that rarely go over 2^arrayBitInterval-1, a 0 bit is copied first public function WriteDynamicSignedInteger(value:int, arrayBitInterval:uint = 4):void { WriteBoolean(value > 0 ? true : false);
WriteDynamicUnsignedInteger(Math.abs(value), arrayBitInterval);
}
//Writes the number of bits and an extra bit to tell to go onto the next sequence of bits.
//In this way the bits for a number are broken up. Smaller numbers take up less space
public function WriteDynamicUnsignedNbitInteger(value:uint, bits:uint):void {
/*
var offset:uint = 0;
for(var copyBits:uint = 0; (0x1 << value ="="">> copyBits) & 0x1)<<(7-(bitIndex+copyBits+offset)%8); //trace("bit-" + (bitIndex+copyBits+offset) + " = " + ((value >> copyBits) & 0x1));
if((copyBits+1) % (bits) == 0 && copyBits != 0){
offset++;
if(uint(0x1 << (copyBits+1))-1 < uint =" 0;" copybits =" 0;">> copyBits) & 0x1) << (7 - (bitIndex + copyBits + offset) % 8); //trace("bit-" + (bitIndex+copyBits+offset) + " = " + ((value >> copyBits) & 0x1));
if ((copyBits + 1) % (bits) == 0 && copyBits != 0)
{
offset++;
var powerOfTwo:uint = (copyBits - offset <> 0 ? true : false);
WriteDynamicUnsignedNbitInteger(Math.abs(value), bits);
}
//Creates a value for the ratio value/2^bitResolution-1 in relationship to value/(max-min)
//This allows a floating point to have a resolution
public function WriteCustomResolutionFloat(min:Number, max:Number, value:Number, bitResolution:uint):void {
WriteCustomUnsignedInteger(Math.round((value-min)/(max-min) * Number(uint(0x1 << uint =" 4):void" uint =" value.length;" uint =" 0;" uint =" 0;" tempvalue =" int(value.charCodeAt(stringItr))-32;">= 0 && tempValue <= 128){ WriteCustomUnsignedInteger(tempValue, 7); } } } //WRITE FUNCTIONS - ARRAYS //Writes an array with a size written with a WriteDynamicUnsignedNbitInteger before the unsigned integers public function WriteUnsignedCustomIntegerArray(value:Array, bits:uint, arrayBits:uint):void {//arrayBitInterval means that for value < uint =" value.length;" uint =" 0;" uint =" value.length;" uint =" 0;" uint =" value.length;" uint =" 0;" uint =" value.length;" uint =" 0;" uint =" value.length;" uint =" 0;" uint =" value.length;" uint =" 0;" uint =" bitIndex;" uint =" 0;" uint =" bitIndex;" bitindex =" sizeBitIndex;" bitindex =" tempBitIndex;" uint =" bitIndex;" uint =" 0;" uint =" bitIndex;" bitindex =" sizeBitIndex;" bitindex =" tempBitIndex;" uint =" bitIndex;" uint =" 0;" uint =" bitIndex;" bitindex =" sizeBitIndex;" bitindex =" tempBitIndex;" uint =" bitIndex;" uint =" 0;" uint =" bitIndex;" bitindex =" sizeBitIndex;" bitindex =" tempBitIndex;" uint =" bitIndex;" uint =" 0;" uint =" bitIndex;" bitindex =" sizeBitIndex;" bitindex =" tempBitIndex;" uint =" packet_.GetBitIndex();" uint =" packet_.Length();//byte" uint =" 0;" boolean =" Boolean((buffer[uint(bitIndex/8)]">>(7-bitIndex%8)) & 0x1);
++bitIndex;
return value;
}
//Reads an 8 bits unsigned byte from the buffer
public function ReadUnsignedByte():uint {
var dataTypeSize:uint = 1;//byte size
var value:uint = 0x0;
for(var copyBits:uint = 0; copyBits <>>(7-(bitIndex+copyBits)%8)) & 0x1;
}
bitIndex += dataTypeSize*8;
return value;
}
//Reads an 8 bits signed byte from the buffer
public function ReadSignedByte():int {
var dataTypeSize:uint = 1;//byte size
var value:int = (buffer[uint(bitIndex/8)]>>(7-bitIndex%8)) & 0x1 == 1 ? 0xFFFFFFFF : 0x0;
for(var copyBits:uint = 0; copyBits <>>(7-(bitIndex+copyBits)%8)) & 0x1;
}
bitIndex += dataTypeSize*8;
return value;
}
//Reads a 16 bit unsigned short from the buffer
public function ReadUnsignedShort():uint {
var dataTypeSize:uint = 2;//byte size
var value:uint = 0x0;
for(var copyBits:uint = 0; copyBits <>>(7-(bitIndex+copyBits)%8)) & 0x1;
}
bitIndex += dataTypeSize*8;
return value;
}
//Reads a 16 bit signed short from the buffer
public function ReadSignedShort():int {
var dataTypeSize:uint = 2;//byte size
var value:int = (buffer[uint(bitIndex/8)]>>(7-bitIndex%8)) & 0x1 == 1 ? 0xFFFFFFFF : 0x0;
for(var copyBits:uint = 0; copyBits <>>(7-(bitIndex+copyBits)%8)) & 0x1;
}
bitIndex += dataTypeSize*8;
return value;
}
//Reads a 32 bit unsigned integer from the buffer
public function ReadUnsignedInt():uint {
var dataTypeSize:uint = 4;//byte size
var value:uint = 0x0;
for(var copyBits:uint = 0; copyBits <>>(7-(bitIndex+copyBits)%8)) & 0x1;
}
bitIndex += dataTypeSize*8;
return value;
}
//Reads a 32 bit signed integer from the buffer
public function ReadSignedInt():int {
var dataTypeSize:uint = 4;//byte size
var value:int = (buffer[uint(bitIndex/8)]>>(7-bitIndex%8)) & 0x1 == 1 ? 0xFFFFFFFF : 0x0;
for(var copyBits:uint = 0; copyBits <>>(7-(bitIndex+copyBits)%8)) & 0x1;
}
bitIndex += dataTypeSize*8;
return value;
}
//Reads an n bit custom unsigned integer from the buffer
public function ReadCustomUnsignedInteger(bits:uint):uint {
var value:uint = 0x0;
for(var copyBits:uint = 0; copyBits <>>(7-(bitIndex+copyBits)%8)) & 0x1;
}
bitIndex += bits;
return value;
}
//Reads an n bit custom signed integer from the buffer
public function ReadCustomSignedInteger(bits:uint):int {
var value:int = (buffer[uint(bitIndex/8)]>>(7-bitIndex%8)) & 0x1 == 1 ? 0xFFFFFFFF : 0x0;
for(var copyBits:uint = 0; copyBits <>>(7-(bitIndex+copyBits)%8)) & 0x1;
}
bitIndex += bits;
return value;
}
//Reads a 32 bit floating point number from the buffer
public function ReadFloat():Number {
tempBuffer.position = 0;
tempBuffer.writeInt(ReadUnsignedInt());
tempBuffer.position = 0;
return tempBuffer.readFloat();
}
//Reads a 64 bit floating point number from the buffer
public function ReadDouble():Number {
tempBuffer.position = 0;
tempBuffer.writeInt(ReadUnsignedInt());
tempBuffer.writeInt(ReadUnsignedInt());
tempBuffer.position = 0;
return tempBuffer.readDouble();
}
//Reads a dynamic unsigned integer that ranges in bytes. Has the ability to error out
public function ReadDynamicUnsignedInteger(arrayBitInterval:uint):uint {
var value:uint = 0;
var tempValue:uint;
var interval:uint = (0x1 << uint =" 0;" uint =" 0;" tempvalue =" 0;" uint =" 0;">>(7-(bitIndex+copyBits)%8)) & 0x1;
}
bitIndex += arrayBitInterval;
value += tempValue;
intervalCount++;
if(tempValue != interval) break;
}
if(error == 256){
packetErrors++;
}
return value;
}
//Reads a dynamic signed integer that ranges in bytes. Has the ability to error out
public function ReadDynamicSignedInteger(arrayBitInterval:uint):int {
var sign:int = ReadBoolean() ? 1 : -1;
var value:int = ReadDynamicUnsignedInteger(arrayBitInterval);
return value * sign;
}
//Reads the unsigned n bit integer. Has the ability to error out
public function ReadDynamicUnsignedNbitInteger(bits:uint = 4):uint {
var value:uint = 0;
var copyBits:uint = 0;
var tempValue:uint;
var offset:uint = 0;
var error:uint;
for (error = 0; error <>= buffer.length)
{
packetErrors++;
return 0;
}
tempValue = (buffer[(int)((bitIndex + copyBits) / 8)] >> (int)(7 - (bitIndex + copyBits) % 8)) & 0x1;
if ((copyBits + 1) % (bits + 1) == 0 && copyBits != 0)
{
//trace("-bit-" + copyBits + " " + tempValue);
offset++;
//uint nextBit = (uint)((buffer[(int)((bitIndex + copyBits + 1) / 8)] >> (int)(7 - (bitIndex + copyBits + 1) % 8)) & 0x1);
if (tempValue == 0)
{
break;
}
}
else
{
//trace("bit-" + copyBits + " " + tempValue);
value |= ((buffer[(int)((bitIndex + copyBits) / 8)] >> (int)(7 - (bitIndex + copyBits) % 8)) & 0x1) << (int)(copyBits - offset); } copyBits++; } if (error == 256) { packetErrors++; } bitIndex += copyBits + 1; return value; } //Reads the signed n bit integer. Has the ability to error out public function ReadDynamicSignedNbitInteger(bits:uint = 4):int { var sign:int = ReadBoolean() ? 1 : -1; var value:int = ReadDynamicUnsignedNbitInteger(bits); return value * sign; } //Reads a custom resolution float public function ReadCustomResolutionFloat(min:Number, max:Number, bitResolution:uint):Number { return ReadCustomUnsignedInteger(bitResolution)/Number(uint(0x1 << uint =" 4):String" string = "" uint =" ReadDynamicUnsignedNbitInteger(arrayBitInterval);" uint =" 0;" array =" new" uint =" ReadDynamicUnsignedNbitInteger(arrayBitInterval);" uint =" 0;" array =" new" uint =" ReadDynamicUnsignedNbitInteger(arrayBitInterval);" uint =" 0;" array =" new" uint =" ReadDynamicUnsignedNbitInteger(arrayBitInterval);" uint =" 0;" array =" new" uint =" ReadDynamicUnsignedNbitInteger(arrayBitInterval);" uint =" 0;" array =" new" uint =" ReadDynamicUnsignedNbitInteger(arrayBitInterval);" uint =" 0;" array =" new" uint =" ReadDynamicUnsignedNbitInteger(arrayBitInterval);" uint =" 0;" object =" new" uint =" ReadCustomUnsignedInteger(arrayBits);" uint =" 0;" object =" new" uint =" ReadCustomUnsignedInteger(arrayBits);" uint =" 0;" object =" new" uint =" ReadCustomUnsignedInteger(arrayBits);" uint =" 0;" object =" new" uint =" ReadCustomUnsignedInteger(arrayBits);" uint =" 0;" object =" new" uint =" ReadCustomUnsignedInteger(arrayBits);" uint =" 0;" binarypacket =" new" uint =" ReadDynamicUnsignedNbitInteger();" uint =" 0;" bitindex =" 0;}" bitindex =" index;}"> 0;
}
public function Trace():String {
var s:String = "";
for(var copyBits:uint = 0; copyBits <>>(7-(copyBits%8))) & 0x1) == 0 ? "0" : "1");
}
return s;
}
/* Useful operation on an int to display the 32-bit binary */
/*
var s:String = "";
for(var i:uint = 0; i <>>(31-i)) & 0x1) == 0 ? "0" : "1");
}
trace(s);
*/
}
}
Không có nhận xét nào:
Đăng nhận xét