GuzzleHttp\Psr7 stream decoder extension for base64 streams.
Note that it's expected the underlying stream will only contain valid base64 characters (normally the stream should be wrapped in a PregReplaceFilterStream to filter out non-base64 characters for reading).
$f = fopen(...);
$stream = new Base64Stream(new PregReplaceFilterStream(
Psr7\Utils::streamFor($f), '/[^a-zA-Z0-9\/\+=]/', ''
));
//...
For writing, a ChunkSplitStream could come in handy so the output is split into lines:
$f = fopen(...);
$stream = new Base64Stream(new ChunkSplitStream(new PregReplaceFilterStream(
Psr7\Utils::streamFor($f), '/[^a-zA-Z0-9\/\+=]/', ''
)));
//...
- author Zaahid Bateson
- Implements
Psr\Http\Message\StreamInterface Stringable - Traits
Methods |
public __call(string $method, array $args) Allow decorators to implement custom methods
|
public __construct(Psr\Http\Message\StreamInterface $stream) |
public __get(string $name) Magic method used to create a new stream if streams are not added in the constructor of a decorator (e.g., LazyOpenStream).
|
public __toString() : string |
public close() : void
|
public detach()
|
public eof() : bool Returns true if the end of stream has been reached. |
public getContents() : string |
public getMetadata( $key = NULL)
|
public getSize() : ?int Returns null, getSize isn't supported
|
public isReadable() : bool |
public isSeekable() : bool Overridden to return false |
public isWritable() : bool |
public read( $length) : string Attempts to read $length bytes after decoding them, and returns them. Note that reading and writing to the same stream may result in wrongly encoded data and is not supported.
|
public rewind() : void |
public seek( $offset, $whence = 0ZBateson\StreamDecorators\SEEK_SET) : void Not implemented (yet). Seek position can be calculated.
|
public tell() : int Returns the current position of the file read/write pointer |
public write( $string) : int Writes the passed string to the underlying stream after encoding it to base64. Base64Stream::close or detach must be called. Failing to do so may result in 1-2 bytes missing from the end of the stream if there's a remainder. Note that the default Stream destructor calls close as well. Note that reading and writing to the same stream may result in wrongly encoded data and is not supported.
|
Methods |
protected createStream() : Psr\Http\Message\StreamInterface Implement in subclasses to dynamically create streams when requested.
|
Properties |
private GuzzleHttp
|
private int $position = 0
|
private string $remainder = ''
|
private Psr\Http\Message\StreamInterface $stream
|
Methods |
private beforeClose() : void Writes out any remaining bytes at the end of the stream and closes. |
private fillBuffer(int $length) : void Fills the internal byte buffer after reading and decoding data from the underlying stream. Note that it's expected the underlying stream will only contain valid base64 characters (normally the stream should be wrapped in a PregReplaceFilterStream to filter out non-base64 characters). |