hardcastle/buffer

PHP implementation of the NodeJS Buffer class

Maintainers

Package info

github.com/AlexanderBuzz/Buffer

pkg:composer/hardcastle/buffer

Statistics

Installs: 9 021

Dependents: 2

Suggesters: 0

Stars: 2

Open Issues: 0

1.0.0 2026-03-20 12:08 UTC

This package is auto-updated.

Last update: 2026-03-20 12:16:32 UTC


README

PHP implementation of the Node.js Buffer module, available for PHP 8.2+.

Features

  • Fully compliant with Node.js Buffer API.
  • Supports various encodings: utf8, hex, base64.
  • Memory-efficient storage using SplFixedArray.
  • Supports reading and writing various data types:
    • Integers: Int8, UInt8, Int16BE/LE, UInt16BE/LE, Int32BE/LE, UInt32BE/LE.
    • Floats & Doubles: FloatBE/LE, DoubleBE/LE.
  • Utility methods: fill, write, copy, compare, indexOf, lastIndexOf, includes, swap16/32/64.
  • ArrayAccess implementation for easy byte access.

Installation

composer require hardcastle/buffer

Usage

Create Buffer

use Hardcastle\Buffer\Buffer;

// From size
$buf = Buffer::alloc(10);

// From array
$buf = Buffer::from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);

// From string
$buf = Buffer::from('hello world', 'utf8');

// From hex
$buf = Buffer::from('627566666572', 'hex');

// From base64
$buf = Buffer::from('aGVsbG8=', 'base64');

Access & Modification

$buf = Buffer::alloc(10);
$buf[0] = 0x41; // 'A'
echo $buf[0]; // 65
echo $buf->length; // 10

$buf->write('hello', 1);
echo $buf->toUtf8(); // Ahello

Reading & Writing

$buf = Buffer::alloc(4);
$buf->writeInt32BE(0x12345678, 0);
echo dechex($buf->readInt32BE(0)); // 12345678

License

MIT