Classes

File io/filesystem/stream/BufferedFileReader.class.php

File io/filesystem/stream/BufferedFileReader.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 
<?php
/**
 * @package     IO
 * @subpackage  Filesystem\stream
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Benoit SAUTEL <ben.popeye@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 10 28
 * @since       PHPBoost 3.0 - 2010 05 29
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class BufferedFileReader implements FileReader
{
    const DEFAULT_BUFFER_SIZE = 100000;

    /**
     * @var File
     */
    private $file;
    private $buffer_max_size;
    private $offset_in_file = 0;
    private $reached_end_of_file = false;
    private $lines = array();

    public function __construct(File $file, $buffer_size = self::DEFAULT_BUFFER_SIZE)
    {
        $this->file = $file;
        $this->buffer_max_size = $buffer_size;
    }

    public function read_all()
    {
        $result = '';
        while (!$this->reached_end_of_file)
        {
            $result .= $this->read_packet();
        }
        return $result;
    }

    private function read_packet()
    {
        $buffer = $this->file->read($this->offset_in_file, $this->buffer_max_size);
        $buffer_size = TextHelper::strlen($buffer);
        $this->offset_in_file += TextHelper::strlen($buffer);
        if ($buffer_size == 0)
        {
            $this->reached_end_of_file = true;
        }
        return $buffer;
    }

    public function read_line()
    {
        if ($this->has_no_more_line())
        {
            return null;
        }
        if ($this->has_buffered_line() || $this->is_last_line())
        {
            return $this->get_oldest_line();
        }
        else
        {
            while (!$this->has_buffered_line() && !$this->reached_end_of_file)
            {
                $this->read_lines_in_new_packet();
            }
            return $this->get_oldest_line();
        }
    }

    private function has_buffered_line()
    {
        // We must have at least two entries because we don't know if the last one is a full line or a partial one.
        return count($this->lines) > 1;
    }

    private function read_lines_in_new_packet()
    {
        $packet = $this->read_packet();
        $lines = explode("\n", $packet);
        $last_partial_line = array_pop($this->lines);
        if ($last_partial_line == null)
        {
            $last_partial_line = '';
        }
        $last_partial_line .= array_shift($lines);
        array_push($this->lines, $last_partial_line);
        $this->lines = array_merge($this->lines, $lines);
    }

    private function is_last_line()
    {
        return $this->reached_end_of_file && count($this->lines) == 1;
    }

    private function has_no_more_line()
    {
        return $this->reached_end_of_file && count($this->lines) == 0;
    }

    private function get_oldest_line()
    {
        return array_shift($this->lines);
    }
}
?>