Ferreteria/v0.6/clade/IO/Aspect/Connx/Buffer/File/Reader

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | IO‎ | Aspect‎ | Connx‎ | Buffer‎ | File
Revision as of 03:06, 28 November 2025 by Woozle (talk | contribs) (Created page with "{{page/clade/v2 |fam= {{!}} align=right {{!}} <code>{{l/ver/clade|IO\Aspect\Connx\Buffer|File}}</code> {{!}} align=right {{!}} ⇒ <code>{{l/ver/clade|IO\Aspect\Connx\Buffer\File|Reader}}</code> ⇒ {{!}} align=right {{!}} ''(none)'' |alia= {{!-!}} '''Base'''* [ca,i] {{!!}} <code>{{l/ver/clade/full|IO\Aspect\Connx\Buffer|File}} {{!-!}} '''FModeEnum''' {{!!}} <code>{{l/ver/clade/full|Sys\FileSys|Mode}} }} ==About== * '''Purpose''': a Buffer which rea...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
clade: IO\Aspect\Connx\Buffer\File\Reader
Clade Family
File Reader (none)
Clade Aliases
Alias Clade
Base* [ca,i] IO\Aspect\Connx\Buffer\File
FModeEnum Sys\FileSys\Mode
Subpages

About

  • Purpose: a Buffer which reads data from a file and sends it to the output stream

History

Code

interface iReader extends BaseIface {}
class cReader extends BaseClass implements iReader {
    // ++ CONFIG ++ //

    protected const MAX_BUFF_LEN = 1024 * 1024; // whatever seems to work best

    protected function ModesArray() : array {
        return [
          FModeEnum::FILE_READER,FModeEnum::FLAG_ON,  // need to read
          ];
    }

    // -- CONFIG -- //
    // ++ ACCESS ++ //

    // bytes currently remaining to read from source
    protected function AvailSrceByteCount() : int { return $this->DataFile()->Size(); }

    // NOTE: This is different from $nSpent because it doesn't include what's in the buffer.
    private $nInPtr = NULL;
    public function RewindAll() : void {
        $this->nInPtr = 0;
        parent::RewindAll();
    }
    // ACTION: remove all bytes from the buffer. Refill buffer if needed, I guess?
    // LATER: might want to add a $nMaxBytes parameter, but for now it doesn't seem needed.
    public function RemoveBytes() : string {
        $this->ErrorIfShut('read from');

        // Is there room in the buffer for more?
        $nBuff = $this->AvailBuffByteCount();    // what's in the buffer
        #$this->AmHere("Bytes remaining: [$nBuff]");
        $nRoom = self::MAX_BUFF_LEN - $nBuff;    // room available
        #$this->AmHere("Bytes remaining: [$nBuff] / Space remaining: [$nRoom]");
        if ($nRoom > 0) {
            $nFileAll = $this->AvailSrceByteCount();
            #echo $this->DataFile()->Ident()->ReflectThis()->Report();
            #$this->AmHere("File bytes: [$nFileAll] in ".$this->DataFile()->Ident()->SpecFull());
            $nFileNew = $nFileAll - $this->nInPtr;      // what hasn't been read from the file yet
            if ($nFileNew > 0) {
                // There's more in the file, so refill the buffer:
                $nToRead = min($nRoom,$nFileNew);
                #echo "ADDING $nToRead BYTES (nFileNew=$nFileNew, nRoom=$nRoom) from file".CRLF;
                $oRes = $this->DataFile()->InOut()->Read($nToRead);
                $osData = $oRes->QData();
                if ($osData->HasIt()) {
                    $sData = $osData->GetIt();
                    $nLen = strlen($sData);
                    $this->AddBytesForFetch($sData);
                }
                #echo $oFile->ReflectThis()->Report(); die();
            }
        }

        $sOut = parent::RemoveBytes();  // grab existing buffer-contents

        $this->OnStatusChange();

        return $sOut;
    }

    // -- ACCESS -- //
}