* any integral value can be saved with explicitly specified size using functions "putBits bh bits value" and "getBits bh bits" (their shortcuts putBit/putWord8...putWord64/getBit/... is also supported for all integral types)

- any integral value can be saved with explicitly specified size using functions "putBits bh bits value" and "getBits bh bits" (their shortcuts putBit/putWord8...putWord64/getBit/... is also supported for all integral types)

any integral value can be saved with explicitly specified size using functions "putBits bh bits value" and "getBits bh bits" (their shortcuts putBit/putWord8...putWord64/getBit/... is also supported for all integral types)

1 First attempt: ByteStream

ByteStream interface provides primitive byte I/O operations - vGetByte
and vPutByte. On top of this, all binary I/O and serialization
facilities are built. As the result, you can use these facilities on
any ByteStream, ranging from the file to string buffer.

Please note that all binary I/O operations come in pairs with names
putXXX and getXXX. I will introduce only "putXXX" operations, you
should just know that each operation has its "getXXX" twin.

First, most ground-close layer includes operations putWord16le,
putWord32le, putWord64le and putWord16be, putWord32be, putWord64be.
Together with the vPutByte operation they allow to write low-endian
and big-endian values of any size and even mix low-endian and
big-endian representations:

All these operations work fine not only with Int, but with any
integral type (Word, Integer, Int8, Word64 and so on). This allows you
to read/write any integral value without explicit type conversion. On
the other side, if you write literal constants using these functions,
you will need to its type (say, Int) explicitly.

2 Second attempt

In AltBinary library there are 4 methods of binary I/O builded on top of
each other:

2.1 Byte I/O

Lowest level, the byte I/O, isn't differ significantly from the Char I/O.
All Streams support vGetByte and vPutByte operations, either directly
or via buffering transformer. These operations has rather generalized
types:

This allows to read/write any integral and enumeration values without
additional type conversions (of course, these values should belong to
the 0..255 range)

Together with other Stream operations, such as vIsEOF, vTell/vSeek,
vGetBuf/vPutBuf, this allows to write any programs that operate upon
binary data. You can freely mix byte and text I/O on one Stream:

main = do vPutByte stdout (1::Int)

vPutStrLn stdout "text"
vPutBuf stdout buf bufsize

2.2 Integral values / bit sequences I/O

The core of this API is two generalized operations:

getBits bits h
putBits bits h value

`getBits` reads certain number of bits from given BinaryStream and
returns it as value of any integral type (Int, Word8, Integer and so on).
`putBits` writes given value as a certain number of bits. The `value`,
again, may be of any integral type.

These two operations can be implemented in one of 4 ways, depending on
the answers on two questions:
- whether integral values written as big- or little-endian?
- whether values written are bit-aligned or byte-aligned?

The library allows you to select any answers on these questions. The
`h` parameter in this operation represents BinaryStream and there are
4 methods to open BinaryStream on top of plain Stream:

Moreover, to simplify your work, Stream by itself can also be used as
BinaryStream - in this case byte-aligned big-endian representation used.
So, you can write, for example:

putBits 16 stdout (0::Int)

or

bh <- openByteAlignedLE stdout
putBits 16 bh (0::Int)

There is also operation `flushBits h` what aligns BinaryStream on the
byte boundary. It fills the rest of pyte with zero bits on output and
skip the rest of bits in current bytes on input. Of course, this
operation does nothing on byte-aligned BinaryStreams.

There are also "shortcut" operations what read/write some number of bits:

Although these operations seems like just shortcuts for partial
application of getBits/putBits, they are works somewhat faster.
In contrast to other binary I/O libraries, each of these operations
can accept/return values of any integral type.

You can freely mix text I/O, byte I/O and bits I/O as long as you
don't forget to make `flushBits` after bit-aligned chunks of I/O:

When you request to write, say, 15 bits to byte-aligned BinaryStream,
the whole number of bytes are written. In particular, each `putBit`
operation on byte-aligned BinaryStream writes the whole byte to the
stream while the same operation on bit-aligned streams fills one bit at
a time.

But that is not yet the whole story! There are also operations that
allow to intermix little-endian and big-endian I/O:

