1: <?php
2:
3: /**
4: * Copyright (c) 2006- Facebook
5: * Distributed under the Thrift Software License
6: *
7: * See accompanying file LICENSE or visit the Thrift site at:
8: * http://developers.facebook.com/thrift/
9: *
10: * @package thrift.transport
11: * @author Levy Klots <lklots@facebook.com>
12: */
13:
14: /**
15: * A memory buffer is a tranpsort that simply reads from and writes to an
16: * in-memory string buffer. Anytime you call write on it, the data is simply
17: * placed into a buffer, and anytime you call read, data is read from that
18: * buffer.
19: *
20: * @package thrift.transport
21: * @author Levy Klots <lklots@facebook.com>
22: */
23: class TMemoryBuffer extends TTransport {
24:
25: /**
26: * Constructor. Optionally pass an initial value
27: * for the buffer.
28: */
29: public function __construct($buf = '') {
30: $this->buf_ = $buf;
31: }
32:
33: protected $buf_ = '';
34:
35: public function isOpen() {
36: return true;
37: }
38:
39: public function open() {}
40:
41: public function close() {}
42:
43: public function write($buf) {
44: $this->buf_ .= $buf;
45: }
46:
47: public function read($len) {
48: if (strlen($this->buf_) === 0) {
49: throw new TTransportException('TMemoryBuffer: Could not read ' .
50: $len . ' bytes from buffer.',
51: TTransportException::UNKNOWN);
52: }
53:
54: if (strlen($this->buf_) <= $len) {
55: $ret = $this->buf_;
56: $this->buf_ = '';
57: return $ret;
58: }
59:
60: $ret = substr($this->buf_, 0, $len);
61: $this->buf_ = substr($this->buf_, $len);
62:
63: return $ret;
64: }
65:
66: function getBuffer() {
67: return $this->buf_;
68: }
69:
70: public function available() {
71: return strlen($this->buf_);
72: }
73: }
74:
75: ?>
76: