on 8aee462 help sign in
680 lines 24 kB history raw
1#include "inline.hs"
3-- |
4-- Module : Streamly.Internal.FileSystem.File
5-- Copyright : (c) 2019 Composewell Technologies
6--
7-- License : BSD3
8-- Maintainer : streamly@composewell.com
9-- Portability : GHC
10--
11-- Read and write streams and arrays to and from files specified by their paths
12-- in the file system. Unlike the handle based APIs which can have a read/write
13-- session consisting of multiple reads and writes to the handle, these APIs
14-- are one shot read or write APIs. These APIs open the file handle, perform
15-- the requested operation and close the handle. Thease are safer compared to
16-- the handle based APIs as there is no possibility of a file descriptor
17-- leakage.
18--
19-- > import qualified Streamly.Internal.FileSystem.File as File
20--
22module Streamly.Internal.FileSystem.File
23 (
24 -- * Streaming IO
25 -- | Stream data to or from a file or device sequentially. When reading,
26 -- the stream is lazy and generated on-demand as the consumer consumes it.
27 -- Read IO requests to the IO device are performed in chunks limited to a
28 -- maximum size of 32KiB, this is referred to as @defaultChunkSize@ in the
29 -- documentation. One IO request may or may not read the full
30 -- chunk. If the whole stream is not consumed, it is possible that we may
31 -- read slightly more from the IO device than what the consumer needed.
32 -- Unless specified otherwise in the API, writes are collected into chunks
33 -- of @defaultChunkSize@ before they are written to the IO device.
35 -- Streaming APIs work for all kind of devices, seekable or non-seekable;
36 -- including disks, files, memory devices, terminals, pipes, sockets and
37 -- fifos. While random access APIs work only for files or devices that have
38 -- random access or seek capability for example disks, memory devices.
39 -- Devices like terminals, pipes, sockets and fifos do not have random
40 -- access capability.
42 -- ** File IO Using Handle
43 withFile
45 -- ** Streams
46 , read
47 , readChunksWith
48 , readChunks
50 -- ** Unfolds
51 , readerWith
52 , reader
53 -- , readShared
54 -- , readUtf8
55 -- , readLines
56 -- , readFrames
57 , chunkReaderWith
58 , chunkReaderFromToWith
59 , chunkReader
61 -- ** Write To File
62 , putChunk -- writeChunk?
64 -- ** Folds
65 , write
66 -- , writeUtf8
67 -- , writeUtf8ByLines
68 -- , writeByFrames
69 , writeWith
70 , writeChunks
72 -- ** Writing Streams
73 , fromBytes -- putBytes?
74 , fromBytesWith
75 , fromChunks
77 -- ** Append To File
78 , writeAppend
79 , writeAppendWith
80 -- , appendShared
81 , writeAppendArray
82 , writeAppendChunks
84 -- * Deprecated
85 , readWithBufferOf
86 , readChunksWithBufferOf
87 , readChunksFromToWith
88 , toBytes
89 , toChunks
90 , toChunksWithBufferOf
91 , writeWithBufferOf
92 , fromBytesWithBufferOf
93 )
94where
96import Control.Monad.Catch (MonadCatch)
97import Control.Monad.IO.Class (MonadIO(..))
98import Data.Word (Word8)
99import System.IO (Handle, openFile, IOMode(..), hClose)
100import Prelude hiding (read)
102import qualified Control.Monad.Catch as MC
103import qualified System.IO as SIO
105import Streamly.Data.Fold (groupsOf, drain)
106import Streamly.Internal.Data.Array.Type (Array(..), pinnedWriteNUnsafe)
107import Streamly.Internal.Data.Fold.Type (Fold(..))
108import Streamly.Data.Stream (Stream)
109import Streamly.Internal.Data.Unbox (Unbox)
110import Streamly.Internal.Data.Unfold.Type (Unfold(..))
111-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
112import Streamly.Internal.System.IO (defaultChunkSize)
114import qualified Streamly.Data.Array as A
115import qualified Streamly.Data.Stream as S
116import qualified Streamly.Data.Unfold as UF
117import qualified Streamly.Internal.Data.Array.Type as IA (pinnedChunksOf)
118import qualified Streamly.Internal.Data.Unfold as UF (bracketIO)
119import qualified Streamly.Internal.Data.Fold.Type as FL
120 (Step(..), snoc, reduce)
121import qualified Streamly.Internal.FileSystem.Handle as FH
123-------------------------------------------------------------------------------
124-- References
125-------------------------------------------------------------------------------
126--
127-- The following references may be useful to build an understanding about the
128-- file API design:
129--
130-- http://www.linux-mag.com/id/308/ for blocking/non-blocking IO on linux.
131-- https://lwn.net/Articles/612483/ Non-blocking buffered file read operations
132-- https://en.wikipedia.org/wiki/C_file_input/output for C APIs.
133-- https://docs.oracle.com/javase/tutorial/essential/io/file.html for Java API.
134-- https://www.w3.org/TR/FileAPI/ for http file API.
136-------------------------------------------------------------------------------
137-- Safe file reading
138-------------------------------------------------------------------------------
140-- | @'withFile' name mode act@ opens a file using 'openFile' and passes
141-- the resulting handle to the computation @act@. The handle will be
142-- closed on exit from 'withFile', whether by normal termination or by
143-- raising an exception. If closing the handle raises an exception, then
144-- this exception will be raised by 'withFile' rather than any exception
145-- raised by 'act'.
146--
147-- /Pre-release/
148--
149{-# INLINE withFile #-}
150withFile :: (MonadIO m, MonadCatch m)
151 => FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
152withFile file mode = S.bracketIO (openFile file mode) hClose
154-- | Transform an 'Unfold' from a 'Handle' to an unfold from a 'FilePath'. The
155-- resulting unfold opens a handle in 'ReadMode', uses it using the supplied
156-- unfold and then makes sure that the handle is closed on normal termination
157-- or in case of an exception. If closing the handle raises an exception, then
158-- this exception will be raised by 'usingFile'.
159--
160-- /Pre-release/
161--
162{-# INLINE usingFile #-}
163usingFile :: (MonadIO m, MonadCatch m)
164 => Unfold m Handle a -> Unfold m FilePath a
165usingFile = UF.bracketIO (`openFile` ReadMode) hClose
167{-# INLINE usingFile2 #-}
168usingFile2 :: (MonadIO m, MonadCatch m)
169 => Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
170usingFile2 = UF.bracketIO before after
172 where
174 before (x, file) = do
175 h <- openFile file ReadMode
176 return (x, h)
178 after (_, h) = hClose h
180{-# INLINE usingFile3 #-}
181usingFile3 :: (MonadIO m, MonadCatch m)
182 => Unfold m (x, y, z, Handle) a -> Unfold m (x, y, z, FilePath) a
183usingFile3 = UF.bracketIO before after
185 where
187 before (x, y, z, file) = do
188 h <- openFile file ReadMode
189 return (x, y, z, h)
191 after (_, _, _, h) = hClose h
193-------------------------------------------------------------------------------
194-- Array IO (Input)
195-------------------------------------------------------------------------------
197-- TODO readArrayOf
199-------------------------------------------------------------------------------
200-- Array IO (output)
201-------------------------------------------------------------------------------
203-- | Write an array to a file. Overwrites the file if it exists.
204--
205-- /Pre-release/
206--
207{-# INLINABLE putChunk #-}
208putChunk :: FilePath -> Array a -> IO ()
209putChunk file arr = SIO.withFile file WriteMode (`FH.putChunk` arr)
211-- | append an array to a file.
212--
213-- /Pre-release/
214--
215{-# INLINABLE writeAppendArray #-}
216writeAppendArray :: FilePath -> Array a -> IO ()
217writeAppendArray file arr = SIO.withFile file AppendMode (`FH.putChunk` arr)
219-------------------------------------------------------------------------------
220-- Stream of Arrays IO
221-------------------------------------------------------------------------------
223-- | @readChunksWith size file@ reads a stream of arrays from file @file@.
224-- The maximum size of a single array is specified by @size@. The actual size
225-- read may be less than or equal to @size@.
226--
227-- /Pre-release/
228--
229{-# INLINE readChunksWith #-}
230readChunksWith :: (MonadIO m, MonadCatch m)
231 => Int -> FilePath -> Stream m (Array Word8)
232readChunksWith size file =
233 withFile file ReadMode (FH.readChunksWith size)
235{-# DEPRECATED toChunksWithBufferOf "Please use 'readChunksWith' instead" #-}
236{-# INLINE toChunksWithBufferOf #-}
237toChunksWithBufferOf :: (MonadIO m, MonadCatch m)
238 => Int -> FilePath -> Stream m (Array Word8)
239toChunksWithBufferOf = readChunksWith
241-- XXX read 'Array a' instead of Word8
242--
243-- | @readChunks file@ reads a stream of arrays from file @file@.
244-- The maximum size of a single array is limited to @defaultChunkSize@. The
245-- actual size read may be less than @defaultChunkSize@.
246--
247-- > readChunks = readChunksWith defaultChunkSize
248--
249-- /Pre-release/
250--
251{-# INLINE readChunks #-}
252readChunks :: (MonadIO m, MonadCatch m)
253 => FilePath -> Stream m (Array Word8)
254readChunks = readChunksWith defaultChunkSize
256{-# DEPRECATED toChunks "Please use 'readChunks' instead" #-}
257{-# INLINE toChunks #-}
258toChunks :: (MonadIO m, MonadCatch m) => FilePath -> Stream m (Array Word8)
259toChunks = readChunks
261-------------------------------------------------------------------------------
262-- Read File to Stream
263-------------------------------------------------------------------------------
265-- TODO for concurrent streams implement readahead IO. We can send multiple
266-- read requests at the same time. For serial case we can use async IO. We can
267-- also control the read throughput in mbps or IOPS.
269-- | Unfold the tuple @(bufsize, filepath)@ into a stream of 'Word8' arrays.
270-- Read requests to the IO device are performed using a buffer of size
271-- @bufsize@. The size of an array in the resulting stream is always less than
272-- or equal to @bufsize@.
273--
274-- /Pre-release/
275--
276{-# INLINE chunkReaderWith #-}
277chunkReaderWith :: (MonadIO m, MonadCatch m)
278 => Unfold m (Int, FilePath) (Array Word8)
279chunkReaderWith = usingFile2 FH.chunkReaderWith
281{-# DEPRECATED readChunksWithBufferOf
282 "Please use 'chunkReaderWith' instead" #-}
283{-# INLINE readChunksWithBufferOf #-}
284readChunksWithBufferOf :: (MonadIO m, MonadCatch m)
285 => Unfold m (Int, FilePath) (Array Word8)
286readChunksWithBufferOf = chunkReaderWith
288-- | Unfold the tuple @(from, to, bufsize, filepath)@ into a stream
289-- of 'Word8' arrays.
290-- Read requests to the IO device are performed using a buffer of size
291-- @bufsize@ starting from absolute offset of @from@ till the absolute
292-- position of @to@. The size of an array in the resulting stream is always
293-- less than or equal to @bufsize@.
294--
295-- /Pre-release/
296{-# INLINE chunkReaderFromToWith #-}
297chunkReaderFromToWith :: (MonadIO m, MonadCatch m) =>
298 Unfold m (Int, Int, Int, FilePath) (Array Word8)
299chunkReaderFromToWith = usingFile3 FH.chunkReaderFromToWith
301{-# DEPRECATED readChunksFromToWith
302 "Please use 'chunkReaderFromToWith' instead" #-}
303{-# INLINE readChunksFromToWith #-}
304readChunksFromToWith :: (MonadIO m, MonadCatch m) =>
305 Unfold m (Int, Int, Int, FilePath) (Array Word8)
306readChunksFromToWith = chunkReaderFromToWith
308-- | Unfolds a 'FilePath' into a stream of 'Word8' arrays. Requests to the IO
309-- device are performed using a buffer of size
310-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'. The
311-- size of arrays in the resulting stream are therefore less than or equal to
312-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'.
313--
314-- /Pre-release/
315{-# INLINE chunkReader #-}
316chunkReader :: (MonadIO m, MonadCatch m) => Unfold m FilePath (Array Word8)
317chunkReader = usingFile FH.chunkReader
319-- | Unfolds the tuple @(bufsize, filepath)@ into a byte stream, read requests
320-- to the IO device are performed using buffers of @bufsize@.
321--
322-- /Pre-release/
323{-# INLINE readerWith #-}
324readerWith :: (MonadIO m, MonadCatch m) => Unfold m (Int, FilePath) Word8
325readerWith = usingFile2 FH.readerWith
327{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead" #-}
328{-# INLINE readWithBufferOf #-}
329readWithBufferOf :: (MonadIO m, MonadCatch m) =>
330 Unfold m (Int, FilePath) Word8
331readWithBufferOf = readerWith
333-- | Unfolds a file path into a byte stream. IO requests to the device are
334-- performed in sizes of
335-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'.
336--
337-- /Pre-release/
338{-# INLINE reader #-}
339reader :: (MonadIO m, MonadCatch m) => Unfold m FilePath Word8
340reader = UF.many A.reader (usingFile FH.chunkReader)
342{-# INLINE concatChunks #-}
343concatChunks :: (Monad m, Unbox a) => Stream m (Array a) -> Stream m a
344concatChunks = S.unfoldMany A.reader
346-- | Generate a stream of bytes from a file specified by path. The stream ends
347-- when EOF is encountered. File is locked using multiple reader and single
348-- writer locking mode.
349--
350-- /Pre-release/
351--
352{-# INLINE read #-}
353read :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8
354read file = concatChunks $ withFile file ReadMode FH.readChunks
356{-# DEPRECATED toBytes "Please use 'read' instead" #-}
357{-# INLINE toBytes #-}
358toBytes :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8
359toBytes = read
361{-
362-- | Generate a stream of elements of the given type from a file 'Handle'. The
363-- stream ends when EOF is encountered. File is not locked for exclusive reads,
364-- writers can keep writing to the file.
365--
366-- @since 0.7.0
367{-# INLINE readShared #-}
368readShared :: MonadIO m => Handle -> Stream m Word8
369readShared = undefined
370-}
372-------------------------------------------------------------------------------
373-- Writing
374-------------------------------------------------------------------------------
376{-# INLINE fromChunksMode #-}
377fromChunksMode :: (MonadIO m, MonadCatch m)
378 => IOMode -> FilePath -> Stream m (Array a) -> m ()
379fromChunksMode mode file xs = S.fold drain $
380 withFile file mode (\h -> S.mapM (FH.putChunk h) xs)
382-- | Write a stream of arrays to a file. Overwrites the file if it exists.
383--
384-- /Pre-release/
385--
386{-# INLINE fromChunks #-}
387fromChunks :: (MonadIO m, MonadCatch m)
388 => FilePath -> Stream m (Array a) -> m ()
389fromChunks = fromChunksMode WriteMode
391-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
392--
393-- XXX test this
394-- Note that if you use a chunk size less than 8K (GHC's default buffer
395-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
396-- do not want buffering to occur at GHC level as well. Same thing applies to
397-- writes as well.
399-- | Like 'write' but provides control over the write buffer. Output will
400-- be written to the IO device as soon as we collect the specified number of
401-- input elements.
402--
403-- /Pre-release/
404--
405{-# INLINE fromBytesWith #-}
406fromBytesWith :: (MonadIO m, MonadCatch m)
407 => Int -> FilePath -> Stream m Word8 -> m ()
408fromBytesWith n file xs = fromChunks file $ IA.pinnedChunksOf n xs
410{-# DEPRECATED fromBytesWithBufferOf "Please use 'fromBytesWith' instead" #-}
411{-# INLINE fromBytesWithBufferOf #-}
412fromBytesWithBufferOf :: (MonadIO m, MonadCatch m)
413 => Int -> FilePath -> Stream m Word8 -> m ()
414fromBytesWithBufferOf = fromBytesWith
416-- > write = 'writeWith' defaultChunkSize
417--
418-- | Write a byte stream to a file. Combines the bytes in chunks of size
419-- up to 'A.defaultChunkSize' before writing. If the file exists it is
420-- truncated to zero size before writing. If the file does not exist it is
421-- created. File is locked using single writer locking mode.
422--
423-- /Pre-release/
424{-# INLINE fromBytes #-}
425fromBytes :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8 -> m ()
426fromBytes = fromBytesWith defaultChunkSize
428{-
429{-# INLINE write #-}
430write :: (MonadIO m, Storable a) => Handle -> Stream m a -> m ()
431write = toHandleWith A.defaultChunkSize
432-}
434-- | Write a stream of chunks to a handle. Each chunk in the stream is written
435-- to the device as a separate IO request.
436--
437-- /Pre-release/
438{-# INLINE writeChunks #-}
439writeChunks :: (MonadIO m, MonadCatch m)
440 => FilePath -> Fold m (Array a) ()
441writeChunks path = Fold step initial extract
442 where
443 initial = do
444 h <- liftIO (openFile path WriteMode)
445 fld <- FL.reduce (FH.writeChunks h)
446 `MC.onException` liftIO (hClose h)
447 return $ FL.Partial (fld, h)
448 step (fld, h) x = do
449 r <- FL.snoc fld x `MC.onException` liftIO (hClose h)
450 return $ FL.Partial (r, h)
451 extract (Fold _ initial1 extract1, h) = do
452 liftIO $ hClose h
453 res <- initial1
454 case res of
455 FL.Partial fs -> extract1 fs
456 FL.Done fb -> return fb
458-- | @writeWith chunkSize handle@ writes the input stream to @handle@.
459-- Bytes in the input stream are collected into a buffer until we have a chunk
460-- of size @chunkSize@ and then written to the IO device.
461--
462-- /Pre-release/
463{-# INLINE writeWith #-}
464writeWith :: (MonadIO m, MonadCatch m)
465 => Int -> FilePath -> Fold m Word8 ()
466writeWith n path =
467 groupsOf n (pinnedWriteNUnsafe n) (writeChunks path)
469{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
470{-# INLINE writeWithBufferOf #-}
471writeWithBufferOf :: (MonadIO m, MonadCatch m)
472 => Int -> FilePath -> Fold m Word8 ()
473writeWithBufferOf = writeWith
475-- > write = 'writeWith' A.defaultChunkSize
476--
477-- | Write a byte stream to a file. Accumulates the input in chunks of up to
478-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize' before writing to
479-- the IO device.
480--
481-- /Pre-release/
482--
483{-# INLINE write #-}
484write :: (MonadIO m, MonadCatch m) => FilePath -> Fold m Word8 ()
485write = writeWith defaultChunkSize
487-- | Append a stream of arrays to a file.
488--
489-- /Pre-release/
490--
491{-# INLINE writeAppendChunks #-}
492writeAppendChunks :: (MonadIO m, MonadCatch m)
493 => FilePath -> Stream m (Array a) -> m ()
494writeAppendChunks = fromChunksMode AppendMode
496-- | Like 'append' but provides control over the write buffer. Output will
497-- be written to the IO device as soon as we collect the specified number of
498-- input elements.
499--
500-- /Pre-release/
501--
502{-# INLINE writeAppendWith #-}
503writeAppendWith :: (MonadIO m, MonadCatch m)
504 => Int -> FilePath -> Stream m Word8 -> m ()
505writeAppendWith n file xs =
506 writeAppendChunks file $ IA.pinnedChunksOf n xs
508-- | Append a byte stream to a file. Combines the bytes in chunks of size up to
509-- 'A.defaultChunkSize' before writing. If the file exists then the new data
510-- is appended to the file. If the file does not exist it is created. File is
511-- locked using single writer locking mode.
512--
513-- /Pre-release/
514--
515{-# INLINE writeAppend #-}
516writeAppend :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8 -> m ()
517writeAppend = writeAppendWith defaultChunkSize
519{-
520-- | Like 'append' but the file is not locked for exclusive writes.
521--
522-- @since 0.7.0
523{-# INLINE appendShared #-}
524appendShared :: MonadIO m => Handle -> Stream m Word8 -> m ()
525appendShared = undefined
526-}
528-------------------------------------------------------------------------------
529-- IO with encoding/decoding Unicode characters
530-------------------------------------------------------------------------------
532{-
533-- |
534-- > readUtf8 = decodeUtf8 . read
535--
536-- Read a UTF8 encoded stream of unicode characters from a file handle.
537--
538-- @since 0.7.0
539{-# INLINE readUtf8 #-}
540readUtf8 :: MonadIO m => Handle -> Stream m Char
541readUtf8 = decodeUtf8 . read
543-- |
544-- > writeUtf8 h s = write h $ encodeUtf8 s
545--
546-- Encode a stream of unicode characters to UTF8 and write it to the given file
547-- handle. Default block buffering applies to the writes.
548--
549-- @since 0.7.0
550{-# INLINE writeUtf8 #-}
551writeUtf8 :: MonadIO m => Handle -> Stream m Char -> m ()
552writeUtf8 h s = write h $ encodeUtf8 s
554-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
555-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
556-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
557-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
558-- option.
559--
560-- @since 0.7.0
561{-# INLINE writeUtf8ByLines #-}
562writeUtf8ByLines :: MonadIO m => Handle -> Stream m Char -> m ()
563writeUtf8ByLines = undefined
565-- | Read UTF-8 lines from a file handle and apply the specified fold to each
566-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
567--
568-- @since 0.7.0
569{-# INLINE readLines #-}
570readLines :: MonadIO m => Handle -> Fold m Char b -> Stream m b
571readLines h f = foldLines (readUtf8 h) f
573-------------------------------------------------------------------------------
574-- Framing on a sequence
575-------------------------------------------------------------------------------
577-- | Read a stream from a file handle and split it into frames delimited by
578-- the specified sequence of elements. The supplied fold is applied on each
579-- frame.
580--
581-- @since 0.7.0
582{-# INLINE readFrames #-}
583readFrames :: (MonadIO m, Storable a)
584 => Array a -> Handle -> Fold m a b -> Stream m b
585readFrames = undefined -- foldFrames . read
587-- | Write a stream to the given file handle buffering up to frames separated
588-- by the given sequence or up to a maximum of @defaultChunkSize@.
589--
590-- @since 0.7.0
591{-# INLINE writeByFrames #-}
592writeByFrames :: (MonadIO m, Storable a)
593 => Array a -> Handle -> Stream m a -> m ()
594writeByFrames = undefined
596-------------------------------------------------------------------------------
597-- Random Access IO (Seek)
598-------------------------------------------------------------------------------
600-- XXX handles could be shared, so we may not want to use the handle state at
601-- all for these APIs. we can use pread and pwrite instead. On windows we will
602-- need to use readFile/writeFile with an offset argument.
604-------------------------------------------------------------------------------
606-- | Read the element at the given index treating the file as an array.
607--
608-- @since 0.7.0
609{-# INLINE readIndex #-}
610readIndex :: Storable a => Handle -> Int -> Maybe a
611readIndex arr i = undefined
613-- NOTE: To represent a range to read we have chosen (start, size) instead of
614-- (start, end). This helps in removing the ambiguity of whether "end" is
615-- included in the range or not.
616--
617-- We could avoid specifying the range to be read and instead use "take size"
618-- on the stream, but it may end up reading more and then consume it partially.
620-- | @readSliceWith chunkSize handle pos len@ reads up to @len@ bytes
621-- from @handle@ starting at the offset @pos@ from the beginning of the file.
622--
623-- Reads are performed in chunks of size @chunkSize@. For block devices, to
624-- avoid reading partial blocks @chunkSize@ must align with the block size of
625-- the underlying device. If the underlying block size is unknown, it is a good
626-- idea to keep it a multiple 4KiB. This API ensures that the start of each
627-- chunk is aligned with @chunkSize@ from second chunk onwards.
628--
629{-# INLINE readSliceWith #-}
630readSliceWith :: (MonadIO m, Storable a)
631 => Int -> Handle -> Int -> Int -> Stream m a
632readSliceWith chunkSize h pos len = undefined
634-- | @readSlice h i count@ streams a slice from the file handle @h@ starting
635-- at index @i@ and reading up to @count@ elements in the forward direction
636-- ending at the index @i + count - 1@.
637--
638-- @since 0.7.0
639{-# INLINE readSlice #-}
640readSlice :: (MonadIO m, Storable a)
641 => Handle -> Int -> Int -> Stream m a
642readSlice = readSliceWith defaultChunkSize
644-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
645-- at index @i@ and reading up to @count@ elements in the reverse direction
646-- ending at the index @i - count + 1@.
647--
648-- @since 0.7.0
649{-# INLINE readSliceRev #-}
650readSliceRev :: (MonadIO m, Storable a)
651 => Handle -> Int -> Int -> Stream m a
652readSliceRev h i count = undefined
654-- | Write the given element at the given index in the file.
655--
656-- @since 0.7.0
657{-# INLINE writeIndex #-}
658writeIndex :: (MonadIO m, Storable a) => Handle -> Int -> a -> m ()
659writeIndex h i a = undefined
661-- | @writeSlice h i count stream@ writes a stream to the file handle @h@
662-- starting at index @i@ and writing up to @count@ elements in the forward
663-- direction ending at the index @i + count - 1@.
664--
665-- @since 0.7.0
666{-# INLINE writeSlice #-}
667writeSlice :: (Monad m, Storable a)
668 => Handle -> Int -> Int -> Stream m a -> m ()
669writeSlice h i len s = undefined
671-- | @writeSliceRev h i count stream@ writes a stream to the file handle @h@
672-- starting at index @i@ and writing up to @count@ elements in the reverse
673-- direction ending at the index @i - count + 1@.
674--
675-- @since 0.7.0
676{-# INLINE writeSliceRev #-}
677writeSliceRev :: (Monad m, Storable a)
678 => Handle -> Int -> Int -> Stream m a -> m ()
679writeSliceRev arr i len s = undefined
680-}