- Saved searches
- Use saved searches to filter your results more quickly
- Ne-Lexa/php-byte-buffer
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- Php encode a buffer to text node
- NodeJS Buffer Equivalent In PHP
- Problem with characters encoding text buffer from process
- stream_get_contents
- Parameters
- Return Values
- Changelog
- Examples
- Notes
- See Also
- User Contributed Notes 5 notes
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
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.
Ne-Lexa/php-byte-buffer
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
nelexa/buffer -> Read And Write Binary Data
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.
composer require nelexa/buffer
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);
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);
$buffer->setPosition($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
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
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');
Method | Type | Values |
---|---|---|
$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 |
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 boolean value false or true . Change size and position by +1.
$buffer->insertBoolean($boolValue);
$buffer->insertByte($byteValue);
Php encode a buffer to text node
You need something that converts directly into HTML character references, or a MathML to HTML character reference converter. You should be able to use : http://pt1.php.net/htmlentities You can change to and it will return Unicode Replacement Characters or Hex character references.
NodeJS Buffer Equivalent In PHP
Seems that you are working with base64; in php you are right pack and unpack is your friends.
$ node > Buffer('hello world').toString('base64') aGVsbG8gd29sZA==
$ php -a php > echo base64_encode('hello world'); aGVsbG8gd29ybGQ=
But if you are only looking for the binary:
php > print_r(unpack('H*', 'hello world')); Array ( [1] => 68656c6c6f20776f726c64 )
So in your instance you would first decode the base64 and then unpack it.
php > $raw = base64_decode('GHlDHToiZA1ViUu+W+EXww=='); php > print_r(unpack('H*', $raw)); Array ( [1] => 1879431d3a22640d55894bbe5be117c3 )
I have the same problem, and i found solution using the packet: lcobucci/jwt.
Must to create a buffer by your key in base64, after create will be converting to binary for sign the jwt.
$configuration = Configuration::forSymmetricSigner( // You may use any HMAC variations (256, 384, and 512) new Sha256(), // replace the value below with a key of your own! InMemory::base64Encoded('your-base64-key') // You may also override the JOSE encoder/decoder if needed by providing extra arguments here );
Node.js buffer toString() Method, Node.js buffer toString () Method Buffer Module Example Display the buffer object as a string: var buf = Buffer.from(‘abc’); console.log(buf.toString()); Run example » Definition and Usage The toString () method returns the buffer object according to the specified encoding. Syntax buffer .toString ( encoding, start, end ); Parameter …
Problem with characters encoding text buffer from process
stream_get_contents
Identical to file_get_contents() , except that stream_get_contents() operates on an already open stream resource and returns the remaining contents in a string, up to length bytes and starting at the specified offset .
Parameters
A stream resource (e.g. returned from fopen() )
The maximum bytes to read. Defaults to null (read all the remaining buffer).
Seek to the specified offset before reading. If this number is negative, no seeking will occur and reading will start from the current position.
Return Values
Returns a string or false on failure.
Changelog
Examples
Example #1 stream_get_contents() example
if ( $stream = fopen ( ‘http://www.example.com’ , ‘r’ )) // print all the page starting at the offset 10
echo stream_get_contents ( $stream , — 1 , 10 );
if ( $stream = fopen ( ‘http://www.example.net’ , ‘r’ )) // print the first 5 bytes
echo stream_get_contents ( $stream , 5 );
Notes
Note: This function is binary-safe.
Note:
When specifying a length value other than null , this function will immediately allocate an internal buffer of that size even if the actual contents are significantly shorter.
See Also
- fgets() — Gets line from file pointer
- fread() — Binary-safe file read
- fpassthru() — Output all remaining data on a file pointer
User Contributed Notes 5 notes
It is important to know that stream_get_contents behaves differently with different versions of PHP. Consider the following
$handle = fopen ( ‘file’ , ‘w+’ ); // truncate + attempt to create
fwrite ( $handle , ‘12345’ ); // file position > 0
rewind ( $handle ); // position = 0
$content = stream_get_contents ( $handle ); // file position = 0 in PHP 5.1.6, file position > 0 in PHP 5.2.17!
fwrite ( $handle , ‘6789’ );
fclose ( $handle );
/**
*
* ‘file’ content
*
* PHP 5.1.6:
* 67895
*
* PHP 5.2.17:
* 123456789
*
*/
?>
As a result, stream_get_contents() affects file position in 5.1, and do not affect file position in 5.2 or better.
In that case when stream_get_contents/fread/fgets or other stream reading functions block indefinitely your script because they don’t reached the limit of bytes to read use the socket_get_meta_data function to figure out the number of the bytes to read. It returns an array that contains a key named ‘unread_bytes’ and then pass that number to your favourite stream reading functions second parameter to read from the stream.
Maybe a good workaround to use the stream_select function, and set the socket to non-blocking mode with the use of stream_set_blocking($stream, 0). In this case the socket reading functions work properly.
When omitting the parameter $maxlength, any received bytes are stacked up until the underlying stream is not readable anymore, the the function returns that stack in one piece.
/*
* problem: stream_get_contents blocks / is very slow.
* I have tried
* 1: stream_set_blocking, doesn’t make a difference.
* 2: stream_get_meta_data[‘unread_bytes’] = ITS BUGGED, ALWAYS SAYS 0.
* 3: feof(): ALSO EFFING BLOCKING
* 4: my_stream_get_contents hack. kinda working! 😀
*/
function my_stream_get_contents ($handle, $timeout_seconds = 0.5)
$ret = «»;
// feof ALSO BLOCKS:
// while(!feof($handle))
while (true) $starttime = microtime(true);
$new = stream_get_contents($handle, 1);
$endtime = microtime(true);
if (is_string($new) && strlen($new) >= 1) $ret .= $new;
>
$time_used = $endtime — $starttime;
// var_dump(‘time_used:’,$time_used);
if (($time_used >= $timeout_seconds) || ! is_string($new) ||
(is_string($new) && strlen($new) < 1)) break;
>
>
return $ret;
>
- Stream Functions
- stream_bucket_append
- stream_bucket_make_writeable
- stream_bucket_new
- stream_bucket_prepend
- stream_context_create
- stream_context_get_default
- stream_context_get_options
- stream_context_get_params
- stream_context_set_default
- stream_context_set_option
- stream_context_set_params
- stream_copy_to_stream
- stream_filter_append
- stream_filter_prepend
- stream_filter_register
- stream_filter_remove
- stream_get_contents
- stream_get_filters
- stream_get_line
- stream_get_meta_data
- stream_get_transports
- stream_get_wrappers
- stream_is_local
- stream_isatty
- stream_notification_callback
- stream_register_wrapper
- stream_resolve_include_path
- stream_select
- stream_set_blocking
- stream_set_chunk_size
- stream_set_read_buffer
- stream_set_timeout
- stream_set_write_buffer
- stream_socket_accept
- stream_socket_client
- stream_socket_enable_crypto
- stream_socket_get_name
- stream_socket_pair
- stream_socket_recvfrom
- stream_socket_sendto
- stream_socket_server
- stream_socket_shutdown
- stream_supports_lock
- stream_wrapper_register
- stream_wrapper_restore
- stream_wrapper_unregister