NAME

Data::BitStream::Base - A Role implementing the API for Data::BitStream

SYNOPSIS

use Mouse;
with 'Data::BitStream::Base';

DESCRIPTION

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.

DATA

pos

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.

len

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.

writing

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.

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.

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.

CLASS METHODS

maxbits

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.

OBJECT METHODS (reading)

These methods are only value while the stream is in reading state.

rewind

Moves the position to the stream beginning.

exhausted

Returns true is the stream is at the end. Rarely used.

read($bits [, 'readahead'])

Reads $bits from the stream and returns the value. $bits must be between 1 and maxbits.

The position is advanced unless the second argument is the string 'readahead'.

Note for implementations: You have to implement this.

skip($bits)

Advances the position $bits bits. Used in conjunction with readahead.

get_unary([$count])

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.

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.

get_unary1([$count])

Like get_unary, but using 1111...0 unary coding. Less common.

get_binword($bits, [$count])

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.

read_string($bits)

Reads $bits bits from the stream and returns them as a binary string, such as '0011011'.

OBJECT METHODS (writing)

These methods are only value while the stream is in writing state.

write($bits, $value)

Writes $value to the stream using $bits bits. $bits must be between 1 and maxbits.

The length is 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.

Note for implementations: You have to implement this.

put_unary(@values)

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.

Note for implementations: You should have efficient code for this.

put_unary1(@values)

Like put_unary, but using 1111...0 unary coding. Less common.

put_binword($bits, @values)

Writes the values to the stream as fixed-length binary values. This is just a loop inserting each value with write($bits, $value).

put_string(@strings)

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.

put_stream($source_stream)

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:

$self->put_string( $source_stream->to_string );

OBJECT METHODS (conversion)

These methods may be called at any time, and will adjust the state of the stream.

to_string

Returns the stream as a binary string, e.g. '00110101'.

to_raw

Returns the stream as packed big-endian data. This form is portable to any other implementation on any architecture.

to_store

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.

from_string($string)

The stream will be set to the binary string $string.

from_raw($packed [, $bits])

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.

from_store($blob [, $bits])

Similar to from_raw, but using the value returned by to_store.

OBJECT METHODS (other)

erase

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.

write_open

Changes the state to writing with no other API-visible changes.

write_close

Changes the state to reading, and the position is set to the end of the stream. No other API-visible changes happen.

erase_for_write

A helper function that performs erase followed by write_open.

rewind_for_read

A helper function that performs write_close followed by rewind.

SEE ALSO

Data::BitStream
Data::BitStream::String
Data::BitStream::WordVec

AUTHORS

Dana Jacobsen <dana@acm.org>

COPYRIGHT

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.