00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class SocketInputStream extends InputStream
00017 {
00027 const READ_ATTEMPTS = 15;
00028
00029 private $socket = null;
00030 protected $eof = false;
00031
00032 public function __construct(Socket $socket)
00033 {
00034 $this->socket = $socket;
00035 }
00036
00037 public function isEof()
00038 {
00039 return $this->eof;
00040 }
00041
00042 public function read($length)
00043 {
00044 if ($length == 0 || $this->eof)
00045 return null;
00046
00047 try {
00048 $result = $this->socket->read($length);
00049
00050 if ($result === null)
00051 $this->eof = true;
00052
00053 $i = 0;
00054
00055 while (
00056 !$this->eof
00057 && strlen($result) < $length
00058 && ($i < self::READ_ATTEMPTS)
00059 ) {
00060
00061 usleep(100000);
00062
00063 $remainingLength = $length - strlen($result);
00064
00065
00066 $nextPart = $this->socket->read($remainingLength);
00067
00068 if ($nextPart !== null)
00069 $result .= $nextPart;
00070 else
00071 $this->eof = true;
00072
00073 ++$i;
00074 }
00075 } catch (NetworkException $e) {
00076 throw new IOException($e->getMessage());
00077 }
00078
00079 if (!$this->eof && strlen($result) < $length)
00080 throw new IOException(
00081 'connection is too slow or length is too large?'
00082 );
00083
00084 return $result;
00085 }
00086 }
00087 ?>