This is automatic generated document for things in Swow\Buffer class

Class Buffer

Constants

public COMMON_SIZE = 8192 public PAGE_SIZE = 4096

This constant holds page size of this machine, it’s platform-dependent.

At Linux mips64 and macOS arm64 platforms, this constant may have a value 16384

Methods

Static method alignSize

public static function alignSize ( int $size , int $alignment ) : int

Round the size up to a multiple of the alignment.

The alignment must be a power of two. Other values give a result that is not a multiple.

Returns int the aligned size

Method __construct

public function __construct ( int $size )

Create a buffer and allocate the memory.

Use new Buffer(0) when the size is not known. Then call alloc().

Method __debugInfo

public function __debugInfo ( ) : array<string, mixed>

Returns array<string, mixed> debug information for var_dump

Method __serialize

public function __serialize ( ) : array{value: string}

Serialize the value.

The size is not kept. After unserialize() the size equals the length.

Method __toString

public function __toString ( ) : string

Method __unserialize

public function __unserialize ( (array<mixed, mixed> | array<mixed>) $data ) : void

Method alloc

public function alloc ( int $size ) : void

Allocate the memory.

The method works only on a buffer without memory. Use new Buffer(0).

Throws \Error when the buffer already has memory

Method append

public function append ( \Stringable|string $string , int<0, max> $start , int<-1, max> $length ) : int

append $string to the end of buffer

$start and $length is for input $string The buffer grows when the data does not fit.

Returns int bytes written

Throws \ValueError when specified $start, $offset or $length not in range

Method clear

public function clear ( ) : void

Method close

public function close ( ) : void

Free the memory and set size and length to 0.

A second close() does nothing. A write after close() returns the byte count and discards the data.

Method dupString

public function dupString ( ) : string

Method extend

public function extend ( int $recommendSize ) : void

Grow the buffer.

The buffer doubles its size until it reaches the recommended size. The result can be larger than the request.

Param int $recommendSize [optional] = $this->getSize() * 2

Throws \ValueError when the size is not larger than the current size

Method fetchString

public function fetchString ( ) : string

Get the value and reset the buffer.

Returns string the value

Throws \Error when the buffer is locked

Method getAvailableSize

public function getAvailableSize ( ) : int

Method getLength

public function getLength ( ) : int

Method getLocker

public function getLocker ( ) : (Coroutine | null)

Returns (Coroutine | null) the locker, or null

Method getSize

public function getSize ( ) : int

Method isAvailable

public function isAvailable ( ) : bool

Method isEmpty

public function isEmpty ( ) : bool

Method isFull

public function isFull ( ) : bool

Method isLocked

public function isLocked ( ) : bool

Returns bool true when the buffer is locked

Method lock

public function lock ( ) : void

Lock the buffer.

A locked buffer rejects write operations with Error. The lock holder is not exempt. The lock stops write calls only. It is not a mutex.

Throws \Error when the buffer is already locked

Method mallocTrim

public function mallocTrim ( ) : void

Free the unused memory.

The buffer shrinks to the used length.

Method prepare

public function prepare ( int $appendLength ) : void

Method read

public function read ( int<0, max> $start , int<-1, max> $length ) : string

read at max $length bytes data from start

Returns string data read

Throws \ValueError when specified $start and $length not in range

Method realloc

public function realloc ( int $size ) : void

Method toString

public function toString ( ) : string

Method truncate

public function truncate ( int $length ) : int

Returns int length of buffer after truncate

Method truncateFrom

public function truncateFrom ( int $offset , int $length ) : int

Param int $offset [optional] = $this->getOffset()

Returns int length of buffer after truncate

Method tryLock

public function tryLock ( ) : bool

Try to lock the buffer.

Returns bool false when the buffer is locked

Method unlock

public function unlock ( ) : void

Unlock the buffer.

Throws \Error when the buffer is not locked, or when the caller is not the locker

Method write

public function write ( int<0, max> $offset , \Stringable|string $string , int<0, max> $start , int<-1, max> $length ) : int

write $string to buffer

$start and $length is for input $string $offset is for output $buffer

The offset must not be larger than the current length. The first write into a new buffer starts at 0. The buffer grows when the data does not fit.

Returns int bytes written

Throws \ValueError when specified $start, $offset or $length not in range


Copyright 2022 the Swow contributors