Buffering for output streams based on bytestring builders.
Buffering an output stream can often improve throughput by reducing the
number of system calls made through the file descriptor. The
package provides an efficient set of primitives for serializing values
directly to an output buffer.
(N.B.: most of the
blaze-builder package has been moved into
bytestring in versions >= 0.10; once two or three Haskell Platform
editions have been released that contain
bytestring 0.10 or higher, the
blaze-builder will be dropped in favor of the native support
Builder contained in the
Using this module
someOutputStream :: OutputStream ByteString
You create a new output stream wrapping the original one that accepts
do newStream <- Streams.
fromByteString"hello") newStream ....
You can flush the output buffer using
As a convention,
builderStream will write the empty string to the wrapped
OutputStream upon a builder buffer flush. Output streams which receive
ByteString should either ignore the empty string or interpret it as a
signal to flush their own buffers, as the System.IO.Streams.Zlib functions
example :: IO [ByteString] example = do let l1 =
intersperse" " ["the", "quick", "brown", "fox"] let l2 =
intersperse" " ["jumped", "over", "the"] let l = map
fromByteStringl1 ++ [
flush] ++ map
fromByteStringl2 is <- Streams.
fromListl (os0, grab) <- Streams.
listOutputStreamos <- Streams.
connectis os >> grab ghci> example ["the quick brown fox","","jumped over the"]
Blaze builder conversion
Unsafe variation on
builderStream that reuses an existing buffer for
NOTE: because the buffer is reused, subsequent
ByteString values written
to the wrapped
OutputString will cause previous yielded strings to change.
Do not retain references to these
ByteString values inside the
OutputStream you pass to this function, or you will violate referential
If you must retain copies of these values, then please use
copy to ensure that you have a fresh copy of the
You can create a Buffer with