on 09f2fa7 help sign in
665 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 (MonadAsync)
113import Streamly.Data.Fold (Fold)
114import Streamly.Internal.Data.Fold.Types (Fold2(..))
115import Streamly.Internal.Data.Unfold.Types (Unfold(..))
116import Streamly.Internal.Memory.Array.Types
117 (Array(..), writeNUnsafe, defaultChunkSize, shrinkToFit,
118 lpackArraysChunksOf)
119import Streamly.Internal.Data.Stream.Serial (SerialT)
120import Streamly.Internal.Data.Stream.StreamK.Type (IsStream, mkStream)
121-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
123import qualified Streamly.Data.Fold as FL
124import qualified Streamly.Internal.Data.Fold.Types as FL
125import qualified Streamly.Internal.Data.Unfold as UF
126import qualified Streamly.Internal.Memory.Array as IA
127import qualified Streamly.Internal.Memory.ArrayStream as AS
128import qualified Streamly.Internal.Prelude as S
129import qualified Streamly.Memory.Array as A
130import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
132-------------------------------------------------------------------------------
133-- References
134-------------------------------------------------------------------------------
135--
136-- The following references may be useful to build an understanding about the
137-- file API design:
138--
139-- http://www.linux-mag.com/id/308/ for blocking/non-blocking IO on linux.
140-- https://lwn.net/Articles/612483/ Non-blocking buffered file read operations
141-- https://en.wikipedia.org/wiki/C_file_input/output for C APIs.
142-- https://docs.oracle.com/javase/tutorial/essential/io/file.html for Java API.
143-- https://www.w3.org/TR/FileAPI/ for http file API.
145-------------------------------------------------------------------------------
146-- Array IO (Input)
147-------------------------------------------------------------------------------
149-- | Read a 'ByteArray' from a file handle. If no data is available on the
150-- handle it blocks until some data becomes available. If data is available
151-- then it immediately returns that data without blocking. It reads a maximum
152-- of up to the size requested.
153{-# INLINABLE readArrayUpto #-}
154readArrayUpto :: Int -> Handle -> IO (Array Word8)
155readArrayUpto size h = do
156 ptr <- mallocPlainForeignPtrBytes size
157 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
158 withForeignPtr ptr $ \p -> do
159 n <- hGetBufSome h p size
160 let v = Array
161 { aStart = ptr
162 , aEnd = p `plusPtr` n
163 , aBound = p `plusPtr` size
164 }
165 -- XXX shrink only if the diff is significant
166 shrinkToFit v
168-------------------------------------------------------------------------------
169-- Stream of Arrays IO
170-------------------------------------------------------------------------------
172-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
173-- The maximum size of a single array is specified by @size@. The actual size
174-- read may be less than or equal to @size@.
175{-# INLINABLE _toChunksWithBufferOf #-}
176_toChunksWithBufferOf :: (IsStream t, MonadIO m)
177 => Int -> Handle -> t m (Array Word8)
178_toChunksWithBufferOf size h = go
179 where
180 -- XXX use cons/nil instead
181 go = mkStream $ \_ yld _ stp -> do
182 arr <- liftIO $ readArrayUpto size h
183 if A.length arr == 0
184 then stp
185 else yld arr go
187-- | @toChunksWithBufferOf size handle@ reads a stream of arrays from the file
188-- handle @handle@. The maximum size of a single array is limited to @size@.
189-- The actual size read may be less than or equal to @size@.
190--
191-- @since 0.7.0
192{-# INLINE_NORMAL toChunksWithBufferOf #-}
193toChunksWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m (Array Word8)
194toChunksWithBufferOf size h = D.fromStreamD (D.Stream step ())
195 where
196 {-# INLINE_LATE step #-}
197 step _ _ = do
198 arr <- liftIO $ readArrayUpto size h
199 return $
200 case A.length arr of
201 0 -> D.Stop
202 _ -> D.Yield arr ()
204-- | Unfold the tuple @(bufsize, handle)@ into a stream of 'Word8' arrays.
205-- Read requests to the IO device are performed using a buffer of size
206-- @bufsize@. The size of an array in the resulting stream is always less than
207-- or equal to @bufsize@.
208--
209-- @since 0.7.0
210{-# INLINE_NORMAL readChunksWithBufferOf #-}
211readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
212readChunksWithBufferOf = Unfold step return
213 where
214 {-# INLINE_LATE step #-}
215 step (size, h) = do
216 arr <- liftIO $ readArrayUpto size h
217 return $
218 case A.length arr of
219 0 -> D.Stop
220 _ -> D.Yield arr (size, h)
222-- XXX read 'Array a' instead of Word8
223--
224-- | @toChunks handle@ reads a stream of arrays from the specified file
225-- handle. The maximum size of a single array is limited to
226-- @defaultChunkSize@. The actual size read may be less than or equal to
227-- @defaultChunkSize@.
228--
229-- > toChunks = toChunksWithBufferOf defaultChunkSize
230--
231-- @since 0.7.0
232{-# INLINE toChunks #-}
233toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
234toChunks = toChunksWithBufferOf defaultChunkSize
236-- | Read a stream of chunks from standard input. The maximum size of a single
237-- chunk is limited to @defaultChunkSize@. The actual size read may be less
238-- than @defaultChunkSize@.
239--
240-- > getChunks = toChunks stdin
241--
242-- /Internal/
243--
244{-# INLINE getChunks #-}
245getChunks :: (IsStream t, MonadIO m) => t m (Array Word8)
246getChunks = toChunks stdin
248-- | Read a stream of bytes from standard input.
249--
250-- > getBytes = toBytes stdin
251--
252-- /Internal/
253--
254{-# INLINE getBytes #-}
255getBytes :: (IsStream t, MonadIO m) => t m Word8
256getBytes = toBytes stdin
258-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
259-- device are performed using a buffer of size
260-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
261-- size of arrays in the resulting stream are therefore less than or equal to
262-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
263--
264-- @since 0.7.0
265{-# INLINE readChunks #-}
266readChunks :: MonadIO m => Unfold m Handle (Array Word8)
267readChunks = UF.supplyFirst readChunksWithBufferOf defaultChunkSize
269-------------------------------------------------------------------------------
270-- Read File to Stream
271-------------------------------------------------------------------------------
273-- TODO for concurrent streams implement readahead IO. We can send multiple
274-- read requests at the same time. For serial case we can use async IO. We can
275-- also control the read throughput in mbps or IOPS.
277-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
278-- to the IO device are performed using buffers of @bufsize@.
279--
280-- @since 0.7.0
281{-# INLINE readWithBufferOf #-}
282readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
283readWithBufferOf = UF.concat readChunksWithBufferOf A.read
285-- | @toBytesWithBufferOf bufsize handle@ reads a byte stream from a file
286-- handle, reads are performed in chunks of up to @bufsize@.
287--
288-- /Internal/
289{-# INLINE toBytesWithBufferOf #-}
290toBytesWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
291toBytesWithBufferOf chunkSize h = AS.concat $ toChunksWithBufferOf chunkSize h
293-- TODO
294-- Generate a stream of elements of the given type from a file 'Handle'.
295-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
296--
297-- | Unfolds a file handle into a byte stream. IO requests to the device are
298-- performed in sizes of
299-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
300--
301-- @since 0.7.0
302{-# INLINE read #-}
303read :: MonadIO m => Unfold m Handle Word8
304read = UF.supplyFirst readWithBufferOf defaultChunkSize
306-- | Generate a byte stream from a file 'Handle'.
307--
308-- /Internal/
309{-# INLINE toBytes #-}
310toBytes :: (IsStream t, MonadIO m) => Handle -> t m Word8
311toBytes = AS.concat . toChunks
313-------------------------------------------------------------------------------
314-- Writing
315-------------------------------------------------------------------------------
317-------------------------------------------------------------------------------
318-- Array IO (output)
319-------------------------------------------------------------------------------
321-- | Write an 'Array' to a file handle.
322--
323-- @since 0.7.0
324{-# INLINABLE writeArray #-}
325writeArray :: Storable a => Handle -> Array a -> IO ()
326writeArray _ arr | A.length arr == 0 = return ()
327writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
328 where
329 aLen =
330 let p = unsafeForeignPtrToPtr aStart
331 in aEnd `minusPtr` p
333-------------------------------------------------------------------------------
334-- Stream of Arrays IO
335-------------------------------------------------------------------------------
337-------------------------------------------------------------------------------
338-- Writing
339-------------------------------------------------------------------------------
341-- XXX use an unfold to fromObjects or fromUnfold so that we can put any object
342-- | Write a stream of arrays to a handle.
343--
344-- @since 0.7.0
345{-# INLINE fromChunks #-}
346fromChunks :: (MonadIO m, Storable a)
347 => Handle -> SerialT m (Array a) -> m ()
348fromChunks h = S.mapM_ (liftIO . writeArray h)
350-- | Write a stream of chunks to standard output.
351--
352-- /Internal/
353--
354{-# INLINE putChunks #-}
355putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
356putChunks = fromChunks stdout
358-- XXX use an unfold so that we can put any type of strings.
359-- | Write a stream of strings to standard output using the supplied encoding.
360-- Output is flushed to the device for each string.
361--
362-- /Internal/
363--
364{-# INLINE putStrings #-}
365putStrings :: MonadAsync m
366 => (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
367putStrings encode = putChunks . S.mapM (IA.fromStream . encode . S.fromList)
369-- XXX use an unfold so that we can put lines from any object
370-- | Write a stream of strings as separate lines to standard output using the
371-- supplied encoding. Output is line buffered i.e. the output is written to the
372-- device as soon as a newline is encountered.
373--
374-- /Internal/
375--
376{-# INLINE putLines #-}
377putLines :: MonadAsync m
378 => (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
379putLines encode = putChunks . S.mapM
380 (\xs -> IA.fromStream $ encode (S.fromList (xs ++ "\n")))
382-- | Write a stream of bytes from standard output.
383--
384-- > putBytes = fromBytes stdout
385--
386-- /Internal/
387--
388{-# INLINE putBytes #-}
389putBytes :: MonadIO m => SerialT m Word8 -> m ()
390putBytes = fromBytes stdout
392-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
393-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
394-- The chunk size is only a maximum and the actual writes could be smaller as
395-- we do not split the arrays to fit exactly to the specified size.
396--
397-- @since 0.7.0
398{-# INLINE fromChunksWithBufferOf #-}
399fromChunksWithBufferOf :: (MonadIO m, Storable a)
400 => Int -> Handle -> SerialT m (Array a) -> m ()
401fromChunksWithBufferOf n h xs = fromChunks h $ AS.compact n xs
403-- | @fromBytesWithBufferOf bufsize handle stream@ writes @stream@ to @handle@
404-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
405-- collect the required input size.
406--
407-- @since 0.7.0
408{-# INLINE fromBytesWithBufferOf #-}
409fromBytesWithBufferOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
410fromBytesWithBufferOf n h m = fromChunks h $ S.arraysOf n m
411-- fromBytesWithBufferOf n h m = fromChunks h $ AS.arraysOf n m
413-- > write = 'writeWithBufferOf' A.defaultChunkSize
414--
415-- | Write a byte stream to a file handle. Accumulates the input in chunks of
416-- up to 'Streamly.Internal.Memory.Array.Types.defaultChunkSize' before writing.
417--
418-- NOTE: This may perform better than the 'write' fold, you can try this if you
419-- need some extra perf boost.
420--
421-- @since 0.7.0
422{-# INLINE fromBytes #-}
423fromBytes :: MonadIO m => Handle -> SerialT m Word8 -> m ()
424fromBytes = fromBytesWithBufferOf defaultChunkSize
426-- | Write a stream of arrays to a handle. Each array in the stream is written
427-- to the device as a separate IO request.
428--
429-- @since 0.7.0
430{-# INLINE writeChunks #-}
431writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
432writeChunks h = FL.drainBy (liftIO . writeArray h)
434{-# INLINE writeChunks2 #-}
435writeChunks2 :: (MonadIO m, Storable a) => Fold2 m Handle (Array a) ()
436writeChunks2 = Fold2 (\h arr -> liftIO $ writeArray h arr >> return h) return (\_ -> return ())
438-- | @writeChunksWithBufferOf bufsize handle@ writes a stream of arrays
439-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
440-- We never split an array, if a single array is bigger than the specified size
441-- it emitted as it is. Multiple arrays are coalesed as long as the total size
442-- remains below the specified size.
443--
444-- @since 0.7.0
445{-# INLINE writeChunksWithBufferOf #-}
446writeChunksWithBufferOf :: (MonadIO m, Storable a)
447 => Int -> Handle -> Fold m (Array a) ()
448writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
450-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
451--
452-- XXX test this
453-- Note that if you use a chunk size less than 8K (GHC's default buffer
454-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
455-- do not want buffering to occur at GHC level as well. Same thing applies to
456-- writes as well.
458-- | @writeWithBufferOf reqSize 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 @reqSize@ and then written to the IO device.
461--
462-- @since 0.7.0
463{-# INLINE writeWithBufferOf #-}
464writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
465writeWithBufferOf n h = FL.lchunksOf n (writeNUnsafe n) (writeChunks h)
467{-# INLINE writeWithBufferOf2 #-}
468writeWithBufferOf2 :: MonadIO m => Int -> Fold2 m Handle Word8 ()
469writeWithBufferOf2 n = FL.lchunksOf2 n (writeNUnsafe n) writeChunks2
471-- > write = 'writeWithBufferOf' A.defaultChunkSize
472--
473-- | Write a byte stream to a file handle. Accumulates the input in chunks of
474-- up to 'Streamly.Internal.Memory.Array.Types.defaultChunkSize' before writing
475-- to the IO device.
476--
477-- @since 0.7.0
478{-# INLINE write #-}
479write :: MonadIO m => Handle -> Fold m Word8 ()
480write = writeWithBufferOf defaultChunkSize
482{-# INLINE write2 #-}
483write2 :: MonadIO m => Fold2 m Handle Word8 ()
484write2 = writeWithBufferOf2 defaultChunkSize
486{-
487{-# INLINE write #-}
488write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
489write = toHandleWith A.defaultChunkSize
490-}
492-- XXX mmap a file into an array. This could be useful for in-place operations
493-- on a file. For example, we can quicksort the contents of a file by mmapping
494-- it.
496-------------------------------------------------------------------------------
497-- IO with encoding/decoding Unicode characters
498-------------------------------------------------------------------------------
500{-
501-- |
502-- > readUtf8 = decodeUtf8 . read
503--
504-- Read a UTF8 encoded stream of unicode characters from a file handle.
505--
506-- @since 0.7.0
507{-# INLINE readUtf8 #-}
508readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
509readUtf8 = decodeUtf8 . read
511-- |
512-- > writeUtf8 h s = write h $ encodeUtf8 s
513--
514-- Encode a stream of unicode characters to UTF8 and write it to the given file
515-- handle. Default block buffering applies to the writes.
516--
517-- @since 0.7.0
518{-# INLINE writeUtf8 #-}
519writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
520writeUtf8 h s = write h $ encodeUtf8 s
522-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
523-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
524-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
525-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
526-- option.
527--
528-- @since 0.7.0
529{-# INLINE writeUtf8ByLines #-}
530writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
531writeUtf8ByLines = undefined
533-- | Read UTF-8 lines from a file handle and apply the specified fold to each
534-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
535--
536-- @since 0.7.0
537{-# INLINE readLines #-}
538readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
539readLines h f = foldLines (readUtf8 h) f
541-------------------------------------------------------------------------------
542-- Framing on a sequence
543-------------------------------------------------------------------------------
545-- | Read a stream from a file handle and split it into frames delimited by
546-- the specified sequence of elements. The supplied fold is applied on each
547-- frame.
548--
549-- @since 0.7.0
550{-# INLINE readFrames #-}
551readFrames :: (IsStream t, MonadIO m, Storable a)
552 => Array a -> Handle -> Fold m a b -> t m b
553readFrames = undefined -- foldFrames . read
555-- | Write a stream to the given file handle buffering up to frames separated
556-- by the given sequence or up to a maximum of @defaultChunkSize@.
557--
558-- @since 0.7.0
559{-# INLINE writeByFrames #-}
560writeByFrames :: (IsStream t, MonadIO m, Storable a)
561 => Array a -> Handle -> t m a -> m ()
562writeByFrames = undefined
564-------------------------------------------------------------------------------
565-- Framing by time
566-------------------------------------------------------------------------------
568-- | Write collecting the input in sessions of n seconds or if chunkSize
569-- gets exceeded.
570{-# INLINE writeByChunksOrSessionsOf #-}
571writeByChunksOrSessionsOf :: MonadIO m
572 => Int -> Double -> Handle -> SerialT m Word8 -> m ()
573writeByChunksOrSessionsOf chunkSize sessionSize h m = undefined
575-- | Write collecting the input in sessions of n seconds or if defaultChunkSize
576-- gets exceeded.
577{-# INLINE writeBySessionsOf #-}
578writeBySessionsOf :: MonadIO m => Double -> Handle -> SerialT m Word8 -> m ()
579writeBySessionsOf n = writeByChunksOrSessionsOf defaultChunkSize n
581-------------------------------------------------------------------------------
582-- Random Access IO (Seek)
583-------------------------------------------------------------------------------
585-- XXX handles could be shared, so we may not want to use the handle state at
586-- all for these APIs. we can use pread and pwrite instead. On windows we will
587-- need to use readFile/writeFile with an offset argument.
589-------------------------------------------------------------------------------
591-- | Read the element at the given index treating the file as an array.
592--
593-- @since 0.7.0
594{-# INLINE readIndex #-}
595readIndex :: Storable a => Handle -> Int -> Maybe a
596readIndex arr i = undefined
598-- NOTE: To represent a range to read we have chosen (start, size) instead of
599-- (start, end). This helps in removing the ambiguity of whether "end" is
600-- included in the range or not.
601--
602-- We could avoid specifying the range to be read and instead use "take size"
603-- on the stream, but it may end up reading more and then consume it partially.
605-- | @readSliceWith chunkSize handle pos len@ reads up to @len@ bytes
606-- from @handle@ starting at the offset @pos@ from the beginning of the file.
607--
608-- Reads are performed in chunks of size @chunkSize@. For block devices, to
609-- avoid reading partial blocks @chunkSize@ must align with the block size of
610-- the underlying device. If the underlying block size is unknown, it is a good
611-- idea to keep it a multiple 4KiB. This API ensures that the start of each
612-- chunk is aligned with @chunkSize@ from second chunk onwards.
613--
614{-# INLINE readSliceWith #-}
615readSliceWith :: (IsStream t, MonadIO m, Storable a)
616 => Int -> Handle -> Int -> Int -> t m a
617readSliceWith chunkSize h pos len = undefined
619-- | @readSlice h i count@ streams a slice from the file handle @h@ starting
620-- at index @i@ and reading up to @count@ elements in the forward direction
621-- ending at the index @i + count - 1@.
622--
623-- @since 0.7.0
624{-# INLINE readSlice #-}
625readSlice :: (IsStream t, MonadIO m, Storable a)
626 => Handle -> Int -> Int -> t m a
627readSlice = readSliceWith A.defaultChunkSize
629-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
630-- at index @i@ and reading up to @count@ elements in the reverse direction
631-- ending at the index @i - count + 1@.
632--
633-- @since 0.7.0
634{-# INLINE readSliceRev #-}
635readSliceRev :: (IsStream t, MonadIO m, Storable a)
636 => Handle -> Int -> Int -> t m a
637readSliceRev h i count = undefined
639-- | Write the given element at the given index in the file.
640--
641-- @since 0.7.0
642{-# INLINE writeIndex #-}
643writeIndex :: (MonadIO m, Storable a) => Handle -> Int -> a -> m ()
644writeIndex h i a = undefined
646-- | @writeSlice h i count stream@ writes a stream to the file handle @h@
647-- starting at index @i@ and writing up to @count@ elements in the forward
648-- direction ending at the index @i + count - 1@.
649--
650-- @since 0.7.0
651{-# INLINE writeSlice #-}
652writeSlice :: (IsStream t, Monad m, Storable a)
653 => Handle -> Int -> Int -> t m a -> m ()
654writeSlice h i len s = undefined
656-- | @writeSliceRev h i count stream@ writes a stream to the file handle @h@
657-- starting at index @i@ and writing up to @count@ elements in the reverse
658-- direction ending at the index @i - count + 1@.
659--
660-- @since 0.7.0
661{-# INLINE writeSliceRev #-}
662writeSliceRev :: (IsStream t, Monad m, Storable a)
663 => Handle -> Int -> Int -> t m a -> m ()
664writeSliceRev arr i len s = undefined
665-}