streamly-0.8.1: Dataflow programming and declarative concurrency
Copyright(c) 2019 Composewell Technologies
LicenseBSD3
Maintainerstreamly@composewell.com
Stabilitypre-release
PortabilityGHC
Safe HaskellNone
LanguageHaskell2010

Streamly.Internal.FileSystem.File

Description

Read and write streams and arrays to and from files specified by their paths in the file system. Unlike the handle based APIs which can have a read/write session consisting of multiple reads and writes to the handle, these APIs are one shot read or write APIs. These APIs open the file handle, perform the requested operation and close the handle. Thease are safer compared to the handle based APIs as there is no possibility of a file descriptor leakage.

import qualified Streamly.Internal.FileSystem.File as File
Synopsis

Streaming IO

Stream data to or from a file or device sequentially. When reading, the stream is lazy and generated on-demand as the consumer consumes it. Read IO requests to the IO device are performed in chunks limited to a maximum size of 32KiB, this is referred to as defaultChunkSize in the documentation. One IO request may or may not read the full chunk. If the whole stream is not consumed, it is possible that we may read slightly more from the IO device than what the consumer needed. Unless specified otherwise in the API, writes are collected into chunks of defaultChunkSize before they are written to the IO device.

File IO Using Handle

withFile :: (IsStream t, MonadCatch m, MonadAsync m) => FilePath -> IOMode -> (Handle -> t m a) -> t m a Source #

withFile name mode act opens a file using openFile and passes the resulting handle to the computation act. The handle will be closed on exit from withFile, whether by normal termination or by raising an exception. If closing the handle raises an exception, then this exception will be raised by withFile rather than any exception raised by act.

Pre-release

Read From File

readWithBufferOf :: (MonadCatch m, MonadAsync m) => Unfold m (Int, FilePath) Word8 Source #

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

Pre-release

read :: (MonadCatch m, MonadAsync m) => Unfold m FilePath Word8 Source #

Unfolds a file path into a byte stream. IO requests to the device are performed in sizes of defaultChunkSize.

Since: 0.7.0

toBytes :: (IsStream t, MonadCatch m, MonadAsync m) => FilePath -> t m Word8 Source #

Generate a stream of bytes from a file specified by path. The stream ends when EOF is encountered. File is locked using multiple reader and single writer locking mode.

Pre-release

readChunksWithBufferOf :: (MonadCatch m, MonadAsync m) => Unfold m (Int, FilePath) (Array Word8) Source #

Unfold the tuple (bufsize, filepath) 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.

Pre-release

readChunksFromToWith :: (MonadCatch m, MonadAsync m) => Unfold m (Int, Int, Int, FilePath) (Array Word8) Source #

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

Pre-release

readChunks :: (MonadCatch m, MonadAsync m) => Unfold m FilePath (Array Word8) Source #

Unfolds a FilePath into a stream of Word8 arrays. Requests to the IO device are performed using a buffer of size defaultChunkSize. The size of arrays in the resulting stream are therefore less than or equal to defaultChunkSize.

Pre-release

toChunksWithBufferOf :: (IsStream t, MonadCatch m, MonadAsync m) => Int -> FilePath -> t m (Array Word8) Source #

toChunksWithBufferOf size file reads a stream of arrays from file file. The maximum size of a single array is specified by size. The actual size read may be less than or equal to size.

toChunks :: (IsStream t, MonadCatch m, MonadAsync m) => FilePath -> t m (Array Word8) Source #

toChunks file reads a stream of arrays from file file. The maximum size of a single array is limited to defaultChunkSize. The actual size read may be less than defaultChunkSize.

toChunks = toChunksWithBufferOf defaultChunkSize

Since: 0.7.0

Write To File

write :: (MonadIO m, MonadCatch m) => FilePath -> Fold m Word8 () Source #

Write a byte stream to a file. Accumulates the input in chunks of up to defaultChunkSize before writing to the IO device.

Pre-release

writeWithBufferOf :: (MonadIO m, MonadCatch m) => Int -> FilePath -> Fold m Word8 () Source #

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

Pre-release

fromBytes :: (MonadAsync m, MonadCatch m) => FilePath -> SerialT m Word8 -> m () Source #

Write a byte stream to a file. Combines the bytes in chunks of size up to defaultChunkSize before writing. If the file exists it is truncated to zero size before writing. If the file does not exist it is created. File is locked using single writer locking mode.

Pre-release

fromBytesWithBufferOf :: (MonadAsync m, MonadCatch m) => Int -> FilePath -> SerialT m Word8 -> m () Source #

Like write but provides control over the write buffer. Output will be written to the IO device as soon as we collect the specified number of input elements.

Since: 0.7.0

putChunk :: Storable a => FilePath -> Array a -> IO () Source #

Write an array to a file. Overwrites the file if it exists.

Since: 0.7.0

writeChunks :: (MonadIO m, MonadCatch m, Storable a) => FilePath -> Fold m (Array a) () Source #

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

Pre-release

fromChunks :: (MonadAsync m, MonadCatch m, Storable a) => FilePath -> SerialT m (Array a) -> m () Source #

Write a stream of arrays to a file. Overwrites the file if it exists.

Since: 0.7.0

Append To File

append :: (MonadAsync m, MonadCatch m) => FilePath -> SerialT m Word8 -> m () Source #

Append a byte stream to a file. Combines the bytes in chunks of size up to defaultChunkSize before writing. If the file exists then the new data is appended to the file. If the file does not exist it is created. File is locked using single writer locking mode.

Since: 0.7.0

appendWithBufferOf :: (MonadAsync m, MonadCatch m) => Int -> FilePath -> SerialT m Word8 -> m () Source #

Like append but provides control over the write buffer. Output will be written to the IO device as soon as we collect the specified number of input elements.

Since: 0.7.0

appendArray :: Storable a => FilePath -> Array a -> IO () Source #

append an array to a file.

Since: 0.7.0

appendChunks :: (MonadAsync m, MonadCatch m, Storable a) => FilePath -> SerialT m (Array a) -> m () Source #

Append a stream of arrays to a file.

Since: 0.7.0