Creates a Buffer object. The buffer class forms the basis for IO in the Concurrence Framework.
The buffer class represents a mutable array of bytes of that can be read from and written to using the
read_XXX and write_XXX methods.
Operations on the buffer are performed relative to the current position attribute of the buffer.
A buffer also has a current limit property above which no data may be read or written.
If an operation tries to read beyond the current limit a BufferUnderflowError is raised. If an operation
tries to write beyond the current limit a BufferOverflowError is raised.
The general idea of the Buffer was shamelessly copied from java NIO.
-
copy(src, src_start, dst_start, length)
- Copies length bytes from buffer src, starting at position src_start, to this
buffer at position dst_start.
-
clear()
- Prepares the buffer for relative read operations. The buffers limit will set to the buffers capacity and
its position will be set to 0.
-
compact()
- Prepares the buffer again for relative reading, but any left over data still present in the buffer (the bytes between
the current position and current limit) will be copied to the start of the buffer. The position of the buffer
will be right after the copied data.
-
copy()
- Copies length bytes from buffer src, starting at position src_start, to this
buffer at position dst_start.
-
duplicate()
- Return a shallow copy of the Buffer, e.g. the copied buffer
references the same bytes as the original buffer, but has its own
independend position and limit.
-
flip()
- Prepares the buffer for relative write operations. The buffers limit will set to the buffers position and
its position will be set to 0.
-
read_byte()
- Reads and returns a single byte from the buffer and updates the position by 1.
-
read_bytes()
- Reads n bytes from buffer, updates position, and returns bytes as a python string,
if there are no n bytes available, a BufferUnderflowError is raised.
-
read_bytes_until()
- Reads bytes until character b is found, or end of buffer is reached in which case it will raise a BufferUnderflowError.
-
read_line()
- Reads a single line of bytes from the buffer where the end of the line is indicated by either ‘LF’ or ‘CRLF’.
The line will be returned as a string not including the line-separator. Optionally include_separator can be specified
to make the method to also return the line-separator.
-
read_short()
- Read a 2 byte little endian integer from buffer and updates position.
-
recv()
- Reads as many bytes as will fit up till the limit of the buffer from the filedescriptor fd.
Returns a tuple (bytes_read, bytes_remaining). If bytes_read is negative, a IO Error was encountered.
The position of the buffer will be updated according to the number of bytes read.
-
rewind()
- Sets the buffers position back to 0.
-
send()
- Sends as many bytes as possible up till the limit of the buffer to the filedescriptor fd.
Returns a tuple (bytes_written, bytes_remaining). If bytes_written is negative, an IO Error was encountered.
-
skip()
- Updates the buffers position by skipping n bytes. It is not allowed to skip passed the current limit.
In that case a BufferUnderflowError will be raised and the position will remain the same
-
write_buffer()
- writes available bytes from other buffer to this buffer
-
write_byte()
- writes a single byte to the buffer and updates position
-
write_bytes()
- Writes a number of bytes given by the python string s to the buffer and updates position. Raises
BufferOverflowError if you try to write beyond the current limit.
-
write_int()
- writes a 32 bit integer to the buffer and updates position (little-endian)
-
write_short()
- writes a 16 bit integer to the buffer and updates position (little-endian)