4 * Copyright (c) 2006- Facebook
5 * Distributed under the Thrift Software License
7 * See accompanying file LICENSE or visit the Thrift site at:
8 * http://developers.facebook.com/thrift/
10 * @package thrift.transport
11 * @author Mark Slee <mcslee@facebook.com>
15 * Buffered transport. Stores data to an internal buffer that it doesn't
16 * actually write out until flush is called. For reading, we do a greedy
17 * read and then serve data out of the internal buffer.
19 * @package thrift.transport
20 * @author Mark Slee <mcslee@facebook.com>
22 class TBufferedTransport
extends TTransport
{
25 * Constructor. Creates a buffered transport around an underlying transport
27 public function __construct($transport=null, $rBufSize=512, $wBufSize=512) {
28 $this->transport_
= $transport;
29 $this->rBufSize_
= $rBufSize;
30 $this->wBufSize_
= $wBufSize;
34 * The underlying transport
38 protected $transport_ = null;
41 * The receive buffer size
45 protected $rBufSize_ = 512;
48 * The write buffer size
52 protected $wBufSize_ = 512;
59 protected $wBuf_ = '';
66 protected $rBuf_ = '';
68 public function isOpen() {
69 return $this->transport_
->isOpen();
72 public function open() {
73 $this->transport_
->open();
76 public function close() {
77 $this->transport_
->close();
80 public function putBack($data) {
81 if (strlen($this->rBuf_
) === 0) {
84 $this->rBuf_
= ($data . $this->rBuf_
);
88 public function read($len) {
89 if (strlen($this->rBuf_
) === 0) {
90 $this->rBuf_
= $this->transport_
->read($this->rBufSize_
);
93 if (strlen($this->rBuf_
) <= $len) {
99 $ret = substr($this->rBuf_
, 0, $len);
100 $this->rBuf_
= substr($this->rBuf_
, $len);
104 public function write($buf) {
105 $this->wBuf_
.= $buf;
106 if (strlen($this->wBuf_
) >= $this->wBufSize_
) {
107 $this->transport_
->write($this->wBuf_
);
112 public function flush() {
113 if (strlen($this->wBuf_
) > 0) {
114 $this->transport_
->write($this->wBuf_
);
117 $this->transport_
->flush();