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

v1.0.0 2025-12-06 17:08 UTC

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 PHP serialize.
  • 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/readObject mirror the legacy PHP serialize/unserialize flow and must not be used with untrusted data.
  • Prefer the safe alternatives writeJson and readJson when 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.