buffer-0.5.3: Simple mutable low-level buffer for IO

Safe HaskellNone
LanguageHaskell2010

Buffer

Contents

Synopsis

Documentation

data Buffer Source #

Mutable buffer.

new :: Int -> IO Buffer Source #

Create a new buffer of the specified initial capacity.

Pushing

push :: Buffer -> Int -> (Ptr Word8 -> IO (Int, result)) -> IO result Source #

Prepares the buffer to be filled with at maximum the specified amount of bytes, then uses the pointer-action to populate it. It is your responsibility to ensure that the action does not exceed the space limit.

The pointer-action returns the amount of bytes it actually writes to the buffer. That amount then is used to move the buffer's cursor accordingly. It can also produce some result, which will then be emitted by push.

It also aligns or grows the buffer if required.

pushBytes :: Buffer -> ByteString -> IO () Source #

Push a byte array into the buffer.

pushStorable :: Storable storable => Buffer -> storable -> IO () Source #

Push a storable value into the buffer.

Pulling

pull :: Buffer -> Int -> (Ptr Word8 -> IO result) -> (Int -> IO result) -> IO result Source #

Pulls the specified amount of bytes from the buffer using the provided pointer-action, freeing the buffer from the pulled bytes afterwards.

In case the buffer does not contain enough bytes yet, the second action is called instead, given the amount of required bytes missing. You should use that action to refill the buffer accordingly and pull again.

pullBytes :: Buffer -> Int -> (ByteString -> result) -> (Int -> IO result) -> IO result Source #

Pulls the specified amount of bytes, converting them into result, if the buffer contains that amount.

In case the buffer does not contain enough bytes yet, the second action is called instead, given the amount of required bytes missing. You should use that action to refill the buffer accordingly and pull again.

pullStorable :: Storable storable => Buffer -> (storable -> result) -> (Int -> IO result) -> IO result Source #

Pulls a storable value, converting it into result, if the buffer contains enough bytes.

In case the buffer does not contain enough bytes yet, the second action is called instead, given the amount of required bytes missing. You should use that action to refill the buffer accordingly and pull again.

Analysing

getBytes :: Buffer -> IO ByteString Source #

Create a bytestring representation without modifying the buffer.

getSpace :: Buffer -> IO Int Source #

Get how much space is occupied by the buffer's data.