Data::BitStream::Base - A Role implementing the API for Data::BitStream
use Mouse; with 'Data::BitStream::Base';
A role written for Data::BitStream that provides the basic API, including generic code for almost all functionality.
This is used by particular implementations such as Data::BitStream::String and Data::BitStream::WordVec.
A read-only non-negative integer indicating the current position in a read stream. It is advanced by read, get, and skip methods, as well as changed by to, from, rewind, and erase methods.
read
get
skip
to
from
rewind
erase
A read-only non-negative integer indicating the current length of the stream in bits. It is advanced by write and put methods, as well as changed by from and erase methods.
write
put
A read-only boolean indicating whether the stream is open for writing or reading. Methods for read such as read, get, skip, rewind, skip, and exhausted are not allowed while writing. Methods for write such as write and put are not allowed while reading.
exhausted
The write_open and erase_for_write methods will set writing to true. The write_close and rewind_for_read methods will set writing to false.
write_open
erase_for_write
write_close
rewind_for_read
The read/write distinction allows implementations more freedom in internal caching of data. For instance, they can gather writes into blocks. It also can be helpful in catching mistakes such as reading from a target stream.
Returns the number of bits in a word, which is the largest allowed size of the bits argument to read and write. This will be either 32 or 64.
bits
These methods are only valid while the stream is in reading state.
Moves the position to the stream beginning.
Returns true is the stream is at the end. Rarely used.
Reads $bits from the stream and returns the value. $bits must be between 1 and maxbits.
$bits
1
maxbits
The position is advanced unless the second argument is the string 'readahead'.
Note for implementations: You have to implement this.
Advances the position $bits bits. Used in conjunction with readahead.
readahead
Reads one or more values from the stream in 0000...1 unary coding. If $count is 1 or not supplied, a single value will be read. If $count is positive, that many values will be read. If $count is negative, values are read until the end of the stream.
0000...1
$count
In list context this returns a list of all values read. In scalar context it returns the last value read.
Note for implementations: You should have efficient code for this.
Like get_unary, but using 1111...0 unary coding. Less common.
get_unary
1111...0
Reads one or more values from the stream as fixed-length binary numbers, each using $bits bits. The treatment of count and return values is identical to get_unary.
Reads $bits bits from the stream and returns them as a binary string, such as '0011011'.
These methods are only valid while the stream is in writing state.
Writes $value to the stream using $bits bits. $bits must be between 1 and maxbits, unless value is 0 or 1, in which case bits may be larger than maxbits.
$value
value
The stream length will be increased by $bits bits. Regardless of the contents of $value, exactly $bits bits will be used. If $value has more non-zero bits than $bits, the lower bits are written. In other words, $value will be masked before writing.
Writes the values to the stream in 0000...1 unary coding. Unary coding is only appropriate for relatively small numbers, as it uses $value + 1 bits.
$value + 1
Like put_unary, but using 1111...0 unary coding. Less common.
put_unary
Writes the values to the stream as fixed-length binary values. This is just a loop inserting each value with write($bits, $value).
write($bits, $value)
Takes one or more binary strings, such as '1001101', '001100', etc. and writes them to the stream. The number of bits used for each value is equal to the string length.
Writes the contents of $source_stream to the stream. This is a helper method that might be more efficient than doing it in one of the many other possible ways. The default implementation uses:
$source_stream
$self->put_string( $source_stream->to_string );
These methods may be called at any time, and will adjust the state of the stream.
Returns the stream as a binary string, e.g. '00110101'.
Returns the stream as packed big-endian data. This form is portable to any other implementation on any architecture.
Returns the stream as some scalar holding the data in some implementation specific way. This may be portable or not, but it can always be read by the same implementation. It might be more efficient than the raw format.
The stream will be set to the binary string $string.
$string
The stream is set to the packed big-endian vector $packed which has $bits bits of data. If $bits is not present, then length($packed) will be used as the byte-length. It is recommended that you include $bits.
$packed
length($packed)
Similar to from_raw, but using the value returned by to_store.
from_raw
to_store
Erases all the data, while the writing state is left unchanged. The position and length will both be 0 after this is finished.
Note for implementations: You need an 'after' method to actually erase the data.
Changes the state to writing with no other API-visible changes.
Changes the state to reading, and the position is set to the end of the stream. No other API-visible changes happen.
A helper function that performs erase followed by write_open.
A helper function that performs write_close followed by rewind.
Dana Jacobsen <dana@acm.org>
Copyright 2011 by Dana Jacobsen <dana@acm.org>
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Data::BitStream, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::BitStream
CPAN shell
perl -MCPAN -e shell install Data::BitStream
For more information on module installation, please visit the detailed CPAN module installation guide.