PHP code example of nelexa / buffer
1. Go to this page and download the library: Download nelexa/buffer library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nelexa / buffer example snippets
$buffer = new \Nelexa\StringBuffer();
// or
$buffer = new \Nelexa\StringBuffer($text);
$buffer = new \Nelexa\FileBuffer($filename);
$buffer = new \Nelexa\MemoryReourceBuffer();
// or
$buffer = new \Nelexa\MemoryReourceBuffer($text);
$fp = fopen('php://temp', 'w+b');
// or
$buffer = new \Nelexa\ResourceBuffer($fp);
$buffer->setReadOnly(true);
$boolValue = $buffer->isReadOnly();
$buffer->setOrder(\Nelexa\Buffer::LITTLE_ENDIAN);
$byteOrder = $buffer->order();
$size = $buffer->size();
$buffer->setPosition($position);
$position = $buffer->position();
$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);
$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
$buffer->rewind();
// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
$buffer->rewind();
assert($buffer->position() === 0);
assert($buffer->size() === 10);
$buffer->flip();
// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
$buffer->setPosition(5);
$buffer->flip();
assert($buffer->position() === 0);
assert($buffer->size() === 5);
$remaining = $buffer->remaining();
// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
$buffer->setPosition(7);
assert($buffer->remaining() === 10 - 7);
$boolValue = $buffer->hasRemaining();
// example
$buffer->insertString('Test value');
assert($buffer->position() === 10);
assert($buffer->hasRemaining() === false);
$buffer->setPosition(9);
assert($buffer->hasRemaining() === true);
$buffer->close();
$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');
$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');
$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');
$buffer->insertBoolean($boolValue);
$buffer->insertByte($byteValue);
$buffer->insertShort($shortValue);
$buffer->insertInt($intValue);
$buffer->insertLong($longValue);
$buffer->insertFloat($floatValue);
$buffer->insertDouble($doubleValue);
$buffer->insertArrayBytes($bytes);
$buffer->insertString($string);
$buffer->insertUTF($string);
$buffer->insertUTF16($string);
$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');
$buffer->putBoolean($boolValue);
$buffer->putByte($byteValue);
$buffer->putShort($shortValue);
$buffer->putInt($intValue);
$buffer->putLong($longValue);
$buffer->putFloat($floatValue);
$buffer->putDouble($doubleValue);
$buffer->putArrayBytes($bytes);
$buffer->putString($string);
$buffer->puttUTF($string);
$buffer->putUTF16($string);
$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');
$buffer->replaceBoolean($boolValue, $length);
$buffer->replaceByte($byteValue, $length);
$buffer->replaceShort($shortValue, $length);
$buffer->replaceInt($intValue, $length);
$buffer->replaceLong($longValue, $length);
$buffer->replaceFloat($floatValue, $length);
$buffer->replaceDouble($doubleValue, $length);
$buffer->replaceArrayBytes($bytes, $length);
$buffer->replaceString($string, $length);
$buffer->replaceUTF($string, $length);
$buffer->replaceUTF16($string, $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');
$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);
// 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'));