Seekable I/O Object¶
pcapkit.corekit.io contains
SeekableReader, a seekable customisation of
io.BufferedReader.
- class pcapkit.corekit.io.SeekableReader(raw, buffer_size=131072, buffer_save=False, buffer_path=None, *, stream_closing=True)[source]¶
Bases:
BufferedReaderSeekable buffered reader.
A buffered binary stream providing higher-level access to a readable, non seekable
RawIOBaseraw binary stream. It inheritsBufferedIOBase.When reading data from this object, a larger amount of data may be requested from the underlying raw stream, and kept in an internal buffer. The buffered data can then be returned directly on subsequent reads.
The constructor creates a
BufferedReaderfor the given readablerawstream andbuffer_size. Ifbuffer_sizeis omitted,DEFAULT_BUFFER_SIZEis used.- Parameters:
- property raw: RawIOBase¶
The underlying raw stream (a
RawIOBaseinstance) thatBufferedIOBasedeals with. This is not part of theBufferedIOBaseAPI and may not exist on some implementations.
- read(size=-1, /)[source]¶
Read and return
sizebytes, or ifsizeis not given or negative, until EOF or if the read call would block in non-blocking mode.- Return type:
- read1(size=-1, /)[source]¶
Read and return up to
sizebytes with only one call on the raw stream. If at least one byte is buffered, only buffered bytes are returned. Otherwise, one raw stream read call is made.- Return type:
- readinto(b, /)[source]¶
Read bytes into a pre-allocated, writable bytes-like object
band return the number of bytes read. For example,bmight be abytearray.Like
read(), multiple reads may be issued to the underlying raw stream, unless the latter is interactive.A
BlockingIOErroris raised if the underlying raw stream is in non blocking-mode, and has no data available at the moment.- Return type:
- readinto1(b, /)[source]¶
Read bytes into a pre-allocated, writable bytes-like object
b, using at most one call to the underlying raw stream’sread()(orreadinto()) method. Return the number of bytes read.A
BlockingIOErroris raised if the underlying raw stream is in non blocking-mode, and has no data available at the moment.- Return type:
- readable()[source]¶
Return
Trueif the stream can be read from. IfFalse,read()will raiseOSError.- Return type:
- readline(size=-1, /)[source]¶
Read and return one line from the stream. If
sizeis specified, at mostsizebytes will be read.The line terminator is always
b'\n'for binary files; for text files, thenewlineargument toopen()can be used to select the line terminator(s) recognized.- Return type:
- readlines(hint=-1, /)[source]¶
Read and return a list of lines from the stream.
hintcan be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceedshint.hintvalues of0or less, as well asNone, are treated as no hint.Note that it’s already possible to iterate on file objects using
for line in file: ...without callingfile.readlines().
- writable()[source]¶
Return
Trueif the stream supports writing. IfFalse,write()andtruncate()will raiseOSError.Note
This was spelled
writeableuntil #645, which is not how theioprotocol spells it, so it overrode nothing andionever consulted it – the inheritedio.IOBase.writable()answered instead. Both returnedFalse, so there was no observable divergence to notice; the coincidence is what hid it.- Return type:
- write(b, /)[source]¶
Write the given bytes-like object,
b, and return the number of bytes written (always equal to the length ofbin bytes, since if the write fails anOSErrorwill be raised). Depending on the actual implementation, these bytes may be readily written to the underlying stream, or held in a buffer for performance and latency reasons.When in non-blocking mode, a
BlockingIOErroris raised if the data needed to be written to the raw stream but it couldn’t accept all the data without blocking.The caller may release or mutate
bafter this method returns, so the implementation should only accessbduring the method call.- Return type:
- seekable()[source]¶
Return
Trueif the stream supports random access. IfFalse,seek(),tell()andtruncate()will raiseOSError.- Return type:
- seek(offset, whence=0, /)[source]¶
Change the stream position to the given byte
offset.offsetis interpreted relative to the position indicated bywhence. The default value forwhenceisSEEK_SET. Values forwhenceare:SEEK_SETor0- start of the stream (the default);offsetshould be zero or positiveSEEK_CURor1- current stream position;offsetmay be negativeSEEK_ENDor2- end of the stream;offsetis usually negative
Return the new absolute position.
Note
The target is computed, then validated, and only then written to
_tell. A branch that assigned the position before deciding whether to accept it left a refused seek having moved it anyway, with the resync below – the one thing that puts the buffer’s cursor back in step – skipped on the way out. A caller that catches the error and reasonably takes the position to be unchanged then read from the rejected offset instead, silently and without a second error.The negative check applies to every
whence, rather than onlySEEK_SETlooking at its offset. An absolute position below zero cannot exist under any of them, and it is not the same failure as a position that has merely slid out of the window – which is ordinary, and recoverable withbuffer_save=True. Reporting the first asnegative seek valuekeeps the two distinguishable, and being a property of the position rather than of the window it holds with a saved buffer as well, where the old refusal did not apply at all and the seek returned a negative position as if it had worked.- Return type:
- truncate(size=None, /)[source]¶
Resize the stream to the given
sizein bytes (or the current position ifsizeis not specified).- Raises:
UnsupportedOperation – Always, since the reader is not writable.
Note
io.IOBase.writable()gates this method as well aswrite()– “IfFalse,write()andtruncate()will raiseOSError” – andwritable()here returnsFalse, so refusing is what the contract asks for. It is also what CPython’s own read-only buffered readers do: both the acceleratedio.BufferedReaderand the pure-Python_pyio.BufferedReaderraiseio.UnsupportedOperation, the latter from_BufferedIOMixin.truncate’s_checkWritable()(#645).The resizing this used to perform is still reachable internally, as
_truncate_buffer(). It never touched the underlying stream in the first place – it resizes a private lookback window – which is why it survives under a private name rather than being removed with the public method’s behaviour.- Return type:
- close()[source]¶
Flush and close this stream. This method has no effect if the file is already closed. Once the file is closed, any operation on the file (e.g. reading or writing) will raise a
ValueError.As a convenience, it is allowed to call this method more than once; only the first call, however, will have an effect.
- flush()[source]¶
Flush the write buffers of the stream if applicable. This does nothing for read-only and non-blocking streams.
- peek(size=0)[source]¶
Return bytes from the stream without advancing the position.
At most one single read on the raw stream is done to satisfy the call. The number of bytes returned may be less or more than requested.
- Return type:
- detach()[source]¶
Separate the underlying raw stream from the buffer and return it.
After the raw stream has been detached, the buffer is in an unusable state.
Some buffers, like
BytesIO, do not have the concept of a single raw stream to return from this method. They raiseUnsupportedOperation.- Return type: