Ferreteria/v0.6/clade/IO/Aspect/Connx/Buffer/File/Reader
Jump to navigation
Jump to search
| ||||||||||||||||
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 -- //
}