binary-0.5.0.2: Binary serialisation for Haskell values using lazy ByteStrings

Portabilityportable to Hugs and GHC
Stabilityexperimental
MaintainerLennart Kolmodin <[email protected]>

Data.Binary.Builder

Contents

Description

Efficient construction of lazy bytestrings.

Synopsis

The Builder type

data Builder Source

A Builder is an efficient way to build lazy ByteStrings. There are several functions for constructing Builders, but only one to inspect them: to extract any data, you have to turn them into lazy ByteStrings using toLazyByteString.

Internally, a Builder constructs a lazy L.Bytestring by filling byte arrays piece by piece. As each buffer is filled, it is 'popped' off, to become a new chunk of the resulting lazy ByteString. All this is hidden from the user of the Builder.

Instances

toLazyByteString :: Builder -> ByteStringSource

O(n). Extract a lazy ByteString from a Builder. The construction work takes place if and when the relevant part of the lazy ByteString is demanded.

Constructing Builders

empty :: BuilderSource

O(1). The empty Builder, satisfying

singleton :: Word8 -> BuilderSource

O(1). A Builder taking a single byte, satisfying

append :: Builder -> Builder -> BuilderSource

O(1). The concatenation of two Builders, an associative operation with identity empty, satisfying

fromByteString :: ByteString -> BuilderSource

O(1). A Builder taking a ByteString, satisfying

fromLazyByteString :: ByteString -> BuilderSource

O(1). A Builder taking a lazy ByteString, satisfying

Flushing the buffer state

flush :: BuilderSource

O(1). Pop the ByteString we have constructed so far, if any, yielding a new chunk in the result lazy ByteString.

Derived Builders

Big-endian writes

putWord16be :: Word16 -> BuilderSource

Write a Word16 in big endian format

putWord32be :: Word32 -> BuilderSource

Write a Word32 in big endian format

putWord64be :: Word64 -> BuilderSource

Write a Word64 in big endian format

Little-endian writes

putWord16le :: Word16 -> BuilderSource

Write a Word16 in little endian format

putWord32le :: Word32 -> BuilderSource

Write a Word32 in little endian format

putWord64le :: Word64 -> BuilderSource

Write a Word64 in little endian format

Host-endian, unaligned writes

putWordhost :: Word -> BuilderSource

O(1). A Builder taking a single native machine word. The word is written in host order, host endian form, for the machine you're on. On a 64 bit machine the Word is an 8 byte value, on a 32 bit machine, 4 bytes. Values written this way are not portable to different endian or word sized machines, without conversion.

putWord16host :: Word16 -> BuilderSource

Write a Word16 in native host order and host endianness. 2 bytes will be written, unaligned.

putWord32host :: Word32 -> BuilderSource

Write a Word32 in native host order and host endianness. 4 bytes will be written, unaligned.

putWord64host :: Word64 -> BuilderSource

Write a Word64 in native host order. On a 32 bit machine we write two host order Word32s, in big endian form. 8 bytes will be written, unaligned.

Unicode

putCharUtf8 :: Char -> BuilderSource

Write a character using UTF-8 encoding.