3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
20 * @package thrift.transport
23 namespace Thrift\Transport
;
25 use Thrift\Exception\TTransportException
;
26 use Thrift\Factory\TStringFuncFactory
;
29 * Buffered transport. Stores data to an internal buffer that it doesn't
30 * actually write out until flush is called. For reading, we do a greedy
31 * read and then serve data out of the internal buffer.
33 * @package thrift.transport
35 class TBufferedTransport
extends TTransport
38 * The underlying transport
42 protected $transport_;
45 * The receive buffer size
49 protected $rBufSize_ = 512;
52 * The write buffer size
56 protected $wBufSize_ = 512;
63 protected $wBuf_ = '';
70 protected $rBuf_ = '';
73 * Constructor. Creates a buffered transport around an underlying transport
75 public function __construct($transport, $rBufSize = 512, $wBufSize = 512)
77 $this->transport_
= $transport;
78 $this->rBufSize_
= $rBufSize;
79 $this->wBufSize_
= $wBufSize;
82 public function isOpen()
84 return $this->transport_
->isOpen();
90 * @throws TTransportException
92 public function open()
94 $this->transport_
->open();
97 public function close()
99 $this->transport_
->close();
102 public function putBack($data)
104 if (TStringFuncFactory
::create()->strlen($this->rBuf_
) === 0) {
105 $this->rBuf_
= $data;
107 $this->rBuf_
= ($data . $this->rBuf_
);
112 * The reason that we customize readAll here is that the majority of PHP
113 * streams are already internally buffered by PHP. The socket stream, for
114 * example, buffers internally and blocks if you call read with $len greater
115 * than the amount of data available, unlike recv() in C.
117 * Therefore, use the readAll method of the wrapped transport inside
118 * the buffered readAll.
120 * @throws TTransportException
122 public function readAll($len)
124 $have = TStringFuncFactory
::create()->strlen($this->rBuf_
);
126 $data = $this->transport_
->readAll($len);
127 } elseif ($have < $len) {
128 $data = $this->rBuf_
;
130 $data .= $this->transport_
->readAll($len - $have);
131 } elseif ($have == $len) {
132 $data = $this->rBuf_
;
134 } elseif ($have > $len) {
135 $data = TStringFuncFactory
::create()->substr($this->rBuf_
, 0, $len);
136 $this->rBuf_
= TStringFuncFactory
::create()->substr($this->rBuf_
, $len);
147 * @throws TTransportException
149 public function read($len)
151 if (TStringFuncFactory
::create()->strlen($this->rBuf_
) === 0) {
152 $this->rBuf_
= $this->transport_
->read($this->rBufSize_
);
155 if (TStringFuncFactory
::create()->strlen($this->rBuf_
) <= $len) {
162 $ret = TStringFuncFactory
::create()->substr($this->rBuf_
, 0, $len);
163 $this->rBuf_
= TStringFuncFactory
::create()->substr($this->rBuf_
, $len);
172 * @throws TTransportException
174 public function write($buf)
176 $this->wBuf_
.= $buf;
177 if (TStringFuncFactory
::create()->strlen($this->wBuf_
) >= $this->wBufSize_
) {
180 // Note that we clear the internal wBuf_ prior to the underlying write
181 // to ensure we're in a sane state (i.e. internal buffer cleaned)
182 // if the underlying write throws up an exception
184 $this->transport_
->write($out);
191 * @throws TTransportException
193 public function flush()
195 if (TStringFuncFactory
::create()->strlen($this->wBuf_
) > 0) {
198 // Note that we clear the internal wBuf_ prior to the underlying write
199 // to ensure we're in a sane state (i.e. internal buffer cleaned)
200 // if the underlying write throws up an exception
202 $this->transport_
->write($out);
204 $this->transport_
->flush();