amorim778 / byte-array-php
A modern PHP ByteArray buffer with endian-aware read/write helpers, compression, and conversions.
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
pkg:composer/amorim778/byte-array-php
Requires (Dev)
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2025-12-06 17:15:41 UTC
README
Modern, endian-aware byte buffer for PHP inspired by ActionScript's ByteArray and Java's ByteBuffer.
Requirements
- PHP >= 8.1
- ext-zlib (for compression helpers)
- ext-iconv
Installation
composer require byte-array/byte-array
Quick start
<?php use ByteArray\ByteArray; require __DIR__ . '/vendor/autoload.php'; $buffer = new ByteArray(); $buffer->writeBoolean(true); $buffer->writeInt(42); $buffer->writeUTF('hello'); $buffer->writeDouble(3.14); $buffer->rewind(); $flag = $buffer->readBoolean(); $number = $buffer->readInt(); $text = $buffer->readUTF(); $pi = $buffer->readDouble();
Features
- Big- and little-endian reads/writes for common primitives (byte, short, int, float, double).
- UTF strings with length prefix plus raw UTF byte helpers.
- Safe bounds checks on every read with clear exceptions.
- Compression/ decompression (
zlib,gzip) with error handling. - Conversions to/from Base64 and hexadecimal.
- File helpers (
fromFile,toFile), slicing, duplication, and position helpers (rewind,seek,skip,isEOF). - Optional JSON-based serialization (
writeJson/readJson) as a safe alternative to PHPserialize. - Random byte generation for tokens and seeds.
Endianness
$buffer->setEndian(ByteArray::ENDIAN_LITTLE); // or ByteArray::ENDIAN_BIG
All numeric reads/writes honor the current setting.
Compression helpers
$buffer->compress(ByteArray::COMPRESSION_GZIP); $buffer->uncompress(ByteArray::COMPRESSION_GZIP);
Exceptions are thrown for unsupported algorithms or failures.
Serialization safety
writeObject/readObjectmirror the legacy PHPserialize/unserializeflow and must not be used with untrusted data.- Prefer the safe alternatives
writeJsonandreadJsonwhen exchanging data externally.
Testing
composer install
composer test
CI
GitHub Actions workflow at .github/workflows/tests.yml runs the PHPUnit suite on PHP 8.1 and 8.2.
License
MIT – see LICENSE.