-
Notifications
You must be signed in to change notification settings - Fork 1
Memory stream
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.
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.