|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00031 class Zend_Amf_Util_BinaryStream 00032 { 00036 protected $_stream; 00037 00041 protected $_streamLength; 00042 00046 protected $_bigEndian; 00047 00051 protected $_needle; 00052 00063 public function __construct($stream) 00064 { 00065 if (!is_string($stream)) { 00066 require_once 'Zend/Amf/Exception.php'; 00067 throw new Zend_Amf_Exception('Inputdata is not of type String'); 00068 } 00069 00070 $this->_stream = $stream; 00071 $this->_needle = 0; 00072 $this->_streamLength = strlen($stream); 00073 $this->_bigEndian = (pack('l', 1) === "\x00\x00\x00\x01"); 00074 } 00075 00081 public function getStream() 00082 { 00083 return $this->_stream; 00084 } 00085 00094 public function readBytes($length) 00095 { 00096 if (($length + $this->_needle) > $this->_streamLength) { 00097 require_once 'Zend/Amf/Exception.php'; 00098 throw new Zend_Amf_Exception('Buffer underrun at needle position: ' . $this->_needle . ' while requesting length: ' . $length); 00099 } 00100 $bytes = substr($this->_stream, $this->_needle, $length); 00101 $this->_needle+= $length; 00102 return $bytes; 00103 } 00104 00113 public function writeBytes($bytes) 00114 { 00115 $this->_stream.= $bytes; 00116 return $this; 00117 } 00118 00124 public function readByte() 00125 { 00126 if (($this->_needle + 1) > $this->_streamLength) { 00127 require_once 'Zend/Amf/Exception.php'; 00128 throw new Zend_Amf_Exception('Buffer underrun at needle position: ' . $this->_needle . ' while requesting length: ' . $length); 00129 } 00130 00131 return ord($this->_stream{$this->_needle++}); 00132 } 00133 00140 public function writeByte($stream) 00141 { 00142 $this->_stream.= pack('c', $stream); 00143 return $this; 00144 } 00145 00151 public function readInt() 00152 { 00153 return ($this->readByte() << 8) + $this->readByte(); 00154 } 00155 00162 public function writeInt($stream) 00163 { 00164 $this->_stream.= pack('n', $stream); 00165 return $this; 00166 } 00167 00173 public function readUtf() 00174 { 00175 $length = $this->readInt(); 00176 return $this->readBytes($length); 00177 } 00178 00185 public function writeUtf($stream) 00186 { 00187 $this->writeInt(strlen($stream)); 00188 $this->_stream.= $stream; 00189 return $this; 00190 } 00191 00192 00198 public function readLongUtf() 00199 { 00200 $length = $this->readLong(); 00201 return $this->readBytes($length); 00202 } 00203 00210 public function writeLongUtf($stream) 00211 { 00212 $this->writeLong(strlen($stream)); 00213 $this->_stream.= $stream; 00214 } 00215 00221 public function readLong() 00222 { 00223 return ($this->readByte() << 24) + ($this->readByte() << 16) + ($this->readByte() << 8) + $this->readByte(); 00224 } 00225 00232 public function writeLong($stream) 00233 { 00234 $this->_stream.= pack('N', $stream); 00235 return $this; 00236 } 00237 00244 public function readUnsignedShort() 00245 { 00246 $byte1 = $this->readByte(); 00247 $byte2 = $this->readByte(); 00248 return (($byte1 << 8) | $byte2); 00249 } 00250 00256 public function readDouble() 00257 { 00258 $bytes = substr($this->_stream, $this->_needle, 8); 00259 $this->_needle+= 8; 00260 00261 if (!$this->_bigEndian) { 00262 $bytes = strrev($bytes); 00263 } 00264 00265 $double = unpack('dflt', $bytes); 00266 return $double['flt']; 00267 } 00268 00275 public function writeDouble($stream) 00276 { 00277 $stream = pack('d', $stream); 00278 if (!$this->_bigEndian) { 00279 $stream = strrev($stream); 00280 } 00281 $this->_stream.= $stream; 00282 return $this; 00283 } 00284 00285 }