Copied!

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\/\+=]/', ''
)));
//...
CloneableInstantiable
Methods
public __call(string $method, array $args)
 

Allow decorators to implement custom methods

  • return mixed
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).

  • return \StreamInterface
public __toString() : string
public close() : void
 
  • inheritDoc
public detach()
 
  • inheritDoc
public eof() : bool
 

Returns true if the end of stream has been reached.

public getContents() : string
public getMetadata( $key = NULL)
 
  • return mixed
public getSize() : ?int
 

Returns null, getSize isn't supported

  • return null
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.

  • param int $length
public rewind() : void
public seek( $offset, $whence = 0ZBateson\StreamDecorators\SEEK_SET) : void
 

Not implemented (yet).

Seek position can be calculated.

  • param int $offset
  • param int $whence
  • throws \RuntimeException
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.

  • param string $string
  • return int the number of bytes written
Methods
protected createStream() : Psr\Http\Message\StreamInterface
 

Implement in subclasses to dynamically create streams when requested.

  • throws \BadMethodCallException
Properties
private GuzzleHttp\Psr7\BufferStream $buffer
 
  • var \BufferStream buffered bytes
private int $position = 0
 
  • var int current number of read/written bytes (for tell())
private string $remainder = ''
 
  • var string remainder of write operation if the bytes didn't align to 3 bytes
private Psr\Http\Message\StreamInterface $stream
 
  • var \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).

© 2025 Bruce Wells
Search Namespaces \ Classes
Configuration