HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleio-streams-1.5.2.2Haskell2010

System.IO.Streams.ByteString

Stream operations on ByteString.

  • 6 types
  • 20 values
  • Packageio-streams-1.5.2.2
  • Exports26
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceByteString.hs

Counting bytes

2 declarations

Wraps an InputStream, counting the number of bytes produced by the stream as a side effect. Produces a new InputStream as well as an IO action to retrieve the count of bytes produced.

Strings pushed back to the returned InputStream will be pushed back to the original stream, and the count of produced bytes will be subtracted accordingly.

Example:

ghci> is <- Streams.System.IO.Streams.fromList ["abc", "def", "ghi"::ByteString]
ghci> (is', getCount) <- Streams.countInput is
ghci> Streams.read is'
Just "abc"
ghci> getCount
3
ghci> Streams.unRead "bc" is'
ghci> getCount
1
ghci> Streams.System.IO.Streams.peek is
Just "bc"
ghci> Streams.System.IO.Streams.toList is'
["bc","def","ghi"]
ghci> getCount
9

Wraps an OutputStream, counting the number of bytes consumed by the stream as a side effect. Produces a new OutputStream as well as an IO action to retrieve the count of bytes consumed.

Example:

ghci> (os :: OutputStream ByteString, getList) <- Streams.System.IO.Streams.listOutputStream
ghci> (os', getCount) <- Streams.countOutput os
ghci> Streams.System.IO.Streams.fromList ["abc", "def", "ghi"] >>= Streams.System.IO.Streams.connectTo os'
ghci> getList
["abc","def","ghi"]
ghci> getCount
9

Treating strings as streams

2 declarations

Input and output

3 declarations
valuetakeBytesWhile
  1. :: (Char -> Bool)

    predicate

  2. -> InputStream ByteString

    input stream

  3. -> IO (Maybe ByteString)
#

Takes from a stream until the given predicate is no longer satisfied. Returns Nothing on end-of-stream, or Just "" if the predicate is never satisfied. See takeWhile and takeWhile.

Example:

ghci> Streams.System.IO.Streams.fromList ["Hello, world!"] >>= Streams.takeBytesWhile (/= ',')
Just "Hello"
ghci> import Data.Char
ghci> Streams.System.IO.Streams.fromList ["7 Samurai"] >>= Streams.takeBytesWhile isAlpha
Just ""
ghci> Streams.System.IO.Streams.fromList [] >>= Streams.takeBytesWhile isAlpha
Nothing

Stream transformers

0 declarations

Splitting/Joining

valuesplitOn
  1. :: (Char -> Bool)

    predicate used to break the input stream into chunks

  2. -> InputStream ByteString

    input stream

  3. -> IO (InputStream ByteString)
#

Splits an InputStream over ByteStrings using a delimiter predicate.

Note that:

  • data pushed back with unRead is *not* propagated upstream here.

  • the resulting InputStream may hold an unbounded amount of the bytestring in memory waiting for the function to return true, so this function should not be used in unsafe contexts.

  • the delimiter is NOT included in the output.

  • consecutive delimiters are not merged.

  • if the input ends in the delimiter, a final empty string is not emitted. (/Since: 1.5.0.0. Previous versions had the opposite behaviour, which was changed to match Prelude.lines./)

Example:

ghci> Streams.System.IO.Streams.fromList ["the quick br", "own  fox"::ByteString] >>=
      Streams.splitOn (== ' ') >>= Streams.System.IO.Streams.toList
["the","quick","brown","","fox"]

Other

valuegiveBytes
  1. :: Int64

    maximum number of bytes to send to the wrapped stream

  2. -> OutputStream ByteString

    output stream to wrap

  3. -> IO (OutputStream ByteString)
#

Wraps an OutputStream, producing a new stream that will pass along at most n bytes to the wrapped stream, throwing any subsequent input away.

Example:

ghci> (os :: OutputStream ByteString, getList) <- Streams.System.IO.Streams.listOutputStream
ghci> os' <- Streams.giveBytes 6 os
ghci> Streams.System.IO.Streams.fromList ["long ", "string"] >>= Streams.System.IO.Streams.connectTo os'
ghci> getList
["long ","s"]

Wraps an OutputStream, producing a new stream that will pass along exactly n bytes to the wrapped stream. If the stream is sent more or fewer than the given number of bytes, the resulting stream will throw an exception (either TooFewBytesWrittenException or TooManyBytesWrittenException) during a call to write.

Example:

ghci> is <- Streams.System.IO.Streams.fromList ["ok"]
ghci> Streams.System.IO.Streams.outputToList (Streams.giveExactly 2 >=> Streams.System.IO.Streams.connect is)
["ok"]
ghci> is <- Streams.System.IO.Streams.fromList ["ok"]
ghci> Streams.System.IO.Streams.outputToList (Streams.giveExactly 1 >=> Streams.System.IO.Streams.connect is)
*** Exception: Too many bytes written
ghci> is <- Streams.System.IO.Streams.fromList ["ok"]
ghci> Streams.System.IO.Streams.outputToList (Streams.giveExactly 3 >=> Streams.System.IO.Streams.connect is)
*** Exception: Too few bytes written
valuetakeBytes
  1. :: Int64

    maximum number of bytes to read

  2. -> InputStream ByteString

    input stream to wrap

  3. -> IO (InputStream ByteString)
#

Wraps an InputStream, producing a new InputStream that will produce at most n bytes, subsequently yielding end-of-stream forever.

Strings pushed back to the returned InputStream will be propagated upstream, modifying the count of taken bytes accordingly.

Example:

ghci> is <- Streams.System.IO.Streams.fromList ["truncated", " string"::ByteString]
ghci> is' <- Streams.takeBytes 9 is
ghci> Streams.read is'
Just "truncated"
ghci> Streams.read is'
Nothing
ghci> Streams.System.IO.Streams.peek is
Just " string"
ghci> Streams.unRead "cated" is'
ghci> Streams.System.IO.Streams.peek is
Just "cated"
ghci> Streams.System.IO.Streams.peek is'
Just "cated"
ghci> Streams.read is'
Just "cated"
ghci> Streams.read is'
Nothing
ghci> Streams.read is
Just " string"
valuethrowIfConsumesMoreThan
  1. :: Int64

    maximum number of bytes to send to the wrapped stream

  2. -> OutputStream ByteString

    output stream to wrap

  3. -> IO (OutputStream ByteString)
#

Wraps an OutputStream, producing a new stream that will pass along at most n bytes to the wrapped stream. If more than n bytes are sent to the outer stream, a TooManyBytesWrittenException will be thrown.

Note: if more than n bytes are sent to the outer stream, throwIfConsumesMoreThan will not necessarily send the first n bytes through to the wrapped stream before throwing the exception.

Example:

ghci> (os :: OutputStream ByteString, getList) <- Streams.System.IO.Streams.listOutputStream
ghci> os' <- Streams.throwIfConsumesMoreThan 5 os
ghci> Streams.System.IO.Streams.fromList ["short"] >>= Streams.System.IO.Streams.connectTo os'
ghci> getList
["short"]
ghci> os'' <- Streams.throwIfConsumesMoreThan 5 os
ghci> Streams.System.IO.Streams.fromList ["long", "string"] >>= Streams.System.IO.Streams.connectTo os''
*** Exception: Too many bytes written
valuethrowIfProducesMoreThan
  1. :: Int64

    maximum number of bytes to read

  2. -> InputStream ByteString

    input stream

  3. -> IO (InputStream ByteString)
#

Wraps an InputStream. If more than n bytes are produced by this stream, read will throw a TooManyBytesReadException.

If a chunk yielded by the input stream would result in more than n bytes being produced, throwIfProducesMoreThan will cut the generated string such that exactly n bytes are yielded by the returned stream, and the subsequent read will throw an exception. Example:

ghci> is <- Streams.System.IO.Streams.fromList ["abc", "def", "ghi"] >>=
            Streams.throwIfProducesMoreThan 5
ghci> replicateM 2 (read is)
[Just "abc",Just "de"]
ghci> Streams.read is
*** Exception: Too many bytes read

Strings pushed back to the returned InputStream will be propagated upstream, modifying the count of taken bytes accordingly. Example:

ghci> is  <- Streams.System.IO.Streams.fromList ["abc", "def", "ghi"]
ghci> is' <- Streams.throwIfProducesMoreThan 5 is
ghci> Streams.read is'
Just "abc"
ghci> Streams.unRead "xyz" is'
ghci> Streams.System.IO.Streams.peek is
Just "xyz"
ghci> Streams.read is
Just "xyz"
ghci> Streams.read is
Just "de"
ghci> Streams.read is
*** Exception: Too many bytes read

Rate limiting

valuethrowIfTooSlow
  1. :: IO ()

    action to bump timeout

  2. -> Double

    minimum data rate, in bytes per second

  3. -> Int

    amount of time in seconds to wait before data rate calculation takes effect

  4. -> InputStream ByteString

    input stream

  5. -> IO (InputStream ByteString)
#

Rate-limits an input stream. If the input stream is not read from faster than the given rate, reading from the wrapped stream will throw a RateTooSlowException.

Strings pushed back to the returned InputStream will be propagated up to the original stream.

String search

2 declarations
valuesearch
  1. :: ByteString

    "needle" to look for

  2. -> InputStream ByteString

    input stream to wrap

  3. -> IO (InputStream MatchInfo)
#

Given a ByteString to look for (the "needle") and an InputStream, produces a new InputStream which yields data of type MatchInfo.

Example:

ghci> System.IO.Streams.fromList ["food", "oof", "oodles", "ok"] >>=
      search "foo" >>= System.IO.Streams.toList
[Match "foo",NoMatch "d",NoMatch "oo",Match "foo",NoMatch "dlesok"]

Uses the Boyer-Moore-Horspool algorithm (http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm).

Exception types

5 declarations