From: lklots Date: Fri, 2 Nov 2007 23:01:27 +0000 (+0000) Subject: [TMemoryBuffer: an in-memory buffer acting like a transport] X-Git-Tag: 0.2.0~1152 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=ffc21fb792b152ed6b45c4e83b4c5fd295465007;p=common%2Fthrift.git [TMemoryBuffer: an in-memory buffer acting like a transport] Summary: This is the php equivalent of the cpp TMemoryBuffer. It is simply a transport that stores read and fetches write requests to and from a string. Trac Bug: # Blame Rev: Reviewed By: dreiss Test Plan: Tested using thrift de/serialization. Wrote thrift objeccts to the buffer and then read them later to resconstruct them. Tested exceptional cases. Revert Plan: ok Database Impact: none Memcache Impact: none Other Notes: EImportant: - begin *PUBLIC* platform impact section - Bugzilla: # - end platform impact - git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665320 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/php/src/transport/TMemoryBuffer.php b/lib/php/src/transport/TMemoryBuffer.php new file mode 100644 index 00000000..f62bb572 --- /dev/null +++ b/lib/php/src/transport/TMemoryBuffer.php @@ -0,0 +1,75 @@ + + */ + +/** + * A memory buffer is a tranpsort that simply reads from and writes to an + * in-memory string buffer. Anytime you call write on it, the data is simply + * placed into a buffer, and anytime you call read, data is read from that + * buffer. + * + * @package thrift.transport + * @author Levy Klots + */ +class TMemoryBuffer extends TTransport { + + /** + * Constructor. Optionally pass an initial value + * for the buffer. + */ + public function __construct($buf = '') { + $this->buf_ = $buf; + } + + protected $buf_ = ''; + + public function isOpen() { + return true; + } + + public function open() {} + + public function close() {} + + public function write($buf) { + $this->buf_ .= $buf; + } + + public function read($len) { + if (empty($this->buf_)) { + throw new TTransportException('TMemoryBuffer: Could not read ' . + $len . ' bytes from buffer.', + TTransportException::UNKNOWN); + } + + if (strlen($this->buf_) <= $len) { + $ret = $this->buf_; + $this->buf_ = ''; + return $ret; + } + + $ret = substr($this->buf_, 0, $len); + $this->buf_ = substr($this->buf_, $len); + + return $ret; + } + + function getBuffer() { + return $this->buf_; + } + + public function available() { + return strlen($this->buf_); + } +} + +?>