Moodle PHP Documentation 4.2
Moodle 4.2.8 (Build: 20240610) (2d41ac46f45)
StreamReader Class Reference

A stream reader class. More...

Public Member Functions

 __construct ($stream, $closeStream=false)
 StreamReader constructor.
 
 __destruct ()
 The destructor.
 
 addOffset ($offset)
 Add an offset to the current offset.
 
 cleanUp ()
 Closes the file handle.
 
 ensure ($pos, $length)
 Ensures bytes in the buffer with a specific length and location in the file.
 
 ensureContent ()
 Make sure that there is at least one character beyond the current offset in the buffer.
 
 getBuffer ($atOffset=true)
 Returns the current buffer.
 
 getBufferLength ($atOffset=false)
 Returns the byte length of the buffer.
 
 getByte ($position=null)
 Gets a byte at a specific position in the buffer.
 
 getOffset ()
 Returns the current offset in the current buffer.
 
 getPosition ()
 Get the current position in the stream.
 
 getStream ()
 Returns the stream.
 
 getTotalLength ()
 Gets the total available length.
 
 increaseLength ($minLength=100)
 Forcefully read more data into the buffer.
 
 readByte ($position=null)
 Returns a byte at a specific position, and set the offset to the next byte position.
 
 readBytes ($length, $position=null)
 Read bytes from the current or a specific offset position and set the internal pointer to the next byte.
 
 readLine ($length=1024)
 Read a line from the current position.
 
 reset ($pos=0, $length=200)
 Resets the buffer to a position and re-read the buffer with the given length.
 
 setOffset ($offset)
 Set the offset position in the current buffer.
 

Static Public Member Functions

static createByFile ($filename)
 Creates a stream reader instance by a filename.
 
static createByString ($content, $maxMemory=2097152)
 Creates a stream reader instance by a string value.
 

Protected Attributes

string $buffer
 The buffer.
 
int $bufferLength
 The buffer length.
 
bool $closeStream
 Defines whether the stream should be closed when the stream reader instance is deconstructed or not.
 
int $offset
 The byte-offset position in the buffer.
 
int $position
 The byte-offset position in the stream.
 
resource $stream
 The stream resource.
 
int $totalLength
 The total length of the stream.
 

Detailed Description

A stream reader class.

Constructor & Destructor Documentation

◆ __construct()

StreamReader::__construct ( $stream,
$closeStream = false )

StreamReader constructor.

Parameters
resource$stream
bool$closeStreamDefines whether to close the stream resource if the instance is destructed or not.

Member Function Documentation

◆ addOffset()

StreamReader::addOffset ( $offset)

Add an offset to the current offset.

Parameters
int$offset

◆ createByFile()

static StreamReader::createByFile ( $filename)
static

Creates a stream reader instance by a filename.

Parameters
string$filename
Return values
StreamReader

◆ createByString()

static StreamReader::createByString ( $content,
$maxMemory = 2097152 )
static

Creates a stream reader instance by a string value.

Parameters
string$content
int$maxMemory
Return values
StreamReader

◆ ensure()

StreamReader::ensure ( $pos,
$length )

Ensures bytes in the buffer with a specific length and location in the file.

Parameters
int$pos
int$length
See also
reset()

◆ ensureContent()

StreamReader::ensureContent ( )

Make sure that there is at least one character beyond the current offset in the buffer.

Return values
bool

◆ getBuffer()

StreamReader::getBuffer ( $atOffset = true)

Returns the current buffer.

Parameters
bool$atOffset
Return values
string

◆ getBufferLength()

StreamReader::getBufferLength ( $atOffset = false)

Returns the byte length of the buffer.

Parameters
bool$atOffset
Return values
int

◆ getByte()

StreamReader::getByte ( $position = null)

Gets a byte at a specific position in the buffer.

If the position is invalid the method will return false.

If the $position parameter is set to null the value of $this->offset will be used.

Parameters
int | null$position
Return values
string|bool

◆ getOffset()

StreamReader::getOffset ( )

Returns the current offset in the current buffer.

Return values
int

◆ getPosition()

StreamReader::getPosition ( )

Get the current position in the stream.

Return values
int

◆ getStream()

StreamReader::getStream ( )

Returns the stream.

Return values
resource

◆ getTotalLength()

StreamReader::getTotalLength ( )

Gets the total available length.

Return values
int

◆ increaseLength()

StreamReader::increaseLength ( $minLength = 100)

Forcefully read more data into the buffer.

Parameters
int$minLength
Return values
boolReturns false if the stream reaches the end

◆ readByte()

StreamReader::readByte ( $position = null)

Returns a byte at a specific position, and set the offset to the next byte position.

If the position is invalid the method will return false.

If the $position parameter is set to null the value of $this->offset will be used.

Parameters
int | null$position
Return values
string|bool

◆ readBytes()

StreamReader::readBytes ( $length,
$position = null )

Read bytes from the current or a specific offset position and set the internal pointer to the next byte.

If the position is invalid the method will return false.

If the $position parameter is set to null the value of $this->offset will be used.

Parameters
int$length
int | null$position
Return values
string|false

◆ readLine()

StreamReader::readLine ( $length = 1024)

Read a line from the current position.

Parameters
int$length
Return values
string|bool

◆ reset()

StreamReader::reset ( $pos = 0,
$length = 200 )

Resets the buffer to a position and re-read the buffer with the given length.

If the $pos parameter is negative the start buffer position will be the $pos'th position from the end of the file.

If the $pos parameter is negative and the absolute value is bigger then the totalLength of the file $pos will set to zero.

Parameters
int | null$posStart position of the new buffer
int$lengthLength of the new buffer. Mustn't be negative

◆ setOffset()

StreamReader::setOffset ( $offset)

Set the offset position in the current buffer.

Parameters
int$offset

The documentation for this class was generated from the following file: