HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulestore-0.7.20Haskell2010

System.IO.ByteBuffer

A ByteBuffer is a simple buffer for bytes. It supports two operations: refilling with the contents of a ByteString, and consuming a fixed number of bytes.

It is implemented as a pointer, together with counters that keep track of the offset and the number of bytes in the buffer. Note that the counters are simple IORefs, so ByteBuffers are not thread-safe!

A ByteBuffer is constructed by new with a given starting length, and will grow (by repeatedly multiplying its size by 1.5) whenever it is being fed a ByteString that is too large.

  • 2 types
  • 10 values
  • Packagestore-0.7.20
  • Exports12
  • LanguageHaskell2010
  • LicenceMIT
  • SourceByteBuffer.hs

Allocation and Deallocation

3 declarations
valuenew
  1. :: MonadIO m
  2. => Maybe Int

    Size of buffer to allocate. If Nothing, use the default value of 4MB

  3. -> m ByteBuffer

    The byte buffer.

#

Allocates a new ByteBuffer with a given buffer size filling from the given FillBuffer.

Note that ByteBuffers created with new have to be deallocated explicitly using free. For automatic deallocation, consider using with instead.

Query for number of available bytes

3 declarations

Feeding new input

2 declarations
valuefillFromFd :: (MonadIO m, MonadFail m) => ByteBuffer -> Fd -> Int -> m Int
#

Will read at most n bytes from the given Fd, in a non-blocking fashion. This function is intended to be used with non-blocking Sockets, such the ones created by the network package.

Returns how many bytes could be read non-blockingly.

Consuming bytes from the buffer

2 declarations
valueunsafeConsume
  1. :: MonadIO m
  2. => ByteBuffer
  3. -> Int

    n

  4. -> m (Either Int (Ptr Word8))

    Will be Left missing when there are only n-missing bytes left in the ByteBuffer.

#

Try to get a pointer to n bytes from the ByteBuffer.

Note that the pointer should be used before any other actions are performed on the ByteBuffer. It points to some address within the buffer, so operations such as enlarging the buffer or feeding it new data will change the data the pointer points to. This is why this function is called unsafe.

Exceptions

1 declaration
datadata ByteBufferException
#

Exception that is thrown when an invalid ByteBuffer is being used that is no longer valid.

A ByteBuffer is considered to be invalid if

  • it has explicitly been freed

  • an Exception has occured during an operation that modified it

Constructors

Instances3Eq, Show, Exception