5 class FileInputStream
implements InputStream
{
10 public function __construct(string $file) {
11 $this->source
= $file;
12 $this->size
= filesize($this->source
);
15 public function open() {
16 $this->source
= fopen($this->source
, "r");
19 public function is_opened() {
20 return is_resource($this->source
);
22 public function is_ended() {
23 return feof($this->source
);
26 public function close() {
28 fclose($this->source
);
31 public function read(integer $size = NULL) {
32 if ($size == NULL) $size = 1;
34 $buf = fread($this->source
, $size);
35 $this->readed +
= sizeof($buf);
39 public function read_line() {
45 if ($buf == "\n") break;
48 } catch (IOException
$e) {
49 if (($e->getCode() == IOConst
::X_STREAM_ENDED
) && $line != '') return $line;
55 public function available() {
56 return $this->size
- $this->readed
;
59 protected function checks() {
60 if (!$this->is_opened()) {
61 throw new IOException(IOConst
::X_STREAM_NOT_OPENED
, "You cannot operate with stream before you open it.");
63 if ($this->is_ended()) {
64 throw new IOException(IOConst
::X_STREAM_ENDED
, "Unable to operate with free stream.");