tiamo / bytebuffer
PHP 字节缓冲区
dev-master / 1.0.x-dev
2017-03-14 22:10 UTC
Requires
- php: >=5.3.0
- ext-bcmath: *
Requires (Dev)
- phpunit/phpunit: ~4.4
This package is auto-updated.
Last update: 2024-09-05 18:59:24 UTC
README
一个用于读取和写入二进制流的 PHP 库。
要求
- PHP 5.3.0 及以上版本。
- bcmath 扩展
安装
安装此扩展的首选方式是通过 composer。
可以运行
php composer.phar require --prefer-dist tiamo/bytebuffer "*"
或者在 composer.json
文件的 require 部分添加
"tiamo/bytebuffer": "*"
使用方法
写入器示例
// create a new empty stream (php://temp) $stream = \ByteBuffer\Stream::factory('', [ 'charset' => 'cp1251' // optional, default string data charset ]); $stream->isLittleEndian = false; // default value is true $stream->write('pure bytes'); $stream->writeBytes([255, 255, 255, 1]); $stream->writeString('hello'); $stream->writeString('привет', 'cp1251'); // optional, custom charset $stream->writeInt(1, 8); // int8 (1 byte) $stream->writeInt(1, 16); // int16 (2 bytes) $stream->writeInt(1, 24); // int24 (3 bytes) $stream->writeInt(1, 32); // int32 (4 bytes) $stream->writeInt(1, 64); // int64 (8bytes) $stream->writeFloat(1.234); $stream->writeDouble(12345.6789); $stream->writeNull(3); $stream->save('data.bin');
读取器示例
$stream = \ByteBuffer\Stream::factory(fopen('data.bin', 'r+')); $stream->read(10); $stream->readBytes(4); // [255, 255, 255, 1] $stream->readString(5); // hello $stream->readInt(8); // read unsigned int8 $stream->readInt(16, false); // read signed int8 // ... $stream->readFloat(); $stream->readDouble(); $stream->skip(3);
分配和管道示例
$stream = \ByteBuffer\Stream::factory(fopen('data.bin', 'r+')); $newStream = $stream->allocate(10); // allocate new buffer in memory $newStream->readString(4); // = pure $stream->pipe($newStream); $stream->readBytes(4); // = [255, 255, 255 ,1]
网络示例
$stream = \ByteBuffer\Stream::factory(fsockopen('google.com', 80)); $stream->readBytes(4);
远程文件示例
$stream = \ByteBuffer\Stream::factory(fopen('http://..../image.jpg', 'r')); $bytes = $stream->readBytes(2); if ($bytes[0] == 0xff && $bytes[1] == 0xd8) { echo 'valid jpeg!'; }
TODO
- 测试
许可证
根据 MIT 许可证 许可。