class ByteBuffer {
/**
* @var <string> 缓冲区内容
*/
private $byteArray;
/**
* @var <int> 缓冲区的大小
*/
private $capacity;
/**
* @var <int> 缓冲区的上限
*/
private $limit;
/**
* @var <int> 缓冲区当前位置
*/
private $position;
/**
* 申请固定长度的缓冲区
* @param <int> $length
* @return ByteBuffer
*/
public static function allocate($length) {
return new ByteBuffer(str_repeat("\0", $length));
}
/**
* 将字符串放到缓冲区中
* @param <string> $byteArray
* @return ByteBuffer
*/
public static function wrap($byteArray) {
return new ByteBuffer($byteArray);
}
/**
* 构造缓冲区对象
* @param <string> $byteArray
*/
public function __construct($byteArray) {
$this->byteArray = $byteArray;
$this->capacity = strlen($byteArray);
$this->limit = $this->capacity;
$this->position = 0;
}
/**
* 获取缓冲区内容
* @return <string>
*/
public function getByteArray() {
return $this->byteArray;
}
/**
* 清除缓冲区(内容没有变化)
*/
public function clear() {
$this->limit = $this->capacity;
$this->position = 0;
}
/**
* 反转缓冲区
* @return ByteBuffer
*/
public function flip() {
$this->limit = $this->position;
$this->position = 0;
return $this;
}
/**
* 读取缓冲区内容
* @param <int> $length
* @return <string>
*/
public function get($length = null) {
if ($length === null) {
$length = $this->limit - $this->position;
} elseif ($length > $this->remaining()) {
throw new Exception("超出缓冲区范围");
}
$data = substr($this->byteArray, $this->position, $length);
$this->position += $length;
return $data;
}
/**
* 读取单字符内容
* @return <int>
*/
public function getByte() {
return ord($this->get(1));
}
/**
* 读取浮点型内容
* @return <float>
*/
public function getFloat() {
$data = unpack("f", $this->get(4));
return $data[1];
}
/**
* 读取长整型内容
* @return <long>
*/
public function getLong() {
$data = unpack("l", $this->get(4));
return $data[1];
}
/**
* 读取短整型内容
* @return <short>
*/
public function getShort() {
$data = unpack("v", $this->get(2));
return $data[1];
}
/**
* 读取空字符结尾之前的字符串内容
* @return <string>
*/
public function getString() {
$zeroByteIndex = strpos($this->byteArray, "\0", $this->position);
if ($zeroByteIndex === false) {
return '';
} else {
$dataString = $this->get($zeroByteIndex-$this->position);
$this->position++;
return $dataString;
}
}
/**
* 读取无符号长整型内容
* @return <unsigned long>
*/
public function getUnsignedLong() {
$data = unpack("V", $this->get(4));
return $data[1];
}
/**
* 设置上限值
* @param <int> $newLimit
* @return <int> or <null>
*/
public function limit($newLimit = null) {
if ($newLimit == null) {
return $this->limit;
} else {
$this->limit = $newLimit;
return null;
}
}
/**
* 获取当前位置
* @return <int>
*/
public function position() {
return $this->position;
}
/**
* 批量替换缓冲区内容
* @param <string> $byteArray
* @return ByteBuffer
*/
public function put($byteArray) {
$newPosition = min($this->remaining(), strlen($byteArray));
$this->byteArray = substr_replace($this->byteArray, $byteArray, $this->position, $newPosition);
$this->position = $newPosition;
return $this;
}
/**
* 获取缓冲区剩余内容的长度
* @return <int>
*/
public function remaining() {
return $this->limit - $this->position;
}
/**
* 重置缓冲区
* @return ByteBuffer
*/
public function rewind() {
$this->position = 0;
return $this;
}
}
?>