\Drupal\strata\Storage BodyReader

Turns a put body into the pieces one endpoint takes.

Every remote provider faces the same three problems and none of them is vendor-specific: a body arrives as a string or as a stream, a stream may not be able to say how long it is, and a body too large for one request has to be cut into uniform pieces with a short one at the end. The vendor decides what a piece is called and how large it may be; cutting it is arithmetic.

A short read is never silently accepted. A read that fails mid-stream raises naming the key, because a provider that returned what it had would write a truncated object that no reader can tell from a real one.

Summary

Methods
Properties
Constants
sliceString
sliceStream
readAll
readExactly
sizeOf
No public properties found
COPY_CHUNK
No protected methods found
No protected properties found
No protected constants found
No private methods found
No private properties found
No private constants found

Constant

COPY_CHUNK

COPY_CHUNK = 1048576

Bytes read per iteration when a body arrives as a stream.

Methods

sliceString()

sliceString(string  $body, int  $partSize) : \Generator<mixed,void>

Cuts a string into uniform pieces.

Parameters

string $body

The bytes.

int $partSize

Bytes per piece.

Returns

\Generator

Every piece the same size except the last.

sliceStream()

sliceStream(resource  $stream, int  $partSize, string  $first, string  $key) : \Generator<mixed,void>

Cuts a stream into uniform pieces.

Parameters

resource $stream

An open readable stream, positioned after $first.

int $partSize

Bytes per piece.

string $first

The piece already read in order to decide that one request would not carry the body.

string $key

Object key, for error messages.

Throws

\RuntimeException

When the stream fails mid-read.

Returns

\Generator

Every piece the same size except the last. A body ending exactly on a boundary yields no empty final piece.

readAll()

readAll(resource  $stream, string  $key) : string

Reads a stream to the end.

Parameters

resource $stream

An open readable stream.

string $key

Object key, for error messages.

Throws

\RuntimeException

When a read fails.

Returns

string —

Everything from the current position to EOF.

readExactly()

readExactly(resource  $stream, int  $want, string  $key) : string

Reads a fixed number of bytes, or fewer at the end of the stream.

Parameters

resource $stream

An open readable stream.

int $want

Bytes to read.

string $key

Object key, for error messages.

Throws

\RuntimeException

When a read fails.

Returns

string —

Exactly $want bytes, or fewer when the stream ended.

sizeOf()

sizeOf(resource  $stream) : int|null

How many bytes a stream has left, where it can say.

Parameters

resource $stream

An open stream.

Returns

int|null —

Bytes from the current position to the end, or NULL for a pipe or any other stream that cannot report a size.