Please note that `h` in these operations is a Stream, not
BinaryStream. Actually, these operations just perform several fixed
vGetByte or vPutByte operations and, strictly speaking, they should be
noted in previous section.

There are also combinator versions of `open*` operations, that
automatically perform `flushBits` at the finish:

2.3 Serialization API

where `h` is a BinaryStream. These operations read and write binary
representation of any value belonging to the class Binary.

3 Third attempt

3.1 Emulation of Binary interface

This library implements 2 interfaces: Binary and AltBinary. First
interface allows to use this library as drop-in replacement for the
well-known Binary and NewBinary libs. all you need to do is to replace
"import Data.Binary" statement with either

import Data.Binary.ByteAligned

or

import Data.Binary.BitAligned

depending on what type of access you need. in the first case
representation of any data value will be written/read as the whole
number of bytes, in the second case data values may cross byte
boundaries and, for example, Bools will be packed 8 values per byte.
please draw attention that despite interface emulation this library
and original Binary lib use different representations for most of the
data types

3.2 AltBinary interface

3.2.1 Types of binary streams: bit/byte-aligned, low/big-endian

AltBinary is "native" interface of this library to (de)serialize data.
it provides the same operations `get` and `put_` to read/write data,
but allows to use them directly on Handles and any other streams:

import Data.AltBinary

h <- openBinaryFile "test" WriteMode
put_ h [1..100::Int]
hClose h

h <- openBinaryFile "test" ReadMode
x <- get h :: IO [Int]
print x

if you need bit-aligned serialization, use the `openBitAligned` stream
transformer:

3.2.2 getBits/putBits; Binary instances for Bool, Maybe, Either

`get` and `put_` operations are just enough if you need only to save
some values in Stream and then restore them. but to assemble/parse
data in some particular format, you will need some more low-level
functions, such as `getBits` and `putBits`, which transfers just the
specified number of bits:

putBits 32 h (123::Int)
x <- getBits 32 h :: IO Int

if you call on byte-aligned stream putBits with number of bits, what
is not divisible by 8, the whole number of bytes are occupied. in
particular, putBit on byte-aligned streams occupies entire byte

this makes possible to use the same (de)serialization code and in
particular the same definitions of Binary instances both for
byte-aligned and bit-aligned streams! for example, the following
definition:

as a result, representation of `Maybe a` uses just one more bit than
representation of type `a` in bit-aligned streams, and whole extra
byte otherwise. the same story is for Either types

3.2.3 getWord8..putWord64; Binary instances for Int8..Word64

most widespread uses of getBits/putBits is for 1/8/16/32/64 bits, and
so there are specialized (and sometimes more efficient) versions of
these functions, called putBit, putWord8...putWord64 (and of course
their get... counterparts). please draw attention that all these
functions accept arguments (or return values) of any Integral type
(i.e. types what are instances of Integral class - Int, Integer, Word,
Int8..Word64), so you don't need to convert types if you want, for
example, encode Int as 8-bit value:

putWord8 h (length "test")

these fixed-bits routines used in definitions of Binary instances for
types with fixed sizes - Int8...Word64. types Int, Word and Integer
by default uses variable-sized representation, which would be
described later. if you need to read or write values of these types
using fixed-size representation, use appropriate fixed-bits procedures
instead of get/put_:

putWord16 h (1::Int)
putWord32 h (2::Word)
putWord64 h (3::Integer)

the same rule applies if you need to write fixed-size value with
non-default number of bits:

putWord8 h (4::Int32)

functions putWord16..putWord64 uses big-endian representation, also
known as network byte order - it is the order of bytes, used natively
on PowerPC/Sparc processors. in this format, representation of value
started fom most significant bytes. if you use bit-aligned stream,
high bits of each byte are also filled first. if you need
little-endian (native for Intel processors) formats,
putWord16le..putWord64le is at your service

3.2.4 putBounded; Binary instances for Bounded Enum types

next pair of functions uses mininal possible number of bits to encode
values in given range [min..max]:

putBounded min max h x
x <- getBounded min max h

they also support values of any Integral type. These functions are used
to provide default Binary instances for all Bounded Enum types (i.e.
types which support both Bounded and Enum interfaces). for example,
you can declare:

data Color = Red | Green | Blue deriving (Bounded, Enum)

