2017 © Pedro Peláez
 

library bytebuffer

PHP Byte Buffer

image

tiamo/bytebuffer

PHP Byte Buffer

  • Tuesday, March 14, 2017
  • by tiamo
  • Repository
  • 0 Watchers
  • 2 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

ByteBuffer

A PHP library for reading and writing binary streams., (*1)

Requirements

  • PHP 5.3.0 and up.
  • bcmath extension

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist tiamo/bytebuffer "*"

or add, (*4)

"tiamo/bytebuffer": "*"

to the require section of your composer.json file., (*5)

Usage

Writer example:, (*6)

// 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');

Reader example:, (*7)

$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);

Allocate and pipe example:, (*8)

$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]

Network example:, (*9)

$stream = \ByteBuffer\Stream::factory(fsockopen('google.com', 80));
$stream->readBytes(4);

Remote file example:, (*10)

$stream = \ByteBuffer\Stream::factory(fopen('http://..../image.jpg', 'r'));
$bytes = $stream->readBytes(2);
if ($bytes[0] == 0xff && $bytes[1] == 0xd8) {
   echo 'valid jpeg!';
}

TODO

  • tests

License

Licensed under the MIT license., (*11)

The Versions

14/03 2017

dev-master

9999999-dev

PHP Byte Buffer

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-bcmath *

 

The Development Requires

by Kornienko Vladislav

parser binary stream buffer