[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
This commit is contained in:
lklots 2007-11-02 23:01:27 +00:00
parent b000f7433d
commit ffc21fb792

View File

@ -0,0 +1,75 @@
<?php
/**
* Copyright (c) 2006- Facebook
* Distributed under the Thrift Software License
*
* See accompanying file LICENSE or visit the Thrift site at:
* http://developers.facebook.com/thrift/
*
* @package thrift.transport
* @author Levy Klots <lklots@facebook.com>
*/
/**
* 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 <lklots@facebook.com>
*/
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_);
}
}
?>