HproseIOStream.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**********************************************************\
  3. | |
  4. | hprose |
  5. | |
  6. | Official WebSite: http://www.hprose.com/ |
  7. | http://www.hprose.net/ |
  8. | http://www.hprose.org/ |
  9. | |
  10. \**********************************************************/
  11. /**********************************************************\
  12. * *
  13. * HproseIOStream.php *
  14. * *
  15. * hprose io stream library for php5. *
  16. * *
  17. * LastModified: Nov 12, 2013 *
  18. * Author: Ma Bingyao <andot@hprfc.com> *
  19. * *
  20. \**********************************************************/
  21. abstract class HproseAbstractStream {
  22. public abstract function close();
  23. public abstract function getc();
  24. public abstract function read($length);
  25. public abstract function readuntil($char);
  26. public abstract function seek($offset, $whence = SEEK_SET);
  27. public abstract function mark();
  28. public abstract function unmark();
  29. public abstract function reset();
  30. public abstract function skip($n);
  31. public abstract function eof();
  32. public abstract function write($string, $length = -1);
  33. }
  34. class HproseStringStream extends HproseAbstractStream {
  35. protected $buffer;
  36. protected $pos;
  37. protected $mark;
  38. protected $length;
  39. public function __construct($string = '') {
  40. $this->buffer = $string;
  41. $this->pos = 0;
  42. $this->mark = -1;
  43. $this->length = strlen($string);
  44. }
  45. public function close() {
  46. $this->buffer = NULL;
  47. $this->pos = 0;
  48. $this->mark = -1;
  49. $this->length = 0;
  50. }
  51. public function length() {
  52. return $this->length;
  53. }
  54. public function getc() {
  55. return $this->buffer{$this->pos++};
  56. }
  57. public function read($length) {
  58. $s = substr($this->buffer, $this->pos, $length);
  59. $this->skip($length);
  60. return $s;
  61. }
  62. public function readuntil($tag) {
  63. $pos = strpos($this->buffer, $tag, $this->pos);
  64. if ($pos !== false) {
  65. $s = substr($this->buffer, $this->pos, $pos - $this->pos);
  66. $this->pos = $pos + strlen($tag);
  67. }
  68. else {
  69. $s = substr($this->buffer, $this->pos);
  70. $this->pos = $this->length;
  71. }
  72. return $s;
  73. }
  74. public function seek($offset, $whence = SEEK_SET) {
  75. switch ($whence) {
  76. case SEEK_SET:
  77. $this->pos = $offset;
  78. break;
  79. case SEEK_CUR:
  80. $this->pos += $offset;
  81. break;
  82. case SEEK_END:
  83. $this->pos = $this->length + $offset;
  84. break;
  85. }
  86. $this->mark = -1;
  87. return 0;
  88. }
  89. public function mark() {
  90. $this->mark = $this->pos;
  91. }
  92. public function unmark() {
  93. $this->mark = -1;
  94. }
  95. public function reset() {
  96. if ($this->mark != -1) {
  97. $this->pos = $this->mark;
  98. }
  99. }
  100. public function skip($n) {
  101. $this->pos += $n;
  102. }
  103. public function eof() {
  104. return ($this->pos >= $this->length);
  105. }
  106. public function write($string, $length = -1) {
  107. if ($length == -1) {
  108. $this->buffer .= $string;
  109. $length = strlen($string);
  110. }
  111. else {
  112. $this->buffer .= substr($string, 0, $length);
  113. }
  114. $this->length += $length;
  115. }
  116. public function toString() {
  117. return $this->buffer;
  118. }
  119. }
  120. class HproseFileStream extends HproseAbstractStream {
  121. protected $fp;
  122. protected $buf;
  123. protected $unmark;
  124. protected $pos;
  125. protected $length;
  126. public function __construct($fp) {
  127. $this->fp = $fp;
  128. $this->buf = "";
  129. $this->unmark = true;
  130. $this->pos = -1;
  131. $this->length = 0;
  132. }
  133. public function close() {
  134. return fclose($this->fp);
  135. }
  136. public function getc() {
  137. if ($this->pos == -1) {
  138. return fgetc($this->fp);
  139. }
  140. elseif ($this->pos < $this->length) {
  141. return $this->buf{$this->pos++};
  142. }
  143. elseif ($this->unmark) {
  144. $this->buf = "";
  145. $this->pos = -1;
  146. $this->length = 0;
  147. return fgetc($this->fp);
  148. }
  149. elseif (($c = fgetc($this->fp)) !== false) {
  150. $this->buf .= $c;
  151. $this->pos++;
  152. $this->length++;
  153. }
  154. return $c;
  155. }
  156. public function read($length) {
  157. if ($this->pos == -1) {
  158. return fread($this->fp, $length);
  159. }
  160. elseif ($this->pos < $this->length) {
  161. $len = $this->length - $this->pos;
  162. if ($len < $length) {
  163. $s = fread($this->fp, $length - $len);
  164. $this->buf .= $s;
  165. $this->length += strlen($s);
  166. }
  167. $s = substr($this->buf, $this->pos, $length);
  168. $this->pos += strlen($s);
  169. }
  170. elseif ($this->unmark) {
  171. $this->buf = "";
  172. $this->pos = -1;
  173. $this->length = 0;
  174. return fread($this->fp, $length);
  175. }
  176. elseif (($s = fread($this->fp, $length)) !== "") {
  177. $this->buf .= $s;
  178. $len = strlen($s);
  179. $this->pos += $len;
  180. $this->length += $len;
  181. }
  182. return $s;
  183. }
  184. public function readuntil($char) {
  185. $s = '';
  186. while ((($c = $this->getc()) != $char) && $c !== false) $s .= $c;
  187. return $s;
  188. }
  189. public function seek($offset, $whence = SEEK_SET) {
  190. if (fseek($this->fp, $offset, $whence) == 0) {
  191. $this->buf = "";
  192. $this->unmark = true;
  193. $this->pos = -1;
  194. $this->length = 0;
  195. return 0;
  196. }
  197. return -1;
  198. }
  199. public function mark() {
  200. $this->unmark = false;
  201. if ($this->pos == -1) {
  202. $this->buf = "";
  203. $this->pos = 0;
  204. $this->length = 0;
  205. }
  206. elseif ($this->pos > 0) {
  207. $this->buf = substr($this->buf, $this->pos);
  208. $this->length -= $this->pos;
  209. $this->pos = 0;
  210. }
  211. }
  212. public function unmark() {
  213. $this->unmark = true;
  214. }
  215. public function reset() {
  216. $this->pos = 0;
  217. }
  218. public function skip($n) {
  219. $this->read($n);
  220. }
  221. public function eof() {
  222. if (($this->pos != -1) && ($this->pos < $this->length)) return false;
  223. return feof($this->fp);
  224. }
  225. public function write($string, $length = -1) {
  226. if ($length == -1) $length = strlen($string);
  227. return fwrite($this->fp, $string, $length);
  228. }
  229. }
  230. class HproseProcStream extends HproseAbstractStream {
  231. protected $process;
  232. protected $pipes;
  233. protected $buf;
  234. protected $unmark;
  235. protected $pos;
  236. protected $length;
  237. public function __construct($process, $pipes) {
  238. $this->process = $process;
  239. $this->pipes = $pipes;
  240. $this->buf = "";
  241. $this->unmark = true;
  242. $this->pos = -1;
  243. $this->length = 0;
  244. }
  245. public function close() {
  246. fclose($this->pipes[0]);
  247. fclose($this->pipes[1]);
  248. proc_close($this->process);
  249. }
  250. public function getc() {
  251. if ($this->pos == -1) {
  252. return fgetc($this->pipes[1]);
  253. }
  254. elseif ($this->pos < $this->length) {
  255. return $this->buf{$this->pos++};
  256. }
  257. elseif ($this->unmark) {
  258. $this->buf = "";
  259. $this->pos = -1;
  260. $this->length = 0;
  261. return fgetc($this->pipes[1]);
  262. }
  263. elseif (($c = fgetc($this->pipes[1])) !== false) {
  264. $this->buf .= $c;
  265. $this->pos++;
  266. $this->length++;
  267. }
  268. return $c;
  269. }
  270. public function read($length) {
  271. if ($this->pos == -1) {
  272. return fread($this->pipes[1], $length);
  273. }
  274. elseif ($this->pos < $this->length) {
  275. $len = $this->length - $this->pos;
  276. if ($len < $length) {
  277. $s = fread($this->pipes[1], $length - $len);
  278. $this->buf .= $s;
  279. $this->length += strlen($s);
  280. }
  281. $s = substr($this->buf, $this->pos, $length);
  282. $this->pos += strlen($s);
  283. }
  284. elseif ($this->unmark) {
  285. $this->buf = "";
  286. $this->pos = -1;
  287. $this->length = 0;
  288. return fread($this->pipes[1], $length);
  289. }
  290. elseif (($s = fread($this->pipes[1], $length)) !== "") {
  291. $this->buf .= $s;
  292. $len = strlen($s);
  293. $this->pos += $len;
  294. $this->length += $len;
  295. }
  296. return $s;
  297. }
  298. public function readuntil($char) {
  299. $s = '';
  300. while ((($c = $this->getc()) != $char) && $c !== false) $s .= $c;
  301. return $s;
  302. }
  303. public function seek($offset, $whence = SEEK_SET) {
  304. if (fseek($this->pipes[1], $offset, $whence) == 0) {
  305. $this->buf = "";
  306. $this->unmark = true;
  307. $this->pos = -1;
  308. $this->length = 0;
  309. return 0;
  310. }
  311. return -1;
  312. }
  313. public function mark() {
  314. $this->unmark = false;
  315. if ($this->pos == -1) {
  316. $this->buf = "";
  317. $this->pos = 0;
  318. $this->length = 0;
  319. }
  320. elseif ($this->pos > 0) {
  321. $this->buf = substr($this->buf, $this->pos);
  322. $this->length -= $this->pos;
  323. $this->pos = 0;
  324. }
  325. }
  326. public function unmark() {
  327. $this->unmark = true;
  328. }
  329. public function reset() {
  330. $this->pos = 0;
  331. }
  332. public function skip($n) {
  333. $this->read($n);
  334. }
  335. public function eof() {
  336. if (($this->pos != -1) && ($this->pos < $this->length)) return false;
  337. return feof($this->pipes[1]);
  338. }
  339. public function write($string, $length = -1) {
  340. if ($length == -1) $length = strlen($string);
  341. return fwrite($this->pipes[0], $string, $length);
  342. }
  343. }
  344. ?>