PHPackages                             nelexa/buffer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nelexa/buffer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nelexa/buffer
=============

Reading And Writing Binary Data (incl. primitive types, ex. byte, ubyte, short, ushort, int, uint, long, float, double). The classes also help with porting the I/O operations of the JAVA code.

1.3.0(7y ago)33527.5k↓10.7%5[1 issues](https://github.com/Ne-Lexa/php-byte-buffer/issues)[2 PRs](https://github.com/Ne-Lexa/php-byte-buffer/pulls)3MITPHPPHP &gt;=5.4CI failing

Since Sep 8Pushed 6y ago2 watchersCompare

[ Source](https://github.com/Ne-Lexa/php-byte-buffer)[ Packagist](https://packagist.org/packages/nelexa/buffer)[ RSS](/packages/nelexa-buffer/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (9)Used By (3)

`nelexa/buffer` -&gt; Read And Write Binary Data
================================================

[](#nelexabuffer---read-and-write-binary-data)

[![Packagist Version](https://camo.githubusercontent.com/48c89e75487a2955804f60067e6c9d8ccf72b62b2197e4ef55e9b8eb88835e67/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e656c6578612f6275666665722e737667)](https://packagist.org/packages/nelexa/buffer)[![Packagist](https://camo.githubusercontent.com/45fad7c85dd0acd820c0e9abbd08c2c29900293195e3b0ddae248fcba6e2e95c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e656c6578612f6275666665722e7376673f636f6c6f723d253233666630303766)](https://packagist.org/packages/nelexa/buffer)[![Build Status](https://camo.githubusercontent.com/3456e9fe318a1f2fa2d7d5883957658aeb2d9c7cd53ef9873c5b352b808aed06/68747470733a2f2f7472617669732d63692e6f72672f4e652d4c6578612f7068702d627974652d6275666665722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Ne-Lexa/php-byte-buffer)[![License](https://camo.githubusercontent.com/619421b2bd09d2aeb03f43a76298d5ea13935ec898a18124517fb3703ac5fa36/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6e656c6578612f6275666665722e737667)](https://packagist.org/packages/nelexa/buffer)

This is classes defines methods for **reading and writing** values of all primitive types. Primitive values are translated to (or from) sequences of bytes according to the buffer's current byte order, which may be retrieved and modified via the order methods. The initial order of a byte buffer is always Buffer::BIG\_ENDIAN.

### Requirements

[](#requirements)

- PHP &gt;= 5.4 (64 bit)

### Installation

[](#installation)

```
composer require nelexa/buffer
```

### Documentation

[](#documentation)

Class `\Nelexa\Buffer` is abstract and base methods for all other buffers.

Initialize buffer as string.

```
$buffer = new \Nelexa\StringBuffer();
// or
$buffer = new \Nelexa\StringBuffer($text);
```

Initialize buffer as file.

```
$buffer = new \Nelexa\FileBuffer($filename);
```

Initialize buffer as memory resource (php://memory).

```
$buffer = new \Nelexa\MemoryReourceBuffer();
// or
$buffer = new \Nelexa\MemoryReourceBuffer($text);
```

Initialize buffer as stream resource.

```
$fp = fopen('php://temp', 'w+b');
// or
$buffer = new \Nelexa\ResourceBuffer($fp);
```

Set read only buffer

```
$buffer->setReadOnly(true);
```

Checking the possibility of recording in the buffer

```
$boolValue = $buffer->isReadOnly();
```

Modifies this buffer's byte order, either `Buffer::BIG_ENDIAN` or `Buffer::LITTLE_ENDIAN`

```
$buffer->setOrder(\Nelexa\Buffer::LITTLE_ENDIAN);
```

Get buffer's byte order

```
$byteOrder = $buffer->order();
```

Get buffer size.

```
$size = $buffer->size();
```

Set buffer position.

```
$buffer->setPosition($position);
```

Get buffer position.

```
$position = $buffer->position();
```

Skip bytes.

```
$buffer->skip($count);

// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
$buffer->skip(-7);
assert($buffer->position() === 3);
$buffer->skip(2);
assert($buffer->position() === 5);
```

Skip primitive type size.

```
$buffer->skipByte(); // skip 1 byte
$buffer->skipShort(); // skip 2 bytes
$buffer->skipInt(); // skip 4 bytes
$buffer->skipLong(); // skip 8 bytes
$buffer->skipFloat(); // skip 4 bytes
$buffer->skipDouble(); // skip 8 bytes
```

Rewinds this buffer. The position is set to zero.

```
$buffer->rewind();

// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
$buffer->rewind();
assert($buffer->position() === 0);
assert($buffer->size() === 10);
```

Flips this buffer. The limit is set to the current position and then the position is set to zero.

```
$buffer->flip();

// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
$buffer->setPosition(5);
$buffer->flip();
assert($buffer->position() === 0);
assert($buffer->size() === 5);
```

Returns the number of elements between the current position and the limit.

```
$remaining = $buffer->remaining();

// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
$buffer->setPosition(7);
assert($buffer->remaining() === 10 - 7);
```

Tells whether there are any elements between the current position and the limit. True if, and only if, there is at least one element remaining in this buffer

```
$boolValue = $buffer->hasRemaining();

// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
assert($buffer->hasRemaining() === false);
$buffer->setPosition(9);
assert($buffer->hasRemaining() === true);
```

Close buffer and release resources

```
$buffer->close();
```

### Read buffer

[](#read-buffer)

Read the entire contents of the buffer into a string without changing the position of the buffer.

```
$allBufferContent = $buffer->toString();

// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
$buffer->setPosition(4);
$allBufferContent = $buffer->toString();
assert($buffer->position() === 4);
assert($allBufferContent === 'Test value');
```

Reads the string at this buffer's current position, and then increments the position.

```
$content = $buffer->get($length);

// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
$buffer->setPosition(3);
$content = $buffer->get(5);
assert($buffer->position() === 8);
assert($content === 't val');
```

##### Read literal types

[](#read-literal-types)

MethodTypeValues`$buffer->getBoolean`boolean`true` or `false``$buffer->getByte()`byte-128 ... 127`$buffer->getUnsignedByte()`unsigned byte (ubyte)0 ... 255`$buffer->getShort()`short (2 bytes)-32768 ... 32767`$buffer->getUnsignedShort()`unsigned short (ushort)0 ... 65535`$buffer->getInt()`int (4 bytes)-2147483648 ... 2147483647`$buffer->getUnsignedInt()`unsigned int (uint)0 ... 4294967296`$buffer->getLong()`long (8 bytes)-9223372036854775808 ... 9223372036854775807`$buffer->getFloat()`float (4 bytes)single-precision 32-bit IEEE 754 floating point number`$buffer->getDouble()`double (5 bytes)double-precision 64-bit IEEE 754 floating point number`$buffer->getArrayBytes($length)`byte\[\]`array``$buffer->getString($length)`string (length bytes)`string``$buffer->getUTF()`string`string``$buffer->getUTF16($length)`string (length \* 2)`string`### Write to buffer

[](#write-to-buffer)

#### Insert bytes to buffer

[](#insert-bytes-to-buffer)

Insert string (byte\[\]) or Buffer to buffer.

```
$buffer->insert('content');
// or
$buffer->insert(new StringBuffer('Other buffer'));

// example
assert($buffer->position() === 0);
assert($buffer->size() === 0);
$buffer->insert('Test value');
assert($buffer->position() === 10);
assert($buffer->size() === 10);
$buffer->setPosition(4);
$buffer->insert('ed');
assert($buffer->position() === 6);
assert($buffer->size() === 12);
assert($buffer->toString() === 'Tested value');
```

##### Insert primitive types

[](#insert-primitive-types)

Insert boolean value `false` or `true`. Change size and position by +1.

```
$buffer->insertBoolean($boolValue);
```

Insert byte (-128 &gt;= byte &lt;= 127). Change size and position by +1.

```
$buffer->insertByte($byteValue);
```

Insert short value (-32768 &gt;= short &lt;= 32767). Change size and position by +2.

```
$buffer->insertShort($shortValue);
```

Insert integer value (-2147483648 &gt;= int &lt;= 2147483647). Change size and position by +4.

```
$buffer->insertInt($intValue);
```

Insert long value (-9223372036854775808 &gt;= long &lt;= 9223372036854775807). Change position +8.

```
$buffer->insertLong($longValue);
```

Insert float value (single-precision 32-bit IEEE 754 floating point number). Change position +4.

```
$buffer->insertFloat($floatValue);
```

Insert double value (double-precision 64-bit IEEE 754 floating point number). Change position +8.

```
$buffer->insertDouble($doubleValue);
```

Insert array bytes. Change size and position by +(size array).

```
$buffer->insertArrayBytes($bytes);
```

Insert string value. Change size and position by +(length string).

```
$buffer->insertString($string);
```

Insert UTF-8 string with encoding first two bytes as length string. Change size and position by +(2 + length string).

Analog java [java.io.DataOutputStream#writeUTF(String str)](https://docs.oracle.com/javase/8/docs/api/java/io/DataOutputStream.html#writeUTF-java.lang.String-)

```
$buffer->insertUTF($string);
```

Insert string with UTF-16 encoding. Change size and position by +(2 \* length string).

```
$buffer->insertUTF16($string);
```

#### Put bytes to buffer

[](#put-bytes-to-buffer)

Put string (byte\[\]) or Buffer to buffer and overwrite old value.

```
$buffer->put('content');
// or
$buffer->put(new StringBuffer('Other buffer'));

// example
assert($buffer->position() === 0);
assert($buffer->size() === 0);
$buffer->insert('Test value');
assert($buffer->position() === 10);
assert($buffer->size() === 10);
$buffer->setPosition(4);
$buffer->put('ed');
assert($buffer->position() === 6);
assert($buffer->size() === 10);
assert($buffer->toString() === 'Testedalue');
```

##### Put primitive types

[](#put-primitive-types)

Put boolean value `false` or `true`. Change position by +1.

```
$buffer->putBoolean($boolValue);
```

Put byte (-128 &gt;= byte &lt;= 127). Change position by +1.

```
$buffer->putByte($byteValue);
```

Put short value (-32768 &gt;= short &lt;= 32767). Change position by +2.

```
$buffer->putShort($shortValue);
```

Put integer value (-2147483648 &gt;= int &lt;= 2147483647). Change position by +4.

```
$buffer->putInt($intValue);
```

Put long value (-9223372036854775808 &gt;= long &lt;= 9223372036854775807). Change position by +8.

```
$buffer->putLong($longValue);
```

Put float value (single-precision 32-bit IEEE 754 floating point number). Change position +4.

```
$buffer->putFloat($floatValue);
```

Put double value (double-precision 64-bit IEEE 754 floating point number). Change position +8.

```
$buffer->putDouble($doubleValue);
```

Put array bytes. Change position by +(size array).

```
$buffer->putArrayBytes($bytes);
```

Insert string value. Change position by +(length string).

```
$buffer->putString($string);
```

Put UTF-8 string with encoding first two bytes as length string. Change position by +(2 + length string).

Analog java [java.io.DataOutputStream#writeUTF(String str)](https://docs.oracle.com/javase/8/docs/api/java/io/DataOutputStream.html#writeUTF-java.lang.String-)

```
$buffer->puttUTF($string);
```

Put string with UTF-16 encoding. Change position by +(2 \* length string).

```
$buffer->putUTF16($string);
```

#### Replace bytes by buffer

[](#replace-bytes-by-buffer)

Replace following a certain number of bytes by string or another Buffer.

```
$buffer->replace('content', $length);
// or
$buffer->insert(new StringBuffer('Other buffer'), $length);

// example
assert($buffer->position() === 0);
assert($buffer->size() === 0);
$buffer->insert('Test value');
assert($buffer->position() === 10);
assert($buffer->size() === 10);
$buffer->setPosition(4);
$buffer->replace('ed', 4); // remove 4 next bytes and insert 2 bytes
assert($buffer->position() === 6);
assert($buffer->size() === 8);
assert($buffer->toString() === 'Testedlue');
```

##### Replace by primitive types

[](#replace-by-primitive-types)

Replace by boolean value `false` or `true`. Change size by (-$length + 1) and position +1.

```
$buffer->replaceBoolean($boolValue, $length);
```

Replace by byte (-128 &gt;= byte &lt;= 127). Change size by (-$length + 1) and position +1.

```
$buffer->replaceByte($byteValue, $length);
```

Replace by short value (-32768 &gt;= short &lt;= 32767). Change size by (-$length + 2) and position +2.

```
$buffer->replaceShort($shortValue, $length);
```

Replace by integer value (-2147483648 &gt;= int &lt;= 2147483647). Change size by (-$length + 4) and position +4.

```
$buffer->replaceInt($intValue, $length);
```

Replace by long value (-9223372036854775808 &gt;= long &lt;= 9223372036854775807). Change size by (-$length + 8) and position +8.

```
$buffer->replaceLong($longValue, $length);
```

Replace by float value (single-precision 32-bit IEEE 754 floating point number). Change size by (-$length + 4) and position +4.

```
$buffer->replaceFloat($floatValue, $length);
```

Replace by double value (double-precision 64-bit IEEE 754 floating point number). Change size by (-$length + 8) and position +8.

```
$buffer->replaceDouble($doubleValue, $length);
```

Replace by array bytes. Change size by (-$length + size array) and position +(size array).

```
$buffer->replaceArrayBytes($bytes, $length);
```

Replace by string value. Change size by (-$length + length string) and position +(length string).

```
$buffer->replaceString($string, $length);
```

Replace by UTF-8 string with encoding first two bytes as length string. Change size by (-$length + 2 + length string) and position +(2 + length string).

Analog java [java.io.DataOutputStream#writeUTF(String str)](https://docs.oracle.com/javase/8/docs/api/java/io/DataOutputStream.html#writeUTF-java.lang.String-)

```
$buffer->replaceUTF($string, $length);
```

Replace by string with UTF-16 encoding. Change size by (-$length + 2 \* length string) and position +(2 \* length string).

```
$buffer->replaceUTF16($string, $length);
```

### Remove bytes by buffer

[](#remove-bytes-by-buffer)

Remove a certain number of bytes. Change size by -$length.

```
$buffer->remove($length);

// example
assert($buffer->position() === 0);
assert($buffer->size() === 0);
$buffer->insert('Test value');
assert($buffer->position() === 10);
assert($buffer->size() === 10);
$buffer->setPosition(4);
$buffer->remove(3); // remove 3 next bytes
assert($buffer->position() === 4);
assert($buffer->size() === 7);
assert($buffer->toString() === 'Testlue');
```

Remove all bytes. Truncate buffer.

```
$buffer->truncate($size = 0);

// example
assert($buffer->position() === 0);
assert($buffer->size() === 0);
$buffer->insert('Test value');
assert($buffer->position() === 10);
assert($buffer->size() === 10);
$buffer->truncate(0);
assert($buffer->position() === 0);
assert($buffer->size() === 0);
```

### Fluent interface

[](#fluent-interface)

```
// example
($buffer = new StringBuffer())
       ->insertByte(1)
       ->insertBoolean(true)
       ->insertShort(5551)
       ->skip(-2)
       ->insertUTF("Hello, World")
       ->truncate()
       ->insertString(str_rot13('Hello World'))
       ->setPosition(7)
       ->flip();

assert($this->buffer->size() === 7);
assert($this->buffer->position() === 0);
assert($this->buffer->toString() === str_rot13('Hello W'));
```

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~164 days

Recently: every ~236 days

Total

7

Last Release

2597d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.3

1.2.1PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/58ee2cf9c1aa935ef472b891a770bb2ce298a231af130f375cfcaa76d2a9a891?d=identicon)[Ne-Lexa](/maintainers/Ne-Lexa)

---

Top Contributors

[![Ne-Lexa](https://avatars.githubusercontent.com/u/17830391?v=4)](https://github.com/Ne-Lexa "Ne-Lexa (32 commits)")

---

Tags

iobinaryDoublefloatshortbytelongunpackpackintjavaprimitiveuintubyteushort

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nelexa-buffer/health.svg)

```
[![Health](https://phpackages.com/badges/nelexa-buffer/health.svg)](https://phpackages.com/packages/nelexa-buffer)
```

###  Alternatives

[react/stream

Event-driven readable and writable streams for non-blocking I/O in ReactPHP

691146.0M211](/packages/react-stream)[danog/phpstruct

PHP implementation of python's struct module.

1110.7k](/packages/danog-phpstruct)[alchemy/binary-driver

A set of tools to build binary drivers

19511.1M40](/packages/alchemy-binary-driver)[phpinnacle/buffer

PHPinnacle binary buffer implementation

2498.3k11](/packages/phpinnacle-buffer)[gabrielelana/byte-units

Library to parse, format and convert byte units

1752.3M21](/packages/gabrielelana-byte-units)[geo-io/interface

Geo I/O base interfaces.

626.4M7](/packages/geo-io-interface)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
