campy.io.bitstream module

File: bitstream.cpp

This file defines the ibitstream and obitstream classes. These classes are patterned after (and, in fact, inherit from) the standard io.BufferedReader and io.BufferedWriter classes.

The ibitstream and obitstream classes are basically the same as the ordinary io.BufferedReader and io.BufferedWriter classes, but add the functionality to read and write one bit at a time.

The idea is that you can substitute an ibitstream in place of an io.BufferedReader and use the same operations (read, close, tell, etc.) along with added member functions of readBit, rewind, and size.

Similarly, the obitstream can be used in place of io.BufferedWriter, and has same operations (write, close, seek, etc.) along with additional member functions writeBit and size.

# You can use ibitstream in any There are two subclasses of ibitstream: ifbitstream and istringbitstream, # which are similar to the io. and istringstream classes. The # obitstream class similarly has ofbitstream and ostringbitstream as # subclasses.

Note: in keeping with the naming conventions of the Python standard library, readBit and writeBit have been renamed as readbit and writebit

Additionally, str() in ibitstream has been removed (doesn’t make much sense anyway) and for consistency with the standard library str in obitstream has been renamed getvalue()

Usage:

To use an ifbitstream:

with ifbitstream(filename) as stream:
    bit = stream.readbit()
    stream.rewind()
    bit = stream.readbit()

To use an ofbitstream:

with ofbitstream(filename) as stream:
    stream.writebit(0)
    ...

To use an ostringbitstream:

with ostringbitstream() as stream:
    stream.writebit(0)
    ...
    stream.getvalue()  # => b'hello world'

To use an istringbitstream:

with istringstream(b'hello world') as stream:
    bit = stream.readbit()
class campy.io.bitstream.ibitstream(raw, buffer_size=8192)[source]

Bases: _io.BufferedReader

readbit()[source]
rewind()[source]
size()[source]
class campy.io.bitstream.obitstream(raw, buffer_size=8192, always_flush=True)[source]

Bases: _io.BufferedWriter

size()[source]
writebit(bit)[source]
class campy.io.bitstream.ifbitstream(filename)[source]

Bases: campy.io.bitstream.ibitstream

class campy.io.bitstream.ofbitstream(filename)[source]

Bases: campy.io.bitstream.obitstream

class campy.io.bitstream.istringbitstream(string)[source]

Bases: campy.io.bitstream.ibitstream

setvalue(string)[source]
class campy.io.bitstream.ostringbitstream[source]

Bases: campy.io.bitstream.obitstream

getvalue()[source]