r1281@opsdev009 (orig r69409): mcslee | 2007-11-13 02:19:08 -0800
[amiethrift.git] / lib / php / src / transport / TBufferedTransport.php
blob8f410dfc39f05358dd1fc07c8dbe156c66fd6702
1 <?php
3 /**
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>
14 /**
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 {
24 /**
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;
33 /**
34 * The underlying transport
36 * @var TTransport
38 protected $transport_ = null;
40 /**
41 * The receive buffer size
43 * @var int
45 protected $rBufSize_ = 512;
47 /**
48 * The write buffer size
50 * @var int
52 protected $wBufSize_ = 512;
54 /**
55 * The write buffer.
57 * @var string
59 protected $wBuf_ = '';
61 /**
62 * The read buffer.
64 * @var string
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) {
82 $this->rBuf_ = $data;
83 } else {
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) {
94 $ret = $this->rBuf_;
95 $this->rBuf_ = '';
96 return $ret;
99 $ret = substr($this->rBuf_, 0, $len);
100 $this->rBuf_ = substr($this->rBuf_, $len);
101 return $ret;
104 public function write($buf) {
105 $this->wBuf_ .= $buf;
106 if (strlen($this->wBuf_) >= $this->wBufSize_) {
107 $this->transport_->write($this->wBuf_);
108 $this->wBuf_ = '';
112 public function flush() {
113 if (strlen($this->wBuf_) > 0) {
114 $this->transport_->write($this->wBuf_);
115 $this->wBuf_ = '';
117 $this->transport_->flush();