{"id":13704343,"url":"https://github.com/Ne-Lexa/php-byte-buffer","last_synced_at":"2025-05-05T09:33:46.032Z","repository":{"id":57024778,"uuid":"67699806","full_name":"Ne-Lexa/php-byte-buffer","owner":"Ne-Lexa","description":"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.","archived":false,"fork":false,"pushed_at":"2020-06-02T12:59:28.000Z","size":64,"stargazers_count":34,"open_issues_count":2,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-20T23:14:24.718Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ne-Lexa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-08T12:08:56.000Z","updated_at":"2023-12-18T15:25:43.000Z","dependencies_parsed_at":"2022-08-23T13:50:34.375Z","dependency_job_id":null,"html_url":"https://github.com/Ne-Lexa/php-byte-buffer","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ne-Lexa%2Fphp-byte-buffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ne-Lexa%2Fphp-byte-buffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ne-Lexa%2Fphp-byte-buffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ne-Lexa%2Fphp-byte-buffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ne-Lexa","download_url":"https://codeload.github.com/Ne-Lexa/php-byte-buffer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224439902,"owners_count":17311546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T21:01:07.822Z","updated_at":"2024-11-13T11:31:16.181Z","avatar_url":"https://github.com/Ne-Lexa.png","language":"PHP","funding_links":[],"categories":["目录"],"sub_categories":["流 Streams"],"readme":"# `nelexa/buffer` -\u003e Read And Write Binary Data\n\n[![Packagist Version](https://img.shields.io/packagist/v/nelexa/buffer.svg)](https://packagist.org/packages/nelexa/buffer)\n[![Packagist](https://img.shields.io/packagist/dt/nelexa/buffer.svg?color=%23ff007f)](https://packagist.org/packages/nelexa/buffer)\n[![Build Status](https://travis-ci.org/Ne-Lexa/php-byte-buffer.svg?branch=master)](https://travis-ci.org/Ne-Lexa/php-byte-buffer\n)\n[![License](https://img.shields.io/packagist/l/nelexa/buffer.svg)](https://packagist.org/packages/nelexa/buffer)\n\nThis 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.\n \n### Requirements\n* PHP \u003e= 5.4 (64 bit)\n\n### Installation\n```bash\ncomposer require nelexa/buffer\n```\n\n### Documentation\n\nClass `\\Nelexa\\Buffer` is abstract and base methods for all other buffers.\n\nInitialize buffer as string.\n```php\n$buffer = new \\Nelexa\\StringBuffer();\n// or\n$buffer = new \\Nelexa\\StringBuffer($text);\n```\n\nInitialize buffer as file.\n```php\n$buffer = new \\Nelexa\\FileBuffer($filename);\n```\n\nInitialize buffer as memory resource (php://memory).\n```php\n$buffer = new \\Nelexa\\MemoryReourceBuffer();\n// or\n$buffer = new \\Nelexa\\MemoryReourceBuffer($text);\n```\n\nInitialize buffer as stream resource.\n```php\n$fp = fopen('php://temp', 'w+b');\n// or\n$buffer = new \\Nelexa\\ResourceBuffer($fp);\n```\n\nSet read only buffer\n```php\n$buffer-\u003esetReadOnly(true);\n```\n\nChecking the possibility of recording in the buffer\n```php\n$boolValue = $buffer-\u003eisReadOnly();\n```\n\nModifies this buffer's byte order, either `Buffer::BIG_ENDIAN` or `Buffer::LITTLE_ENDIAN`\n```php\n$buffer-\u003esetOrder(\\Nelexa\\Buffer::LITTLE_ENDIAN);\n```\n\nGet buffer's byte order\n```php\n$byteOrder = $buffer-\u003eorder();\n```\n\nGet buffer size.\n```php\n$size = $buffer-\u003esize();\n```\n\nSet buffer position.\n```php\n$buffer-\u003esetPosition($position);\n```\n\nGet buffer position.\n```php\n$position = $buffer-\u003eposition();\n```\n\nSkip bytes.\n```php\n$buffer-\u003eskip($count);\n\n// example\n$buffer-\u003einsertString('Test value');\nassert($buffer-\u003eposition() === 10);\n$buffer-\u003eskip(-7);\nassert($buffer-\u003eposition() === 3);\n$buffer-\u003eskip(2);\nassert($buffer-\u003eposition() === 5);\n```\n\nSkip primitive type size.\n```php\n$buffer-\u003eskipByte(); // skip 1 byte\n$buffer-\u003eskipShort(); // skip 2 bytes\n$buffer-\u003eskipInt(); // skip 4 bytes\n$buffer-\u003eskipLong(); // skip 8 bytes\n$buffer-\u003eskipFloat(); // skip 4 bytes\n$buffer-\u003eskipDouble(); // skip 8 bytes\n```\n\nRewinds this buffer. The position is set to zero.\n```php\n$buffer-\u003erewind();\n\n// example\n$buffer-\u003einsertString('Test value');\nassert($buffer-\u003eposition() === 10);\n$buffer-\u003erewind();\nassert($buffer-\u003eposition() === 0);\nassert($buffer-\u003esize() === 10);\n```\n\nFlips this buffer. The limit is set to the current position and then the position is set to zero.\n```php\n$buffer-\u003eflip();\n\n// example\n$buffer-\u003einsertString('Test value');\nassert($buffer-\u003eposition() === 10);\n$buffer-\u003esetPosition(5);\n$buffer-\u003eflip();\nassert($buffer-\u003eposition() === 0);\nassert($buffer-\u003esize() === 5);\n```\n\nReturns the number of elements between the current position and the limit.\n```php\n$remaining = $buffer-\u003eremaining();\n\n// example\n$buffer-\u003einsertString('Test value');\nassert($buffer-\u003eposition() === 10);\n$buffer-\u003esetPosition(7);\nassert($buffer-\u003eremaining() === 10 - 7);\n```\n\nTells 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\n```php\n$boolValue = $buffer-\u003ehasRemaining();\n\n// example\n$buffer-\u003einsertString('Test value');\nassert($buffer-\u003eposition() === 10);\nassert($buffer-\u003ehasRemaining() === false);\n$buffer-\u003esetPosition(9);\nassert($buffer-\u003ehasRemaining() === true);\n```\n\nClose buffer and release resources\n```php\n$buffer-\u003eclose();\n```\n\n### Read buffer\n\nRead the entire contents of the buffer into a string without changing the position of the buffer.\n```php\n$allBufferContent = $buffer-\u003etoString();\n\n// example\n$buffer-\u003einsertString('Test value');\nassert($buffer-\u003eposition() === 10);\n$buffer-\u003esetPosition(4);\n$allBufferContent = $buffer-\u003etoString();\nassert($buffer-\u003eposition() === 4);\nassert($allBufferContent === 'Test value');\n```\n\nReads the string at this buffer's current position, and then increments the position.\n```php\n$content = $buffer-\u003eget($length);\n\n// example\n$buffer-\u003einsertString('Test value');\nassert($buffer-\u003eposition() === 10);\n$buffer-\u003esetPosition(3);\n$content = $buffer-\u003eget(5);\nassert($buffer-\u003eposition() === 8);\nassert($content === 't val');\n```\n##### Read literal types\nMethod                            | Type                    | Values\n--------------------------------- | ----------------------- | -----------------\n`$buffer-\u003egetBoolean`             | boolean                 | `true` or `false`\n`$buffer-\u003egetByte()`              | byte                    | -128 ... 127\n`$buffer-\u003egetUnsignedByte()`      | unsigned byte (ubyte)   | 0 ... 255\n`$buffer-\u003egetShort()`             | short (2 bytes)         | -32768 ... 32767\n`$buffer-\u003egetUnsignedShort()`     | unsigned short (ushort) | 0 ... 65535\n`$buffer-\u003egetInt()`               | int (4 bytes)           | -2147483648 ... 2147483647\n`$buffer-\u003egetUnsignedInt()`       | unsigned int (uint)     | 0 ... 4294967296\n`$buffer-\u003egetLong()`              | long (8 bytes)          | -9223372036854775808 ... 9223372036854775807\n`$buffer-\u003egetFloat()`             | float (4 bytes)         | single-precision 32-bit IEEE 754 floating point number\n`$buffer-\u003egetDouble()`            | double (5 bytes)        | double-precision 64-bit IEEE 754 floating point number\n`$buffer-\u003egetArrayBytes($length)` | byte[]                  | `array`\n`$buffer-\u003egetString($length)`     | string (length bytes)   | `string`\n`$buffer-\u003egetUTF()`               | string                  | `string`\n`$buffer-\u003egetUTF16($length)`      | string (length * 2)     | `string`\n\n\n### Write to buffer\n\n#### Insert bytes to buffer\nInsert string (byte[]) or Buffer to buffer.\n```php\n$buffer-\u003einsert('content');\n// or\n$buffer-\u003einsert(new StringBuffer('Other buffer'));\n\n// example\nassert($buffer-\u003eposition() === 0);\nassert($buffer-\u003esize() === 0);\n$buffer-\u003einsert('Test value');\nassert($buffer-\u003eposition() === 10);\nassert($buffer-\u003esize() === 10);\n$buffer-\u003esetPosition(4);\n$buffer-\u003einsert('ed');\nassert($buffer-\u003eposition() === 6);\nassert($buffer-\u003esize() === 12);\nassert($buffer-\u003etoString() === 'Tested value');\n```\n##### Insert primitive types\nInsert boolean value `false` or `true`. Change size and position by +1.\n```php\n$buffer-\u003einsertBoolean($boolValue);\n```\nInsert byte (-128 \u003e= byte \u003c= 127). Change size and position by +1.\n```php\n$buffer-\u003einsertByte($byteValue);\n```\nInsert short value (-32768 \u003e= short \u003c= 32767). Change size and position by +2.\n```php\n$buffer-\u003einsertShort($shortValue);\n```\nInsert integer value (-2147483648 \u003e= int \u003c= 2147483647). Change size and position by +4.\n```php\n$buffer-\u003einsertInt($intValue);\n```\nInsert long value (-9223372036854775808 \u003e= long \u003c= 9223372036854775807). Change position +8.\n```php\n$buffer-\u003einsertLong($longValue);\n```\nInsert float value (single-precision 32-bit IEEE 754 floating point number). Change position +4.\n```php\n$buffer-\u003einsertFloat($floatValue);\n```\nInsert double value (double-precision 64-bit IEEE 754 floating point number). Change position +8.\n```php\n$buffer-\u003einsertDouble($doubleValue);\n```\nInsert array bytes. Change size and position by +(size array).\n```php\n$buffer-\u003einsertArrayBytes($bytes);\n```\nInsert string value. Change size and position by +(length string).\n```php\n$buffer-\u003einsertString($string);\n```\nInsert UTF-8 string with encoding first two bytes as length string. Change size and position by +(2 + length string).\n\nAnalog java [java.io.DataOutputStream#writeUTF(String str)](https://docs.oracle.com/javase/8/docs/api/java/io/DataOutputStream.html#writeUTF-java.lang.String-)\n```php\n$buffer-\u003einsertUTF($string);\n```\nInsert string with UTF-16 encoding. Change size and position by +(2 * length string).\n```php\n$buffer-\u003einsertUTF16($string);\n```\n#### Put bytes to buffer\nPut string (byte[]) or Buffer to buffer and overwrite old value.\n```php\n$buffer-\u003eput('content');\n// or\n$buffer-\u003eput(new StringBuffer('Other buffer'));\n\n// example\nassert($buffer-\u003eposition() === 0);\nassert($buffer-\u003esize() === 0);\n$buffer-\u003einsert('Test value');\nassert($buffer-\u003eposition() === 10);\nassert($buffer-\u003esize() === 10);\n$buffer-\u003esetPosition(4);\n$buffer-\u003eput('ed');\nassert($buffer-\u003eposition() === 6);\nassert($buffer-\u003esize() === 10);\nassert($buffer-\u003etoString() === 'Testedalue');\n```\n##### Put primitive types\nPut boolean value `false` or `true`. Change position by +1.\n```php\n$buffer-\u003eputBoolean($boolValue);\n```\nPut byte (-128 \u003e= byte \u003c= 127). Change position by +1.\n```php\n$buffer-\u003eputByte($byteValue);\n```\nPut short value (-32768 \u003e= short \u003c= 32767). Change position by +2.\n```php\n$buffer-\u003eputShort($shortValue);\n```\nPut integer value (-2147483648 \u003e= int \u003c= 2147483647). Change position by +4.\n```php\n$buffer-\u003eputInt($intValue);\n```\nPut long value (-9223372036854775808 \u003e= long \u003c= 9223372036854775807). Change position by +8.\n```php\n$buffer-\u003eputLong($longValue);\n```\nPut float value (single-precision 32-bit IEEE 754 floating point number). Change position +4.\n```php\n$buffer-\u003eputFloat($floatValue);\n```\nPut double value (double-precision 64-bit IEEE 754 floating point number). Change position +8.\n```php\n$buffer-\u003eputDouble($doubleValue);\n```\nPut array bytes. Change position by +(size array).\n```php\n$buffer-\u003eputArrayBytes($bytes);\n```\nInsert string value. Change position by +(length string).\n```php\n$buffer-\u003eputString($string);\n```\nPut UTF-8 string with encoding first two bytes as length string. Change position by +(2 + length string).\n\nAnalog java [java.io.DataOutputStream#writeUTF(String str)](https://docs.oracle.com/javase/8/docs/api/java/io/DataOutputStream.html#writeUTF-java.lang.String-)\n```php\n$buffer-\u003eputtUTF($string);\n```\nPut string with UTF-16 encoding. Change position by +(2 * length string).\n```php\n$buffer-\u003eputUTF16($string);\n```\n#### Replace bytes by buffer\nReplace following a certain number of bytes by string or another Buffer.\n```php\n$buffer-\u003ereplace('content', $length);\n// or\n$buffer-\u003einsert(new StringBuffer('Other buffer'), $length);\n\n// example\nassert($buffer-\u003eposition() === 0);\nassert($buffer-\u003esize() === 0);\n$buffer-\u003einsert('Test value');\nassert($buffer-\u003eposition() === 10);\nassert($buffer-\u003esize() === 10);\n$buffer-\u003esetPosition(4);\n$buffer-\u003ereplace('ed', 4); // remove 4 next bytes and insert 2 bytes\nassert($buffer-\u003eposition() === 6);\nassert($buffer-\u003esize() === 8);\nassert($buffer-\u003etoString() === 'Testedlue');\n```\n##### Replace by primitive types\nReplace by boolean value `false` or `true`. Change size by (-$length + 1) and position +1.\n```php\n$buffer-\u003ereplaceBoolean($boolValue, $length);\n```\nReplace by byte (-128 \u003e= byte \u003c= 127). Change size by (-$length + 1) and position +1.\n```php\n$buffer-\u003ereplaceByte($byteValue, $length);\n```\nReplace by short value (-32768 \u003e= short \u003c= 32767). Change size by (-$length + 2) and position +2.\n```php\n$buffer-\u003ereplaceShort($shortValue, $length);\n```\nReplace by integer value (-2147483648 \u003e= int \u003c= 2147483647). Change size by (-$length + 4) and position +4.\n```php\n$buffer-\u003ereplaceInt($intValue, $length);\n```\nReplace by long value (-9223372036854775808 \u003e= long \u003c= 9223372036854775807). Change size by (-$length + 8) and position +8.\n```php\n$buffer-\u003ereplaceLong($longValue, $length);\n```\nReplace by float value (single-precision 32-bit IEEE 754 floating point number). Change size by (-$length + 4) and position +4.\n```php\n$buffer-\u003ereplaceFloat($floatValue, $length);\n```\nReplace by double value (double-precision 64-bit IEEE 754 floating point number). Change size by (-$length + 8) and position +8.\n```php\n$buffer-\u003ereplaceDouble($doubleValue, $length);\n```\nReplace by array bytes. Change size by (-$length + size array) and position +(size array).\n```php\n$buffer-\u003ereplaceArrayBytes($bytes, $length);\n```\nReplace by string value. Change size by (-$length + length string) and position +(length string).\n```php\n$buffer-\u003ereplaceString($string, $length);\n```\nReplace by UTF-8 string with encoding first two bytes as length string. Change size by (-$length + 2 + length string) and position +(2 + length string).\n\nAnalog java [java.io.DataOutputStream#writeUTF(String str)](https://docs.oracle.com/javase/8/docs/api/java/io/DataOutputStream.html#writeUTF-java.lang.String-)\n```php\n$buffer-\u003ereplaceUTF($string, $length);\n```\nReplace by string with UTF-16 encoding. Change size by (-$length + 2 * length string) and position +(2 * length string).\n```php\n$buffer-\u003ereplaceUTF16($string, $length);\n```\n\n### Remove bytes by buffer\nRemove a certain number of bytes. Change size by -$length.\n```php\n$buffer-\u003eremove($length);\n\n// example\nassert($buffer-\u003eposition() === 0);\nassert($buffer-\u003esize() === 0);\n$buffer-\u003einsert('Test value');\nassert($buffer-\u003eposition() === 10);\nassert($buffer-\u003esize() === 10);\n$buffer-\u003esetPosition(4);\n$buffer-\u003eremove(3); // remove 3 next bytes\nassert($buffer-\u003eposition() === 4);\nassert($buffer-\u003esize() === 7);\nassert($buffer-\u003etoString() === 'Testlue');\n```\nRemove all bytes. Truncate buffer.\n```php\n$buffer-\u003etruncate($size = 0);\n\n// example\nassert($buffer-\u003eposition() === 0);\nassert($buffer-\u003esize() === 0);\n$buffer-\u003einsert('Test value');\nassert($buffer-\u003eposition() === 10);\nassert($buffer-\u003esize() === 10);\n$buffer-\u003etruncate(0);\nassert($buffer-\u003eposition() === 0);\nassert($buffer-\u003esize() === 0);\n```\n\n### Fluent interface\n```php\n// example\n($buffer = new StringBuffer())\n       -\u003einsertByte(1)\n       -\u003einsertBoolean(true)\n       -\u003einsertShort(5551)\n       -\u003eskip(-2)\n       -\u003einsertUTF(\"Hello, World\")\n       -\u003etruncate()\n       -\u003einsertString(str_rot13('Hello World'))\n       -\u003esetPosition(7)\n       -\u003eflip();\n        \nassert($this-\u003ebuffer-\u003esize() === 7);\nassert($this-\u003ebuffer-\u003eposition() === 0);\nassert($this-\u003ebuffer-\u003etoString() === str_rot13('Hello W'));\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNe-Lexa%2Fphp-byte-buffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNe-Lexa%2Fphp-byte-buffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNe-Lexa%2Fphp-byte-buffer/lists"}