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

Portabilityportable to Hugs and GHC.
Stabilityexperimental
MaintainerLennart Kolmodin <kolmodin@gmail.com>
Safe HaskellTrustworthy

Data.Binary.Get

Contents

Description

The Get monad. A monad for efficiently building structures from encoded lazy ByteStrings.

Synopsis

The Get type

The lazy input interface

The lazy interface consumes a single lazy bytestring. It's the easiest interface to get started with, but it has limitations. If the decoder runs into an error, it will throw an exception using error. It will also throw an error if the decoder runs out of input.

There is no way to provide more input other than the initial data. To be able to incrementally give more data, see the incremental input interface.

runGet :: Get a -> ByteString -> aSource

The simplest interface to run a Get decoder. If the decoder runs into an error, calling fail or running out of input, it will call error.

runGetState :: Get a -> ByteString -> Int64 -> (a, ByteString, Int64)Source

Deprecated: Use runGetPartial instead. This function will be removed.

DEPRECATED. Provides compatibility with previous versions of this library. Run a Get monad and return a tuple with thee values. The first value is the result of the decoder. The second and third are the unused input, and the number of consumed bytes.

The incremental input interface

The incremental interface consumes a strict ByteString at a time, each being part of the total amount of input. If your decoder needs more input to finish it will return a Partial with a continuation. If there is no more input, provide it Nothing.

data Decoder a Source

A decoder procuced by running a Get monad.

Constructors

Fail !ByteString !Int64 String

The decoder ran into an error. The decoder either used fail or was not provided enough input.

Partial (Maybe ByteString -> Decoder a)

The decoder has consumed the available input and needs more to continue. Provide Just if more input is available and Nothing otherwise, and you will get a new Decoder.

Done !ByteString !Int64 a

The decoder has successfully finished. Except for the output value you also get the unused input as well as the count of used bytes.

runGetIncremental :: Get a -> Decoder aSource

Run a Get monad. See Decoder for what to do next, like providing input, handling decoder errors and to get the output value. Hint: Use the helper functions pushChunk, pushChunks and pushEndOfInput.

Providing input

pushChunk :: Decoder a -> ByteString -> Decoder aSource

Feed a Decoder with more input. If the Decoder is Done or Fail it will add the input to ByteString of unconsumed input.

    runGetPartial myParser `pushChunk` myInput1 `pushChunk` myInput2

pushChunks :: Decoder a -> ByteString -> Decoder aSource

Feed a Decoder with more input. If the Decoder is Done or Fail it -- will add the input to ByteString of unconsumed input.

    runGetPartial myParser `pushChunks` myLazyByteString

pushEndOfInput :: Decoder a -> Decoder aSource

Tell a Decoder that there is no more input. This passes Nothing to a Partial decoder, otherwise returns the decoder unchanged.

Parsing

skip :: Int -> Get ()Source

Skip ahead n bytes. Fails if fewer than n bytes are available.

isEmpty :: Get BoolSource

Test whether all input has been consumed, i.e. there are no remaining undecoded bytes.

bytesRead :: Get Int64Source

Get the total number of bytes read to this point.

ByteStrings

getByteString :: Int -> Get ByteStringSource

An efficient get method for strict ByteStrings. Fails if fewer than n bytes are left in the input. If n <= 0 then the empty string is returned.

getLazyByteString :: Int64 -> Get ByteStringSource

An efficient get method for lazy ByteStrings. Fails if fewer than n bytes are left in the input.

getLazyByteStringNul :: Get ByteStringSource

Get a lazy ByteString that is terminated with a NUL byte. The returned string does not contain the NUL byte. Fails if it reaches the end of input without finding a NUL.

getRemainingLazyByteString :: Get ByteStringSource

Get the remaining bytes as a lazy ByteString. Note that this can be an expensive function to use as it forces reading all input and keeping the string in-memory.

Decoding words

getWord8 :: Get Word8Source

Read a Word8 from the monad state

Big-endian decoding

getWord16be :: Get Word16Source

Read a Word16 in big endian format

getWord32be :: Get Word32Source

Read a Word32 in big endian format

getWord64be :: Get Word64Source

Read a Word64 in big endian format

Little-endian decoding

getWord16le :: Get Word16Source

Read a Word16 in little endian format

getWord32le :: Get Word32Source

Read a Word32 in little endian format

getWord64le :: Get Word64Source

Read a Word64 in little endian format

Host-endian, unaligned decoding

getWordhost :: Get WordSource

O(1). Read a single native machine word. The word is read 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.

getWord16host :: Get Word16Source

O(1). Read a 2 byte Word16 in native host order and host endianness.

getWord32host :: Get Word32Source

O(1). Read a Word32 in native host order and host endianness.

getWord64host :: Get Word64Source

O(1). Read a Word64 in native host order and host endianess.

Deprecated functions

remaining :: Get Int64Source

DEPRECATED. Get the number of bytes of remaining input. Note that this is an expensive function to use as in order to calculate how much input remains, all input has to be read and kept in-memory. The decoder keeps the input as a strict bytestring, so you are likely better off by calculating the remaining input in another way.

getBytes :: Int -> Get ByteStringSource

DEPRECATED. Same as getByteString.