Skip to content

Add a Blob class as a bridge between string, bytes and streams#360

Open
thekid wants to merge 9 commits intoxp-framework:mainfrom
thekid:feature/blob-api
Open

Add a Blob class as a bridge between string, bytes and streams#360
thekid wants to merge 9 commits intoxp-framework:mainfrom
thekid:feature/blob-api

Conversation

@thekid
Copy link
Copy Markdown
Member

@thekid thekid commented Mar 29, 2026

This pull request adds an io.Blob class.

Examples

Creating a blob from various inputs:

use io\Blob;
use io\streams\{Streams, MemoryInputStream};
use util\Bytes;

$test= new Blob('Test');
$test= new Blob(new Bytes('Test'));
$test= new Blob(new MemoryInputStream('Test'));
$test= new Blob(['Te', 'st']);

$test->bytes();                    // util.Bytes("Test")
Streams::readAll($test->stream()); // "Test"
(string)$test;                     // "Test"

Iterating slices of a stream:

use io\Blob;
use io\streams\FileInputStream;

// Will echo the file's contents in slices of 8192 bytes
$blob= new Blob(new FileInputStream($argv[1]));
foreach ($blob->slices(size: 8192) as $slice) {
  echo $slice;
}

See also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant