<?php
+include_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
/**
* Copyright (c) 2006- Facebook
}
}
+/**
+ * Accelerated binary protocol: used in conjunction with the thrift_protocol
+ * extension for faster deserialization
+ */
+class TBinaryProtocolAccelerated extends TBinaryProtocol {
+ public function __construct($trans, $strictRead=false, $strictWrite=true) {
+ // If the transport doesn't implement putBack, wrap it in a
+ // TBufferedTransport (which does)
+ if (!method_exists($trans, 'putBack')) {
+ $trans = new TBufferedTransport($trans);
+ }
+ parent::__construct($trans, $strictRead, $strictWrite);
+ }
+}
+
?>
$this->transport_->close();
}
- public function readAll($len) {
- return $this->transport_->readAll($len);
+ public function putBack($data) {
+ if (empty($this->rBuf_)) {
+ $this->rBuf_ = $data;
+ }
+ else {
+ $this->rBuf_ = ($data . $this->rBuf_);
+ }
}
public function read($len) {
- // Methinks PHP is already buffering these for us
- return $this->transport_->read($len);
+ if (empty($this->rBuf_)){
+ $this->rBuf_ = $this->transport_->read($this->rBufSize_);
+ }
- if (strlen($this->rBuf_) >= $len) {
- $ret = substr($this->rBuf_, 0, $len);
- $this->rBuf_ = substr($this->rBuf_, $len);
+ if (strlen($this->rBuf_) <= $len) {
+ $ret = $this->rBuf_;
+ $this->rBuf_ = '';
return $ret;
}
- $this->rBuf_ .= $this->transport_->read($this->rBufSize_);
- $give = min(strlen($this->rBuf_), $len);
- $ret = substr($this->rBuf_, 0, $give);
- $this->rBuf_ = substr($this->rBuf_, $give);
+ $ret = substr($this->rBuf_, 0, $len);
+ $this->rBuf_ = substr($this->rBuf_, $len);
return $ret;
}
$this->transport_->write($this->wBuf_);
$this->wBuf_ = '';
}
+ $this->transport_->flush();
}
}
}
// Just return full buff
- if ($len > strlen($this->rBuf_)) {
+ if ($len >= strlen($this->rBuf_)) {
$out = $this->rBuf_;
$this->rBuf_ = null;
return $out;
return $out;
}
+ /**
+ * Put previously read data back into the buffer
+ *
+ * @param string $data data to return
+ */
+ public function putBack($data) {
+ if (empty($this->rBuf_)) {
+ $this->rBuf_ = $data;
+ }
+ else {
+ $this->rBuf_ = ($data . $this->rBuf_);
+ }
+ }
+
/**
* Reads a chunk of data into the internal read buffer.
*/
$this->wBuf_ = '';
}
-}
\ No newline at end of file
+}