Download the PHP package sorelvi/stream-reader without Composer

On this page you can find all versions of the php package sorelvi/stream-reader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package stream-reader

Sorelvi Stream Reader

PHP Version License CI Packagist

Industrial-grade, memory-efficient streaming reader for PHP.

Reading gigabytes of text data (logs, SQL dumps, CSVs) in PHP can be tricky. Standard fread breaks multi-byte characters (UTF-8, UTF-16) if a chunk boundary splits a character in half. Sorelvi\StreamReader solves this problem by strictly analyzing byte sequences and ensuring every yielded chunk contains only valid, complete characters.

Key Features

Requirements

Installation

Quick Start

Reading a Large File

The easiest way to read a file is using the static factory method with a Preset.

Reading from String

Per-call Chunk Size

The readChunk() method accepts an optional chunk size that overrides the instance default for that single call:

Integrity vs. Validation

Important: This library focuses on Stream Integrity, not Content Validation.

Garbage In → Complete Garbage Out (not Half-Garbage).

Advanced Usage

Dependency Injection (Manual Construction)

For strict control over the estimator or context, you can instantiate the class manually.

Resume Reading (Context Management)

The HandleContext tracks the number of bytes read. You can persist this state to resume reading later (e.g., between HTTP requests or background jobs).

HandleContext API

Method Description
getTotalReadBytes(): int Returns total bytes consumed so far.
addTotalReadBytes(int): void Increments the byte counter.
setTotalReadBytes(int): void Overrides the byte counter.
resetTotalReadBytes(): void Resets counter to zero.
toArray(): array Serializes context to a plain array (for persistence).
fromArray(array): self Restores context from a previously serialized array.
setParam(string $part, string $key, scalar\|null): self Stores arbitrary state (used internally by estimators).
getParam(string $part, string $key, $default): scalar\|null Retrieves stored state.
hasParam(string $part, string $key): bool Checks if a parameter is stored.

Supporting UTF-16

The reader intelligently handles UTF-16 endianness:

  1. Uses the explicitly configured endianness if Preset::UTF16LE or Preset::UTF16BE is specified.
  2. For Preset::UTF16 (auto mode), detects endianness from the Byte Order Mark (BOM): 0xFF 0xFE → LE, anything else → BE (per RFC 2781).
  3. Correctly handles Surrogate Pairs (4-byte characters in UTF-16) to ensure they are not split across chunks.
  4. The detected endianness is stored in HandleContext and reused on subsequent calls, ensuring consistency across resumed sessions.

Network and Pipe Streams

When working with non-blocking or slow network streams, fread may return an empty string without reaching EOF. The Stream class handles this transparently with configurable retry logic.

If the stream returns empty data more times than maxEmptyAttempts, a TooManyEmptyAttempts exception is thrown.

Custom Estimator

You can implement EstimatorInterface to support any custom encoding or fixed-width binary protocol.

Custom Stream

You can implement StreamInterface to wrap any data source (e.g., in-memory buffers, S3 streams, database BLOBs).

Supported Presets

Use the Sorelvi\StreamReader\Enum\Preset enum to select your encoding:

Preset Description
Preset::UTF8 Variable-width (1–4 bytes), bitwise scan.
Preset::UTF16 Variable-width (2 or 4 bytes), BOM auto-detect.
Preset::UTF16LE UTF-16 Little Endian, explicit.
Preset::UTF16BE UTF-16 Big Endian, explicit.
Preset::UTF32 Fixed 4-byte width. Equivalent to Preset::BYTE4.
Preset::BYTE1 Fixed 1-byte (ASCII, Windows-1251, ISO-8859-*, KOI8-R, etc.).
Preset::BYTE2 Fixed 2-byte width.
Preset::BYTE3 Fixed 3-byte width.
Preset::BYTE4 Fixed 4-byte width.
Preset::BYTE5Preset::BYTE10 Fixed 5–10 byte widths for custom binary protocols.

Exception Reference

All library exceptions extend Sorelvi\StreamReader\Exception\StreamReaderException.

Exception Code Thrown When
FileNotAccessible 303–305 File does not exist, is a directory, or is not readable.
IsNotStream Constructor receives a non-resource argument.
CanNotCreateStream fopen() fails internally.
CanNotReadZeroBytes Stream::read() called with $length < 1.
CanNotReadZeroChunk Reader::readChunk() called with $chunkLength < 1.
ChunkLengthMustBePositive Reader::setChunkLength() called with value < 1.
ErrorReadingFromStream 301, 302 Stream became invalid or fread() returned false.
TooManyEmptyAttempts Empty-read retry limit exceeded (network/pipe streams).
StreamDamaged 101–107 Invalid or incomplete byte sequence detected in stream.
CanNotRestoreReadingStream 306 Context byte offset cannot be seeked to in the stream.
MaxAddReadMustBeZeroOrPositive Custom estimator returns a negative value from getMaxAddReadBytes().

ErrorCode Enum

Sorelvi\StreamReader\Exception\ErrorCode provides integer codes for programmatic error handling:

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please ensure to run static analysis tools (composer check) before submitting.

See CONTRIBUTING.md for full development workflow.

License

Licensed under the Apache License, Version 2.0.

Copyright 2026 Sorelvi.


All versions of stream-reader with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package sorelvi/stream-reader contains the following files

Loading the files please wait ...