on de680fb help sign in
663 lines 24 kB history raw
1#include "inline.hs"
3-- |
4-- Module : Streamly.Internal.FileSystem.Handle
5-- Copyright : (c) 2018 Composewell Technologies
6--
7-- License : BSD3
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
10-- Portability : GHC
12module Streamly.Internal.FileSystem.Handle
13 (
14 -- ** Read from Handle
15 read
16 -- , readUtf8
17 -- , readLines
18 -- , readFrames
19 , readWithBufferOf
21 , toBytes
22 , toBytesWithBufferOf
23 , getBytes
25 -- -- * Array Read
26 -- , readArrayUpto
27 -- , readArrayOf
28 , readChunks
29 , readChunksWithBufferOf
31 , toChunksWithBufferOf
32 , toChunks
33 , getChunks
35 -- ** Write to Handle
36 -- Byte stream write (Folds)
37 , write
38 , write2
39 -- , writeUtf8
40 -- , writeUtf8ByLines
41 -- , writeByFrames
42 -- , writeLines
43 , writeWithBufferOf
45 -- Byte stream write (Streams)
46 , fromBytes
47 , fromBytesWithBufferOf
49 -- -- * Array Write
50 , writeArray
51 , writeChunks
52 , writeChunksWithBufferOf
54 -- -- * Array stream Write
55 , fromChunksWithBufferOf
56 , fromChunks
57 , putChunks
58 , putStrings
59 , putBytes
60 , putLines
62 -- -- * Random Access (Seek)
63 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
64 -- files that have random access or seek capability. This type of devices
65 -- include disks, files, memory devices and exclude terminals, pipes,
66 -- sockets and fifos.
67 --
68 -- XXX need to decide whether to use readFromStepN style or readFromThenTo
69 -- style. The latter is consistent with list enumeration. The former may be
70 -- more unambiguous and it is easier and clearer to specify 0 elements.
71 --
72 -- We can also generate the request pattern using a funciton.
73 --
74 -- , readIndex
75 -- , readFrom -- read from a given position to the end of file
76 -- , readFromRev -- read from a given position the beginning of file
77 -- , readTo -- read from beginning up to the given position
78 -- , readToRev -- read from end to the given position in file
79 -- , readFromTo
80 -- , readFromThenTo
82 -- , readChunksFrom
83 -- , readChunksFromTo
84 -- , readChunksFromToWithBufferOf
85 -- , readChunksFromThenToWithBufferOf
87 -- , writeIndex
88 -- , writeFrom -- start writing at the given position
89 -- , writeFromRev
90 -- , writeTo -- write from beginning up to the given position
91 -- , writeToRev
92 -- , writeFromTo
93 -- , writeFromThenTo
94 --
95 -- , writeChunksFrom
96 -- , writeChunksFromTo
97 -- , writeChunksFromToWithBufferOf
98 -- , writeChunksFromThenToWithBufferOf
99 )
100where
102import Control.Monad.IO.Class (MonadIO(..))
103import Data.Word (Word8)
104import Foreign.ForeignPtr (withForeignPtr)
105import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
106import Foreign.Ptr (minusPtr, plusPtr)
107import Foreign.Storable (Storable(..))
108import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
109import System.IO (Handle, hGetBufSome, hPutBuf, stdin, stdout)
110import Prelude hiding (read)
112import Streamly.Prelude (MonadAsync)
113import Streamly.Data.Fold (Fold)
114import Streamly.Internal.Data.Fold.Types (Fold2(..))
115import Streamly.Internal.Data.Unfold.Types (Unfold(..))
116import Streamly.Internal.Data.Array.Storable.Foreign.Types
117 (Array(..), writeNUnsafe, defaultChunkSize, lpackArraysChunksOf
118 , unsafeFreezeWithShrink)
119import Streamly.Internal.Data.Array.Storable.Foreign.Mut.Types (mutableArray)
120import Streamly.Internal.Data.Stream.Serial (SerialT)
121import Streamly.Internal.Data.Stream.StreamK.Type (IsStream, mkStream)
122-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
124import qualified Streamly.Data.Fold as FL
125import qualified Streamly.Internal.Data.Fold.Types as FL
126import qualified Streamly.Internal.Data.Unfold as UF
127import qualified Streamly.Internal.Data.Array.Storable.Foreign as IA
128import qualified Streamly.Internal.Memory.ArrayStream as AS
129import qualified Streamly.Internal.Data.Stream.IsStream as S
130import qualified Streamly.Data.Array.Storable.Foreign as A
131import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
133-------------------------------------------------------------------------------
134-- References
135-------------------------------------------------------------------------------
136--
137-- The following references may be useful to build an understanding about the
138-- file API design:
139--
140-- http://www.linux-mag.com/id/308/ for blocking/non-blocking IO on linux.
141-- https://lwn.net/Articles/612483/ Non-blocking buffered file read operations
142-- https://en.wikipedia.org/wiki/C_file_input/output for C APIs.
143-- https://docs.oracle.com/javase/tutorial/essential/io/file.html for Java API.
144-- https://www.w3.org/TR/FileAPI/ for http file API.
146-------------------------------------------------------------------------------
147-- Array IO (Input)
148-------------------------------------------------------------------------------
150-- | Read a 'ByteArray' from a file handle. If no data is available on the
151-- handle it blocks until some data becomes available. If data is available
152-- then it immediately returns that data without blocking. It reads a maximum
153-- of up to the size requested.
154{-# INLINABLE readArrayUpto #-}
155readArrayUpto :: Int -> Handle -> IO (Array Word8)
156readArrayUpto size h = do
157 ptr <- mallocPlainForeignPtrBytes size
158 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
159 withForeignPtr ptr $ \p -> do
160 n <- hGetBufSome h p size
161 -- XXX shrink only if the diff is significant
162 return $
163 unsafeFreezeWithShrink $
164 mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
166-------------------------------------------------------------------------------
167-- Stream of Arrays IO
168-------------------------------------------------------------------------------
170-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
171-- The maximum size of a single array is specified by @size@. The actual size
172-- read may be less than or equal to @size@.
173{-# INLINABLE _toChunksWithBufferOf #-}
174_toChunksWithBufferOf :: (IsStream t, MonadIO m)
175 => Int -> Handle -> t m (Array Word8)
176_toChunksWithBufferOf size h = go
177 where
178 -- XXX use cons/nil instead
179 go = mkStream $ \_ yld _ stp -> do
180 arr <- liftIO $ readArrayUpto size h
181 if A.length arr == 0
182 then stp
183 else yld arr go
185-- | @toChunksWithBufferOf size handle@ reads a stream of arrays from the file
186-- handle @handle@. The maximum size of a single array is limited to @size@.
187-- The actual size read may be less than or equal to @size@.
188--
189-- @since 0.7.0
190{-# INLINE_NORMAL toChunksWithBufferOf #-}
191toChunksWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
192toChunksWithBufferOf size h = D.fromStreamD (D.Stream step ())
193 where
194 {-# INLINE_LATE step #-}
195 step _ _ = do
196 arr <- liftIO $ readArrayUpto size h
197 return $
198 case A.length arr of
199 0 -> D.Stop
200 _ -> D.Yield arr ()
202-- | Unfold the tuple @(bufsize, handle)@ into a stream of 'Word8' arrays.
203-- Read requests to the IO device are performed using a buffer of size
204-- @bufsize@. The size of an array in the resulting stream is always less than
205-- or equal to @bufsize@.
206--
207-- @since 0.7.0
208{-# INLINE_NORMAL readChunksWithBufferOf #-}
209readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
210readChunksWithBufferOf = Unfold step return
211 where
212 {-# INLINE_LATE step #-}
213 step (size, h) = do
214 arr <- liftIO $ readArrayUpto size h
215 return $
216 case A.length arr of
217 0 -> D.Stop
218 _ -> D.Yield arr (size, h)
220-- XXX read 'Array a' instead of Word8
221--
222-- | @toChunks handle@ reads a stream of arrays from the specified file
223-- handle. The maximum size of a single array is limited to
224-- @defaultChunkSize@. The actual size read may be less than or equal to
225-- @defaultChunkSize@.
226--
227-- > toChunks = toChunksWithBufferOf defaultChunkSize
228--
229-- @since 0.7.0
230{-# INLINE toChunks #-}
231toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
232toChunks = toChunksWithBufferOf defaultChunkSize
234-- | Read a stream of chunks from standard input. The maximum size of a single
235-- chunk is limited to @defaultChunkSize@. The actual size read may be less
236-- than @defaultChunkSize@.
237--
238-- > getChunks = toChunks stdin
239--
240-- /Internal/
241--
242{-# INLINE getChunks #-}
243getChunks :: (IsStream t, MonadIO m) => t m (Array Word8)
244getChunks = toChunks stdin
246-- | Read a stream of bytes from standard input.
247--
248-- > getBytes = toBytes stdin
249--
250-- /Internal/
251--
252{-# INLINE getBytes #-}
253getBytes :: (IsStream t, MonadIO m) => t m Word8
254getBytes = toBytes stdin
256-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
257-- device are performed using a buffer of size
258-- 'Streamly.Internal.Data.Array.Storable.Foreign.Types.defaultChunkSize'. The
259-- size of arrays in the resulting stream are therefore less than or equal to
260-- 'Streamly.Internal.Data.Array.Storable.Foreign.Types.defaultChunkSize'.
261--
262-- @since 0.7.0
263{-# INLINE readChunks #-}
264readChunks :: MonadIO m => Unfold m Handle (Array Word8)
265readChunks = UF.supplyFirst readChunksWithBufferOf defaultChunkSize
267-------------------------------------------------------------------------------
268-- Read File to Stream
269-------------------------------------------------------------------------------
271-- TODO for concurrent streams implement readahead IO. We can send multiple
272-- read requests at the same time. For serial case we can use async IO. We can
273-- also control the read throughput in mbps or IOPS.
275-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
276-- to the IO device are performed using buffers of @bufsize@.
277--
278-- @since 0.7.0
279{-# INLINE readWithBufferOf #-}
280readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
281readWithBufferOf = UF.concat readChunksWithBufferOf A.read
283-- | @toBytesWithBufferOf bufsize handle@ reads a byte stream from a file
284-- handle, reads are performed in chunks of up to @bufsize@.
285--
286-- /Internal/
287{-# INLINE toBytesWithBufferOf #-}
288toBytesWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
289toBytesWithBufferOf chunkSize h = AS.concat $ toChunksWithBufferOf chunkSize h
291-- TODO
292-- Generate a stream of elements of the given type from a file 'Handle'.
293-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
294--
295-- | Unfolds a file handle into a byte stream. IO requests to the device are
296-- performed in sizes of
297-- 'Streamly.Internal.Data.Array.Storable.Foreign.Types.defaultChunkSize'.
298--
299-- @since 0.7.0
300{-# INLINE read #-}
301read :: MonadIO m => Unfold m Handle Word8
302read = UF.supplyFirst readWithBufferOf defaultChunkSize
304-- | Generate a byte stream from a file 'Handle'.
305--
306-- /Internal/
307{-# INLINE toBytes #-}
308toBytes :: (IsStream t, MonadIO m) => Handle -> t m Word8
309toBytes = AS.concat . toChunks
311-------------------------------------------------------------------------------
312-- Writing
313-------------------------------------------------------------------------------
315-------------------------------------------------------------------------------
316-- Array IO (output)
317-------------------------------------------------------------------------------
319-- | Write an 'Array' to a file handle.
320--
321-- @since 0.7.0
322{-# INLINABLE writeArray #-}
323writeArray :: Storable a => Handle -> Array a -> IO ()
324writeArray _ arr | A.length arr == 0 = return ()
325writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
326 where
327 aLen =
328 let p = unsafeForeignPtrToPtr aStart
329 in aEnd `minusPtr` p
331-------------------------------------------------------------------------------
332-- Stream of Arrays IO
333-------------------------------------------------------------------------------
335-------------------------------------------------------------------------------
336-- Writing
337-------------------------------------------------------------------------------
339-- XXX use an unfold to fromObjects or fromUnfold so that we can put any object
340-- | Write a stream of arrays to a handle.
341--
342-- @since 0.7.0
343{-# INLINE fromChunks #-}
344fromChunks :: (MonadIO m, Storable a)
345 => Handle -> SerialT m (Array a) -> m ()
346fromChunks h = S.mapM_ (liftIO . writeArray h)
348-- | Write a stream of chunks to standard output.
349--
350-- /Internal/
351--
352{-# INLINE putChunks #-}
353putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
354putChunks = fromChunks stdout
356-- XXX use an unfold so that we can put any type of strings.
357-- | Write a stream of strings to standard output using the supplied encoding.
358-- Output is flushed to the device for each string.
359--
360-- /Internal/
361--
362{-# INLINE putStrings #-}
363putStrings :: MonadAsync m
364 => (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
365putStrings encode = putChunks . S.mapM (IA.fromStream . encode . S.fromList)
367-- XXX use an unfold so that we can put lines from any object
368-- | Write a stream of strings as separate lines to standard output using the
369-- supplied encoding. Output is line buffered i.e. the output is written to the
370-- device as soon as a newline is encountered.
371--
372-- /Internal/
373--
374{-# INLINE putLines #-}
375putLines :: MonadAsync m
376 => (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
377putLines encode = putChunks . S.mapM
378 (\xs -> IA.fromStream $ encode (S.fromList (xs ++ "\n")))
380-- | Write a stream of bytes from standard output.
381--
382-- > putBytes = fromBytes stdout
383--
384-- /Internal/
385--
386{-# INLINE putBytes #-}
387putBytes :: MonadIO m => SerialT m Word8 -> m ()
388putBytes = fromBytes stdout
390-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
391-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
392-- The chunk size is only a maximum and the actual writes could be smaller as
393-- we do not split the arrays to fit exactly to the specified size.
394--
395-- @since 0.7.0
396{-# INLINE fromChunksWithBufferOf #-}
397fromChunksWithBufferOf :: (MonadIO m, Storable a)
398 => Int -> Handle -> SerialT m (Array a) -> m ()
399fromChunksWithBufferOf n h xs = fromChunks h $ AS.compact n xs
401-- | @fromBytesWithBufferOf bufsize handle stream@ writes @stream@ to @handle@
402-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
403-- collect the required input size.
404--
405-- @since 0.7.0
406{-# INLINE fromBytesWithBufferOf #-}
407fromBytesWithBufferOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
408fromBytesWithBufferOf n h m = fromChunks h $ S.arraysOf n m
409-- fromBytesWithBufferOf n h m = fromChunks h $ AS.arraysOf n m
411-- > write = 'writeWithBufferOf' A.defaultChunkSize
412--
413-- | Write a byte stream to a file handle. Accumulates the input in chunks of
414-- up to 'Streamly.Internal.Data.Array.Storable.Foreign.Types.defaultChunkSize' before writing.
415--
416-- NOTE: This may perform better than the 'write' fold, you can try this if you
417-- need some extra perf boost.
418--
419-- @since 0.7.0
420{-# INLINE fromBytes #-}
421fromBytes :: MonadIO m => Handle -> SerialT m Word8 -> m ()
422fromBytes = fromBytesWithBufferOf defaultChunkSize
424-- | Write a stream of arrays to a handle. Each array in the stream is written
425-- to the device as a separate IO request.
426--
427-- @since 0.7.0
428{-# INLINE writeChunks #-}
429writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
430writeChunks h = FL.drainBy (liftIO . writeArray h)
432{-# INLINE writeChunks2 #-}
433writeChunks2 :: (MonadIO m, Storable a) => Fold2 m Handle (Array a) ()
434writeChunks2 = Fold2 (\h arr -> liftIO $ writeArray h arr >> return h) return (\_ -> return ())
436-- | @writeChunksWithBufferOf bufsize handle@ writes a stream of arrays
437-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
438-- We never split an array, if a single array is bigger than the specified size
439-- it emitted as it is. Multiple arrays are coalesed as long as the total size
440-- remains below the specified size.
441--
442-- @since 0.7.0
443{-# INLINE writeChunksWithBufferOf #-}
444writeChunksWithBufferOf :: (MonadIO m, Storable a)
445 => Int -> Handle -> Fold m (Array a) ()
446writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
448-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
449--
450-- XXX test this
451-- Note that if you use a chunk size less than 8K (GHC's default buffer
452-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
453-- do not want buffering to occur at GHC level as well. Same thing applies to
454-- writes as well.
456-- | @writeWithBufferOf reqSize handle@ writes the input stream to @handle@.
457-- Bytes in the input stream are collected into a buffer until we have a chunk
458-- of @reqSize@ and then written to the IO device.
459--
460-- @since 0.7.0
461{-# INLINE writeWithBufferOf #-}
462writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
463writeWithBufferOf n h = FL.lchunksOf n (writeNUnsafe n) (writeChunks h)
465{-# INLINE writeWithBufferOf2 #-}
466writeWithBufferOf2 :: MonadIO m => Int -> Fold2 m Handle Word8 ()
467writeWithBufferOf2 n = FL.lchunksOf2 n (writeNUnsafe n) writeChunks2
469-- > write = 'writeWithBufferOf' A.defaultChunkSize
470--
471-- | Write a byte stream to a file handle. Accumulates the input in chunks of
472-- up to 'Streamly.Internal.Data.Array.Storable.Foreign.Types.defaultChunkSize' before writing
473-- to the IO device.
474--
475-- @since 0.7.0
476{-# INLINE write #-}
477write :: MonadIO m => Handle -> Fold m Word8 ()
478write = writeWithBufferOf defaultChunkSize
480{-# INLINE write2 #-}
481write2 :: MonadIO m => Fold2 m Handle Word8 ()
482write2 = writeWithBufferOf2 defaultChunkSize
484{-
485{-# INLINE write #-}
486write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
487write = toHandleWith A.defaultChunkSize
488-}
490-- XXX mmap a file into an array. This could be useful for in-place operations
491-- on a file. For example, we can quicksort the contents of a file by mmapping
492-- it.
494-------------------------------------------------------------------------------
495-- IO with encoding/decoding Unicode characters
496-------------------------------------------------------------------------------
498{-
499-- |
500-- > readUtf8 = decodeUtf8 . read
501--
502-- Read a UTF8 encoded stream of unicode characters from a file handle.
503--
504-- @since 0.7.0
505{-# INLINE readUtf8 #-}
506readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
507readUtf8 = decodeUtf8 . read
509-- |
510-- > writeUtf8 h s = write h $ encodeUtf8 s
511--
512-- Encode a stream of unicode characters to UTF8 and write it to the given file
513-- handle. Default block buffering applies to the writes.
514--
515-- @since 0.7.0
516{-# INLINE writeUtf8 #-}
517writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
518writeUtf8 h s = write h $ encodeUtf8 s
520-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
521-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
522-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
523-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
524-- option.
525--
526-- @since 0.7.0
527{-# INLINE writeUtf8ByLines #-}
528writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
529writeUtf8ByLines = undefined
531-- | Read UTF-8 lines from a file handle and apply the specified fold to each
532-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
533--
534-- @since 0.7.0
535{-# INLINE readLines #-}
536readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
537readLines h f = foldLines (readUtf8 h) f
539-------------------------------------------------------------------------------
540-- Framing on a sequence
541-------------------------------------------------------------------------------
543-- | Read a stream from a file handle and split it into frames delimited by
544-- the specified sequence of elements. The supplied fold is applied on each
545-- frame.
546--
547-- @since 0.7.0
548{-# INLINE readFrames #-}
549readFrames :: (IsStream t, MonadIO m, Storable a)
550 => Array a -> Handle -> Fold m a b -> t m b
551readFrames = undefined -- foldFrames . read
553-- | Write a stream to the given file handle buffering up to frames separated
554-- by the given sequence or up to a maximum of @defaultChunkSize@.
555--
556-- @since 0.7.0
557{-# INLINE writeByFrames #-}
558writeByFrames :: (IsStream t, MonadIO m, Storable a)
559 => Array a -> Handle -> t m a -> m ()
560writeByFrames = undefined
562-------------------------------------------------------------------------------
563-- Framing by time
564-------------------------------------------------------------------------------
566-- | Write collecting the input in sessions of n seconds or if chunkSize
567-- gets exceeded.
568{-# INLINE writeByChunksOrSessionsOf #-}
569writeByChunksOrSessionsOf :: MonadIO m
570 => Int -> Double -> Handle -> SerialT m Word8 -> m ()
571writeByChunksOrSessionsOf chunkSize sessionSize h m = undefined
573-- | Write collecting the input in sessions of n seconds or if defaultChunkSize
574-- gets exceeded.
575{-# INLINE writeBySessionsOf #-}
576writeBySessionsOf :: MonadIO m => Double -> Handle -> SerialT m Word8 -> m ()
577writeBySessionsOf n = writeByChunksOrSessionsOf defaultChunkSize n
579-------------------------------------------------------------------------------
580-- Random Access IO (Seek)
581-------------------------------------------------------------------------------
583-- XXX handles could be shared, so we may not want to use the handle state at
584-- all for these APIs. we can use pread and pwrite instead. On windows we will
585-- need to use readFile/writeFile with an offset argument.
587-------------------------------------------------------------------------------
589-- | Read the element at the given index treating the file as an array.
590--
591-- @since 0.7.0
592{-# INLINE readIndex #-}
593readIndex :: Storable a => Handle -> Int -> Maybe a
594readIndex arr i = undefined
596-- NOTE: To represent a range to read we have chosen (start, size) instead of
597-- (start, end). This helps in removing the ambiguity of whether "end" is
598-- included in the range or not.
599--
600-- We could avoid specifying the range to be read and instead use "take size"
601-- on the stream, but it may end up reading more and then consume it partially.
603-- | @readSliceWith chunkSize handle pos len@ reads up to @len@ bytes
604-- from @handle@ starting at the offset @pos@ from the beginning of the file.
605--
606-- Reads are performed in chunks of size @chunkSize@. For block devices, to
607-- avoid reading partial blocks @chunkSize@ must align with the block size of
608-- the underlying device. If the underlying block size is unknown, it is a good
609-- idea to keep it a multiple 4KiB. This API ensures that the start of each
610-- chunk is aligned with @chunkSize@ from second chunk onwards.
611--
612{-# INLINE readSliceWith #-}
613readSliceWith :: (IsStream t, MonadIO m, Storable a)
614 => Int -> Handle -> Int -> Int -> t m a
615readSliceWith chunkSize h pos len = undefined
617-- | @readSlice h i count@ streams a slice from the file handle @h@ starting
618-- at index @i@ and reading up to @count@ elements in the forward direction
619-- ending at the index @i + count - 1@.
620--
621-- @since 0.7.0
622{-# INLINE readSlice #-}
623readSlice :: (IsStream t, MonadIO m, Storable a)
624 => Handle -> Int -> Int -> t m a
625readSlice = readSliceWith A.defaultChunkSize
627-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
628-- at index @i@ and reading up to @count@ elements in the reverse direction
629-- ending at the index @i - count + 1@.
630--
631-- @since 0.7.0
632{-# INLINE readSliceRev #-}
633readSliceRev :: (IsStream t, MonadIO m, Storable a)
634 => Handle -> Int -> Int -> t m a
635readSliceRev h i count = undefined
637-- | Write the given element at the given index in the file.
638--
639-- @since 0.7.0
640{-# INLINE writeIndex #-}
641writeIndex :: (MonadIO m, Storable a) => Handle -> Int -> a -> m ()
642writeIndex h i a = undefined
644-- | @writeSlice h i count stream@ writes a stream to the file handle @h@
645-- starting at index @i@ and writing up to @count@ elements in the forward
646-- direction ending at the index @i + count - 1@.
647--
648-- @since 0.7.0
649{-# INLINE writeSlice #-}
650writeSlice :: (IsStream t, Monad m, Storable a)
651 => Handle -> Int -> Int -> t m a -> m ()
652writeSlice h i len s = undefined
654-- | @writeSliceRev h i count stream@ writes a stream to the file handle @h@
655-- starting at index @i@ and writing up to @count@ elements in the reverse
656-- direction ending at the index @i - count + 1@.
657--
658-- @since 0.7.0
659{-# INLINE writeSliceRev #-}
660writeSliceRev :: (IsStream t, Monad m, Storable a)
661 => Handle -> Int -> Int -> t m a -> m ()
662writeSliceRev arr i len s = undefined
663-}