A collection of utility functions for dealing with Enumerator
s.
- mapE :: (ByteString -> ByteString) -> Enumerator -> Enumerator
- toLBS :: Enumerator -> IO ByteString
- fromLBS :: ByteString -> Enumerator
- fromLBS' :: IO ByteString -> Enumerator
- toSource :: Enumerator -> IO Source
- fromHandle :: Handle -> Enumerator
- fromHandleFinally :: Handle -> IO a -> Enumerator
- fromFile :: FilePath -> Enumerator
- fromFileFinally :: FilePath -> IO a -> Enumerator
- fromTempFile :: FilePath -> Enumerator
- fromResponseBody :: ResponseBody -> Enumerator
Utilities
mapE :: (ByteString -> ByteString) -> Enumerator -> EnumeratorSource
Performs a specified conversion on each ByteString
output by an
enumerator.
Conversions
Lazy byte strings
toLBS :: Enumerator -> IO ByteStringSource
This uses unsafeInterleaveIO
to lazily read from an enumerator. All
normal lazy I/O warnings apply. In addition, since it is based on
toSource
, please observe all precautions for that function.
fromLBS :: ByteString -> EnumeratorSource
This function safely converts a lazy bytestring into an enumerator.
fromLBS' :: IO ByteString -> EnumeratorSource
Same as fromLBS
, but the lazy bytestring is in the IO monad. This allows
you to lazily read a file into memory, perform some mapping on the data and
convert it into an enumerator.
Source
toSource :: Enumerator -> IO SourceSource
This function uses another thread to convert an Enumerator
to a
Source
. In essence, this allows you to write code which "pulls" instead
of code which is pushed to. While this can be a useful technique, some
caveats apply:
- It will be more resource heavy than using the
Enumerator
directly. - You *must* consume all input. If you do not, then the other thread will be deadlocked.
Handle
fromHandle :: Handle -> EnumeratorSource
Read a chunk of data from the given Handle
at a time. We use
defaultChunkSize
from the bytestring package to determine the largest
chunk to take.
fromHandleFinally :: Handle -> IO a -> EnumeratorSource
Wrapper around fromHandle to perform an action after EOF or an exception.
FilePath
fromFile :: FilePath -> EnumeratorSource
A little wrapper around fromHandle
which first opens a file for reading.
fromFileFinally :: FilePath -> IO a -> EnumeratorSource
Wrapper around fromFile to perform an action after the file is closed.
fromTempFile :: FilePath -> EnumeratorSource
Enumerator to read and remove a file. Being based on fromFileFinally
, it
ensures the file is removed, even in the presence of exceptions.
fromResponseBody :: ResponseBody -> EnumeratorSource
Since the response body is defined as a ResponseBody
, this function
simply reduces the whole value to an enumerator. This can be convenient for
server implementations not optimizing file sending.