Skip to content
This repository was archived by the owner on Jan 16, 2019. It is now read-only.

Memory stream

mikey179 edited this page Jan 24, 2012 · 4 revisions

Memory streams

Sometimes it is helpful if one can read or write data into memory. For such purposes the net\stubbles\streams\memory\MemoryInputStream and net\stubbles\streams\memory\MemoryOutputStream exist. While the memory input stream class expects the content to be read from as string parameter for its constructor, the memory output stream does not expect a value on construction, but offers an additional method getBuffer() which returns all data written to this stream so far.

The memory input stream is a seekable stream.

Memory stream wrapper

The net\stubbles\streams\memory package contains a stream wrapper class which "invents" the memory protocol. If loaded, one can use the normal file functions from PHP, but without files. With the memory stream wrapper you can fool classes relying on the file functions by writing the contents to memory://mydata, now you just to get the class to read the data from this URL. Example:

file_put_contents('memory://mydata', $xmlData);
$xmlUnserializer = new stubXMLUnserializer();
$data = $xmlUnserializer->unserializeFile('memory://mydata');

To be able to read and write from/to such URLs just make sure the net\stubbles\streams\memory\MemoryStreamWrapper is loaded. You do not need to call any methods on this class yourself.

Clone this wiki locally