* @package Mammut\IO */ class Buffer extends \Mammut\StdObject implements iInput, iOutput { const _VERSION_ = '0.5.0.1'; private $pos = 0; private $buffer = null; public function __construct($buffer = null) { if (!is_null($buffer)) $this->setBuffer($buffer); } /** * Put data into the buffer * @param mixed $data */ public function setBuffer($data) { $this->buffer = $data; } public function getBuffer() { return $this->buffer; } public function getIOTypes() { return array(iIO::INPUT,iIO::OUTPUT); } public function isSeekSupported() { return true; } public function create() { if(!is_null($this->buffer)) throw new \BadMethodCallException('already open'); $this->buffer = ''; $this->pos = 0; } public function open() { if(!is_null($this->buffer)) throw new \BadMethodCallException('already open'); $this->buffer = ''; $this->pos = 0; } public function isOpen() { return !is_null($this->buffer); } public function seek($pos) { if(is_null($this->buffer)) throw new \BadMethodCallException('already open'); if($pos < 0 || $pos >= strlen($this->buffer)) throw new \OutOfBoundsException(); $this->pos = $pos; } public function getSize() { return strlen($this->buffer); } public function read($chunksize = 1024) { if(is_null($this->buffer)) throw new \BadMethodCallException('not open'); $result = substr($this->buffer, $this->pos, $chunksize); $this->pos += strlen($result); return $result; } public function write($data) { if(is_null($this->buffer)) throw new \BadMethodCallException('not open'); $this->buffer .= $data; } public function isEOF() { if(is_null($this->buffer)) throw new \BadMethodCallException('not open'); return strlen($this->buffer) - 1 <= $this->pos; } public function close() { $this->pos = 0; $this->buffer = NULL; } }