1.2.10Class MemoryBuffer

Metaclass for MemBuf items.

Class MemoryBuffer from \
                 BOM( )

The Memory Buffers have a set of internal pointers and sequence methods useful to parse binary streams read in variable size chunks.

Initially, allocate a memory buffer wide enough to store enough data. The maximum possible amount of data units (generally bytes) that can be stored in a memory buffer is its length, returned by the BOM len method or len function.

After having read some contents from a stream, the buffer limit will be moved to the amount of incoming data (which may be also the same as the length if the buffer is completely filled).

The application may get one item at a time via the MemoryBuffer.get() method, or process blocks of data transferring them to other membufs or arrays via ranged operators.

Each get() moves a position indicator forward up to the limit. At the same time, it is possible to put data in the buffer, moving forward the position pointer up to the limit.

The buffer has a marker, that can be set at current position, and that can be later used to to return to a previously marked position.

The following invariants always hold:


      0 <= [mark] <= position <= limit <= length

The limit is usually set to the buffer length, unless it is explicitly set to a lower position via explicit calls, or the last read didn't bear enough data to fill the buffer.

The following operations are meant to simplify read and partial parsing of binary data:

All the members in this group not explicitly returning data or sizes return the MemPool itself, so that it is possible to concatenate calls like this:


      mb.clear()
      mb.position(3)

      // equivalent:
      mb.clear().position(3)
Methods
backReturns the last element in this memory buffer.
clearClears the buffer resetting it to initial status.
compactDiscards processed data and prepares to a new read.
fillFills all the elements in the memory buffer with a given value.
firstReturns an iterator to the first element of this buffer.
flipSets the limit to the current position, and the position to zero.
frontReturns the first element in this memory buffer.
getGets a value in the memory buffer.
lastReturns an iterator to the last element of this buffer.
limitGets or sets current filled data size.
markPlaces the mark at current position.
positionSets or get the current position in the buffer.
ptrReturns the pointer to the raw memory stored in this memory buffer.
putPuts a value in the memory buffer.
remainingDetermines how many items can be read.
resetReturns the position to the last mark.
rewindRewinds the buffer and discards the mark.
wordSizeReturns the number of bytes used to store each entry of this Memory Buffer.
Methods inherited from class BOM
__addOverrides binary addition operand.
__callOverrides call operator "self()".
__decOverrides decrement unary prefix operand.
__decpostOverrides decrement unary postfix operand.
__divOverrides binary division operand.
__getIndexOverrides array access operator []
__incOverrides increment unary prefix operand.
__incpostOverrides increment unary postifx operand.
__modOverrides modulo operand.
__mulOverrides binary multiplication operand.
__powOverrides power operand.
__setIndexOverrides array write operator []
__subOverrides binary subtraction operand.
baseClassReturns the class item from which an object has been instantiated.
boundDetermines if an item is bound or not.
classNameReturns the name of the class an instance is instantiated from.
clonePerforms a deep copy of the item.
comparePerforms a lexicographical comparison.
derivedFromChecks if this item has a given parent.
describeReturns the deep contents of an item on a string representation.
isCallableDetermines if an item is callable.
lenRetrieves the length of a collection
metaclassReturns the metaclass associated with this item.
ptrReturns a raw memory pointer out of this data (as an integer).
serializeSerialize the item on a stream for persistent storage.
toStringCoverts the object to string.
typeIdReturns an integer indicating the type of this item.

Methods

back

Returns the last element in this memory buffer.

MemoryBuffer.back()
ReturnA number representing the last element in this buffer.
Raise
AccessError if this buffer is empty.

clear

Clears the buffer resetting it to initial status.

MemoryBuffer.clear()
ReturnThe buffer itself.

Removes the mark, sets the position to zero and the limit to the length. This makes the buffer ready for a new set of operations.

compact

Discards processed data and prepares to a new read.

MemoryBuffer.compact()
ReturnThe buffer itself

This operation moves all the bytes between the position (included) and the limit (escluded) to the beginning of the buffer. Then, the position is moved to the previous value of the limit, and the limit is reset to the length of the buffer.

As read is performed by filling the data between the current position and the limit, this operation allows to trying get more data when a previously imcomplete input was taken.

Note: Just, remember to move the position back to the first item that was still to be decoded before compact

fill

Fills all the elements in the memory buffer with a given value.

MemoryBuffer.fill( value )
value The value to be applied to the memory buffer.
ReturnThis memory buffer.

first

Returns an iterator to the first element of this buffer.

MemoryBuffer.first()
ReturnAn iterator.

flip

Sets the limit to the current position, and the position to zero.

MemoryBuffer.flip()
ReturnThe buffer itself

This is useful to write some parsed or created contents back on a stream. Once filled the buffer with data, the position is on the last element; then, using flip and writing the buffer, the prepared data is sent to the output.

front

Returns the first element in this memory buffer.

MemoryBuffer.front()
ReturnA number representing the first element in this buffer.
Raise
AccessError if this buffer is empty.

get

Gets a value in the memory buffer.

MemoryBuffer.get()
Returnthe retreived value.
Raise
AccessError if the buffer is full (limit() == position())

Gets the vaulue at current position and advances it.

last

Returns an iterator to the last element of this buffer.

MemoryBuffer.last()
ReturnAn iterator.

limit

Gets or sets current filled data size.

MemoryBuffer.limit( [pos] )
pos The value to be applied to the memory buffer.
Returncurrent limit.

This method can be used to get the current postition in the memory buffer, or to set a new position. In the first case, the current value of the position is returned, in the second case the current buffer is returned.

mark

Places the mark at current position.

MemoryBuffer.mark()
ReturnThe buffer itself

This position sets the mark at current position; calling MemoryBuffer.reset later on will move the position back to this point.

position

Sets or get the current position in the buffer.

MemoryBuffer.position( [pos] )
pos The new position
ReturnThe buffer itself or the requested position.
Raise
AccessError if the pos parameter is > limit.

This method can be used to get the current postition in the memory buffer, or to set a new position. In the first case, the current value of the position is returned, in the second case the current buffer is returned.

As the position can never be greater that the limit, an error is raised in case a value too high has been set.

ptr

Returns the pointer to the raw memory stored in this memory buffer.

MemoryBuffer.ptr()
ReturnA memory pointer.

This function returns a pointer (as a Falcon integer) to the memory area managed by this memory buffer.

put

Puts a value in the memory buffer.

MemoryBuffer.put()
ReturnThe buffer itself.
Raise
AccessError if the buffer is full (limit() == position())

Sets the vaulue at current position and advances it.

remaining

Determines how many items can be read.

MemoryBuffer.remaining()
ReturnCount of remanining items.

Returns the count of times get and put can be called on this memory buffer before raising an error.

reset

Returns the position to the last mark.

MemoryBuffer.reset()
ReturnThe buffer itself
Raise
AccessError if the mark is not defined.

Returns the position at the value previously marked with MemoryBuffer.mark. The mark itself is not discarded.

rewind

Rewinds the buffer and discards the mark.

MemoryBuffer.rewind()
ReturnThe buffer itself

Returns the position at 0 and discards the mark. Limit is unchanged.

wordSize

Returns the number of bytes used to store each entry of this Memory Buffer.

MemoryBuffer.wordSize()
ReturnSize of each memory buffer entity in bytes.
Made with http://www.falconpl.org