A PHP ByteArray class that works like the one in as3
A lot of the data being sent from flash to php is just bytes to save bandwidth, so I wanted a php class that worked simular to the ByteArray class in actionscript 3. So I made this one.
Cheers
-
<?php
-
/**
-
* ByteArray class
-
*
-
* Should work close to the ActionScript 3 ByteArray class
-
*
-
* Copyright (c) 2009, Mikael Brandt.
-
* This code is free: you can redistribute it and/or modify
-
* it under the terms of the GNU General Public License as published by
-
* the Free Software Foundation, either version 3 of the License, or
-
* (at your option) any later version.
-
*
-
* This program is distributed in the hope that it will be useful,
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-
* GNU General Public License for more details.
-
*
-
* You should have received a copy of the GNU General Public License
-
* along with this code. If not, see <http://www.gnu.org/licenses/>.
-
*
-
* @author Mikael Brandt <mikaelbrandt@gmail.com> <www.mikaelbrandt.com>
-
* @version 1.0
-
*
-
* @copyright Copyright (c) 2009, Mikael Brandt.
-
* @license GNU General Public License
-
*/
-
-
class ByteArray {
-
-
/**
-
* The bytes.
-
*
-
* @var array
-
*/
-
private $_bytes;
-
-
/**
-
* Current byte stream position.
-
*
-
* @var int
-
*/
-
public $position;
-
-
/**
-
* Default constructor.
-
*
-
* @return void
-
* @param array[optional] $bytes
-
*/
-
public function __construct($bytes = array())
-
{
-
$this->_bytes = $bytes;
-
-
$this->position = 0;
-
}
-
-
/**
-
* Get count of bytes in the byte stream.
-
*
-
* @return int
-
*/
-
public function length()
-
{
-
return count($this->_bytes);
-
}
-
-
/**
-
* Get the bytes left from the current position to the end of the byte array.
-
*
-
* @return int
-
*/
-
public function bytesAvailable()
-
{
-
return count($this->_bytes) - $this->position;
-
}
-
-
/**
-
* Replace the current position to a new value. If jump is true, then
-
* jump to the next position in the byte stream.
-
*
-
* @return void
-
* @param int[optional] $value
-
* @param boolean[optional] $jump
-
*/
-
public function changeto($value = 0, $jump = false)
-
{
-
$this->_bytes[$this->position] = chr($value);
-
-
if ($jump) $this->position++;
-
}
-
-
/**
-
* Get the byte stream as a string.
-
*
-
* @return string
-
*/
-
public function toString()
-
{
-
return implode($this->_bytes);
-
}
-
-
/**
-
* Get the byte stream as base64.
-
*
-
* @return string
-
*/
-
public function base64()
-
{
-
return base64_encode($this->toString());
-
}
-
-
/**
-
* Reads a byte from the byte stream.
-
*
-
* @return int
-
*/
-
public function readByte()
-
{
-
$returnByte = ord($this->_bytes[$this->position]);
-
-
$this->position++;
-
-
return $returnByte;
-
}
-
-
/**
-
* Reads a 16-bit integer from the byte stream.
-
*
-
* @return int
-
*/
-
public function readShort()
-
{
-
$returnShort = (ord($this->_bytes[$this->position])<<8) |
-
(ord($this->_bytes[$this->position+1]));
-
-
$this->position +=2;
-
-
return $returnShort;
-
}
-
-
/**
-
* Reads a 32-bit integer from the byte stream.
-
*
-
* @return int
-
*/
-
public function readInt()
-
{
-
$returnInt = (ord($this->_bytes[$this->position])<<24) |
-
(ord($this->_bytes[$this->position+1])<<16) |
-
(ord($this->_bytes[$this->position+2])<<8) |
-
(ord($this->_bytes[$this->position+3]));
-
-
$this->position +=4;
-
-
return $returnInt;
-
}
-
-
/**
-
* Write a value to the byte stream at the current position.
-
*
-
* @return void
-
* @param int[optional] $value
-
*/
-
public function writeByte($value = 0)
-
{
-
array_splice($this->_bytes, $this->position, 0, chr($value));
-
-
$this->position++;
-
}
-
-
/**
-
* Writes a 16-bit integer to the byte stream at the current position.
-
*
-
* @return void
-
* @param int[optional] $value
-
*/
-
public function writeShort($value = 0)
-
{
-
array_splice(
-
$this->_bytes,
-
$this->position,
-
0,
-
array(
-
chr(($value >> 8) & 0xff),
-
chr($value & 0xff)
-
)
-
);
-
-
$this->position += 2;
-
}
-
-
/**
-
* Writes a 32-bit integer to the byte stream at the current position.
-
*
-
* @return void
-
* @param int[optional] $value
-
*/
-
public function writeInt($value = 0)
-
{
-
array_splice(
-
$this->_bytes,
-
$this->position,
-
0,
-
array(
-
chr(($value >> 24) & 0xff),
-
chr(($value >> 16) & 0xff),
-
chr(($value >> 8) & 0xff),
-
chr($value & 0xff)
-
)
-
);
-
-
$this->position += 4;
-
}
-
}
-
?>
Comments(0)