and now you can use get/put_ on Colors; Color values would be encoded
using 2 bits in bit-aligned streams (of course, whole byte would be
used in byte-aligned streams)

putUnsigned provides variable-sized encoding, what can be used to
represent any non-negative Integral value using minimal possible
number of bytes. it uses 7+1 encoding, i.e. 7 bits in each byte
represents bits of actual value, and higher bit used to distinguish
last byte in sequence. so, values in range 0..127 would be encoded
using one byte, values in range 128..2^14-1 - using two bytes and so on

putInteger is about the same, but allows to encode also negative
values, so -64..63 encoded with one byte, -2^13..2^13-1 - with two
bytes...

putLength is synonym for putUnsigned, just used to represent lengths
of various containers - strings, lists, arrays and so on

put_ uses putInteger to encode Int and Integer, and putUnsigned to
encode Word; i don't used fixed-size representation for Int and Word
because that will produce data incompatible between 32-bit and 64-bit
platforms. i also don't use internal GHC's representation of Integer
to speed up (de)serialization because that will produce data
incompatible with other Haskell compilers. but if you need to
(de)serialize large number of Integers quickly, you should use
putGhcInteger/getGhcInteger procedures, described later. of course,
this way your program will become compatible only with the GHC
compiler.

3.2.6 Binary instances for Char and String (unwritten)

3.2.7 Lists support (unwritten)

3.2.8 Arrays support

This library supports (de)serialization for all array types, included
in standard hierarchical libraries plus PArr arrays, supported only by
GHC. Immutable array types can be (de)serialized to any Stream (just
like lists); mutable arrays can be (de)serialized only in the
corresponding monad (where this array can be read/modified), i.e.
IOArray can be get/put only to Stream belonging to IO monad, STArray
can be get/put only to Stream belonging to the same state monad. all
that is done automatically, just use put_ or get operation on the
corresponding array

if you read an array, you may need (or don't need, depending on the
surrounding code) to specify its type explicitly, say:

arr <- get h :: IO (Array Int Int32)

besides of automatic support for all array types in put_/get
operations, there are also huge number of "low-level" array
(de)serialization routines. first, there are routines

putIArray h arr
putMArray h arr

what can be used to write to the Stream any array that is instance of
IArray or MArray class, correspondingly (the first class contains all
immutable arrays: Array, UArray, DiffArray, DiffUArray; the second -
all other, mutable arrays - IOArray, IOUArray, STArray, STUArray,
StorableArray). corresponding operations to read these arrays require
to explicitly pass them bounds of array read:

arr <- getIArray h bounds
arr <- getMArray h bounds

note that this operations is not full analogues of put_/get ones, which
are write and read array bounds automatically. these operations are
more low-level - they are read/write only the array elements. also
note that just like `get` operation, you may need to specify type of
the array read:

arr <- getIArray h (0,9) :: IO (Array Int Int32)

second, you can read/write array elements with explicitly pointed
(de)serialization procedure for array elements isstead of default
ones, provided by the Binary class. to achive this, add `With` suffix
to routine name and specify procedure to read or write array elements
as the first argument:

of course, you can also provide your own read/write procedures, if
they have the same types as standard get/put_ functions.

there are also variants of all get operations, which uses `size` parameter
instead of `bounds`, and creates arrays with bounds (0,size-1::Int).
they have names with `N` at the end of of procedure name, but before
`With`:

at last, part of the `get` operations have versions, specialized to
specific type constructors. for example, `getMArrayN` have
`getIOArrayN` and `getIOUArrayN` variants which can read only the
IOArray/IOUArray, accordingly. it's just a trick to avoid necessity to
specify array types in `get` operations, say instead of:

arr <- getIArrayN h 10 :: IO (Array Int Int32)

one can write

arr <- getArrayN h 10

it is nothing more than handy shortcuts. the only exclusion is
operations to read `UArray`, what is not specializations of
corresponding `IArray` operations, but use some faster algorithm and
work only in IO monad. if you need to read `UArray` in any other monad
- please use general operations on the `IArray` instead (anyway the
compiler will ensure proper use via the typechecking)

so far i don't say anything about specific operations for
(de)serialization of parallel arrays (available only in GHC via
the module GHC.PArr).