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

Modulestreamly-core-0.2.2Haskell2010

Streamly.Internal.FileSystem.Handle

The fundamental singleton IO APIs are getChunk and putChunk and the fundamental stream IO APIs built on top of those are readChunksWith and writeChunks. Rest of this module is just combinatorial programming using these.

We can achieve line buffering by folding lines in the input stream into a stream of arrays using Stream.splitOn or Fold.takeEndBy_ and similar operations. One can wrap the input stream in Maybe type and then use writeMaybesWith to achieve user controlled buffering.

  • 28 values

Setup

0 declarations

To execute the code examples provided in this module in ghci, please run the following commands first.

Example6 expressions
:mimport qualified Streamly.Data.Array as Arrayimport qualified Streamly.FileSystem.Handle as Handle hiding (readChunks)import qualified Streamly.Data.Fold as Foldimport qualified Streamly.Data.Stream as Streamimport qualified Streamly.Data.Unfold as Unfold

For APIs that have not been released yet.

Example4 expressions
import qualified Streamly.Internal.Data.Array as Array (unsafeCreateOf)import qualified Streamly.Internal.Data.Unfold as Unfold (first)import qualified Streamly.Internal.FileSystem.Handle as Handleimport qualified Streamly.Internal.System.IO as IO (defaultChunkSize)

Singleton APIs

3 declarations
valuegetChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
#

Read a ByteArray consisting of one or more bytes from a file handle. If no data is available on the handle it blocks until at least one byte becomes available. If any data is available then it immediately returns that data without blocking. As a result of this behavior, it may read less than or equal to the size requested.

Streams

4 declarations
valueread :: MonadIO m => Handle -> Stream m Word8
#

Generate a byte stream from a file Handle.

Example1 expression
read h = Stream.unfoldMany Array.reader $ Handle.readChunks h

Pre-release

valuereadWith :: MonadIO m => Int -> Handle -> Stream m Word8
#

readWith bufsize handle reads a byte stream from a file handle, reads are performed in chunks of up to bufsize.

Example1 expression
readWith size h = Stream.unfoldMany Array.reader $ Handle.readChunksWith size h

Pre-release

valuereadChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
#

readChunksWith size handle reads a stream of arrays from the file handle handle. The maximum size of a single array is limited to size. The actual size read may be less than or equal to size.

Example1 expression
readChunksWith size h = Stream.unfold Handle.chunkReaderWith (size, h)
valuereadChunks :: MonadIO m => Handle -> Stream m (Array Word8)
#

getChunks handle reads a stream of arrays from the specified file handle. The maximum size of a single array is limited to defaultChunkSize. The actual size read may be less than or equal to defaultChunkSize.

Example1 expression
readChunks = Handle.readChunksWith IO.defaultChunkSize

Pre-release

Unfolds

4 declarations
valuereader :: MonadIO m => Unfold m Handle Word8
#

Unfolds a file handle into a byte stream. IO requests to the device are performed in sizes of Streamly.Internal.Data.Array.Type.defaultChunkSize.

Example1 expression
reader = Unfold.many Array.reader Handle.chunkReader
valuereaderWith :: MonadIO m => Unfold m (Int, Handle) Word8
#

Unfolds the tuple (bufsize, handle) into a byte stream, read requests to the IO device are performed using buffers of bufsize.

Example1 expression
readerWith = Unfold.many Array.reader Handle.chunkReaderWith

Unfolds a handle into a stream of Word8 arrays. Requests to the IO device are performed using a buffer of size Streamly.Internal.Data.Array.Type.defaultChunkSize. The size of arrays in the resulting stream are therefore less than or equal to Streamly.Internal.Data.Array.Type.defaultChunkSize.

Example1 expression
chunkReader = Unfold.first IO.defaultChunkSize Handle.chunkReaderWith

Unfold the tuple (bufsize, handle) into a stream of Word8 arrays. Read requests to the IO device are performed using a buffer of size bufsize. The size of an array in the resulting stream is always less than or equal to bufsize.

Folds

5 declarations
valuewrite :: MonadIO m => Handle -> Fold m Word8 ()
#

Write a byte stream to a file handle. Accumulates the input in chunks of up to Streamly.Internal.Data.Array.Type.defaultChunkSize before writing to the IO device.

Example1 expression
write = Handle.writeWith IO.defaultChunkSize
valuewriteWith :: MonadIO m => Int -> Handle -> Fold m Word8 ()
#

writeWith reqSize handle writes the input stream to handle. Bytes in the input stream are collected into a buffer until we have a chunk of reqSize and then written to the IO device.

Example1 expression
writeWith n h = Fold.groupsOf n (Array.unsafeCreateOf n) (Handle.writeChunks h)
valuewriteChunks :: MonadIO m => Handle -> Fold m (Array a) ()
#

Write a stream of arrays to a handle. Each array in the stream is written to the device as a separate IO request.

writeChunks h = Fold.drainBy (Handle.putChunk h)

valuewriteChunksWith
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Handle
  4. -> Fold m (Array a) ()
#

writeChunksWith bufsize handle writes a stream of arrays to handle after coalescing the adjacent arrays in chunks of bufsize. We never split an array, if a single array is bigger than the specified size it emitted as it is. Multiple arrays are coalesed as long as the total size remains below the specified size.

valuewriteMaybesWith :: MonadIO m => Int -> Handle -> Fold m (Maybe Word8) ()
#

Write a stream of Maybe values. Keep buffering the just values in an array until a Nothing is encountered or the buffer size exceeds the specified limit, at that point flush the buffer to the handle.

Pre-release

Refolds

3 declarations

Stream writes

4 declarations
valueputBytes :: MonadIO m => Handle -> Stream m Word8 -> m ()
#

Write a byte stream to a file handle. Accumulates the input in chunks of up to Streamly.Internal.Data.Array.Type.defaultChunkSize before writing.

NOTE: This may perform better than the write fold, you can try this if you need some extra perf boost.

Example1 expression
putBytes = Handle.putBytesWith IO.defaultChunkSize
valueputBytesWith :: MonadIO m => Int -> Handle -> Stream m Word8 -> m ()
#

putBytesWith bufsize handle stream writes stream to handle in chunks of bufsize. A write is performed to the IO device as soon as we collect the required input size.

valueputChunksWith
  1. :: (MonadIO m, Unbox a)
  2. => Int
  3. -> Handle
  4. -> Stream m (Array a)
  5. -> m ()
#

putChunksWith bufsize handle stream writes a stream of arrays to handle after coalescing the adjacent arrays in chunks of bufsize. The chunk size is only a maximum and the actual writes could be smaller as we do not split the arrays to fit exactly to the specified size.

valueputChunks :: MonadIO m => Handle -> Stream m (Array a) -> m ()
#

Write a stream of arrays to a handle.

Example1 expression
putChunks h = Stream.fold (Fold.drainBy (Handle.putChunk h))

Random Access (Seek)

1 declaration

Unlike the streaming APIs listed above, these APIs apply to devices or files that have random access or seek capability. This type of devices include disks, files, memory devices and exclude terminals, pipes, sockets and fifos.

Deprecated

4 declarations