php ByteBuffer

<?php


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

转载于:https://my.oschina.net/thmz/blog/93691

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值