on 01643fe help sign in
707 lines 26 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
11--
12-- The fundamental singleton IO APIs are 'getChunk' and 'putChunk' and the
13-- fundamental stream IO APIs built on top of those are
14-- 'readChunksWithBufferOf' and 'writeChunks'. Rest of this module is just
15-- combinatorial programming using these.
17-- TODO: Need a separate module for pread/pwrite based reading writing for
18-- seekable devices. Stateless read/write can be helpful in multithreaded
19-- applications.
20--
21module Streamly.Internal.FileSystem.Handle
22 (
23 -- * Singleton APIs
24 getChunk
25 , getChunkOf
26 , putChunk
28 -- * Byte Stream Read
29 , read
30 -- , readUtf8
31 -- , readLines
32 -- , readFrames
33 , readWithBufferOf
35 , toBytes
36 , toBytesWithBufferOf
38 -- * Chunked Stream Read
39 , readChunks
40 , readChunksWithBufferOf
42 , toChunksWithBufferOf
43 , toChunks
45 -- * Byte Stream Write
46 -- Byte stream write (Folds)
47 , write
48 , consumer
49 -- , writeUtf8
50 -- , writeUtf8ByLines
51 -- , writeByFrames
52 -- , writeLines
53 , writeWithBufferOf
55 , putBytes
56 , putBytesWithBufferOf
58 -- * Chunked Stream Write
59 , writeChunks
60 , writeChunksWithBufferOf
62 , putChunksWithBufferOf
63 , putChunks
65 -- * Random Access (Seek)
66 -- | Unlike the streaming APIs listed above, these APIs apply to devices or
67 -- files that have random access or seek capability. This type of devices
68 -- include disks, files, memory devices and exclude terminals, pipes,
69 -- sockets and fifos.
71 -- We can also generate the request pattern using a funciton.
72 --
73 -- , readIndex
74 -- , readFrom -- read from a given position to the end of file
75 -- , readFromRev -- read from a given position the beginning of file
76 -- , readTo -- read from beginning up to the given position
77 -- , readToRev -- read from end to the given position in file
78 -- , readFromTo
79 -- , readFromThenTo
81 -- , readChunksFrom
82 -- , readChunksFromTo
83 , readChunksFromToWith
84 -- , readChunksFromThenToWith
86 -- , writeIndex
87 -- , writeFrom -- start writing at the given position
88 -- , writeFromRev
89 -- , writeTo -- write from beginning up to the given position
90 -- , writeToRev
91 -- , writeFromTo
92 -- , writeFromThenTo
93 --
94 -- , writeChunksFrom
95 -- , writeChunksFromTo
96 -- , writeChunksFromToWith
97 -- , writeChunksFromThenToWith
98 )
99where
101import Control.Exception (assert)
102import Control.Monad.IO.Class (MonadIO(..))
103import Data.Word (Word8)
104import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
105import Foreign.ForeignPtr (withForeignPtr)
106import Foreign.Ptr (minusPtr, plusPtr)
107import Foreign.Storable (Storable(..))
108import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
109import System.IO (Handle, SeekMode(..), hGetBufSome, hPutBuf, hSeek)
110import Prelude hiding (read)
112import Streamly.Internal.BaseCompat
113import Streamly.Internal.Data.Fold (Fold)
114import Streamly.Internal.Data.Consumer.Type (Consumer(..))
115import Streamly.Internal.Data.Unfold.Type (Unfold(..))
116import Streamly.Internal.Data.Array.Foreign.Type
117 (Array(..), writeNUnsafe, unsafeFreezeWithShrink)
118import Streamly.Internal.Data.Array.Foreign.Mut.Type (fromForeignPtrUnsafe)
119import Streamly.Internal.Data.Stream.Serial (SerialT)
120import Streamly.Internal.Data.Stream.IsStream.Type
121 (IsStream, mkStream, fromStreamD)
122import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
123-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
124import Streamly.Internal.System.IO (defaultChunkSize)
126import qualified Streamly.Internal.Data.Array.Foreign as A
127import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
128import qualified Streamly.Internal.Data.Consumer.Type as Consumer
129import qualified Streamly.Internal.Data.Fold as FL
130import qualified Streamly.Internal.Data.Fold.Type as FL
131import qualified Streamly.Internal.Data.Stream.IsStream as S
132import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
133import qualified Streamly.Internal.Data.Unfold as UF
135-- $setup
136-- >>> import qualified Streamly.Data.Array.Foreign as Array
137-- >>> import qualified Streamly.Data.Fold as Fold
138-- >>> import qualified Streamly.Data.Unfold as Unfold
139-- >>> import qualified Streamly.FileSystem.Handle as Handle
140-- >>> import qualified Streamly.Prelude as Stream
141--
142-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
143-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (supplyFirst)
144-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
145-- >>> import qualified Streamly.Internal.System.IO as IO (defaultChunkSize)
147-------------------------------------------------------------------------------
148-- References
149-------------------------------------------------------------------------------
150--
151-- The following references may be useful to build an understanding about the
152-- file API design:
153--
154-- http://www.linux-mag.com/id/308/ for blocking/non-blocking IO on linux.
155-- https://lwn.net/Articles/612483/ Non-blocking buffered file read operations
156-- https://en.wikipedia.org/wiki/C_file_input/output for C APIs.
157-- https://docs.oracle.com/javase/tutorial/essential/io/file.html for Java API.
158-- https://www.w3.org/TR/FileAPI/ for http file API.
160-------------------------------------------------------------------------------
161-- Array IO (Input)
162-------------------------------------------------------------------------------
164-- | Read a 'ByteArray' consisting of one or more bytes from a file handle. If
165-- no data is available on the handle it blocks until at least one byte becomes
166-- available. If any data is available then it immediately returns that data
167-- without blocking. As a result of this behavior, it may read less than or
168-- equal to the size requested.
169--
170-- @since 0.8.1
171{-# INLINABLE getChunk #-}
172getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
173getChunk size h = liftIO $ do
174 ptr <- mallocPlainForeignPtrBytes size
175 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
176 withForeignPtr ptr $ \p -> do
177 n <- hGetBufSome h p size
178 -- XXX shrink only if the diff is significant
179 return $
180 unsafeFreezeWithShrink $
181 fromForeignPtrUnsafe ptr (p `plusPtr` n) (p `plusPtr` size)
183-- This could be useful in implementing the "reverse" read APIs or if you want
184-- to read arrays of exact size instead of compacting them later. Compacting
185-- later requires more copying.
186--
187-- | Read a 'ByteArray' consisting of exactly the specified number of bytes
188-- from a file handle.
189--
190-- /Unimplemented/
191{-# INLINABLE getChunkOf #-}
192getChunkOf :: Int -> Handle -> IO (Array Word8)
193getChunkOf = undefined
195-------------------------------------------------------------------------------
196-- Stream of Arrays IO
197-------------------------------------------------------------------------------
199-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
200-- The maximum size of a single array is specified by @size@. The actual size
201-- read may be less than or equal to @size@.
202{-# INLINABLE _toChunksWithBufferOf #-}
203_toChunksWithBufferOf :: (IsStream t, MonadIO m)
204 => Int -> Handle -> t m (Array Word8)
205_toChunksWithBufferOf size h = go
206 where
207 -- XXX use cons/nil instead
208 go = mkStream $ \_ yld _ stp -> do
209 arr <- getChunk size h
210 if A.length arr == 0
211 then stp
212 else yld arr go
214-- | @toChunksWithBufferOf size handle@ reads a stream of arrays from the file
215-- handle @handle@. The maximum size of a single array is limited to @size@.
216-- The actual size read may be less than or equal to @size@.
217--
218-- >>> toChunksWithBufferOf size h = Stream.unfold Handle.readChunksWithBufferOf (size, h)
219--
220-- @since 0.7.0
221{-# INLINE_NORMAL toChunksWithBufferOf #-}
222toChunksWithBufferOf :: (IsStream t, MonadIO m) =>
223 Int -> Handle -> t m (Array Word8)
224toChunksWithBufferOf size h = fromStreamD (D.Stream step ())
225 where
226 {-# INLINE_LATE step #-}
227 step _ _ = do
228 arr <- getChunk size h
229 return $
230 case A.length arr of
231 0 -> D.Stop
232 _ -> D.Yield arr ()
234-- | Unfold the tuple @(bufsize, handle)@ into a stream of 'Word8' arrays.
235-- Read requests to the IO device are performed using a buffer of size
236-- @bufsize@. The size of an array in the resulting stream is always less than
237-- or equal to @bufsize@.
238--
239-- @since 0.7.0
240{-# INLINE_NORMAL readChunksWithBufferOf #-}
241readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
242readChunksWithBufferOf = Unfold step return
243 where
244 {-# INLINE_LATE step #-}
245 step (size, h) = do
246 arr <- getChunk size h
247 return $
248 case A.length arr of
249 0 -> D.Stop
250 _ -> D.Yield arr (size, h)
252-- There are two ways to implement this.
253--
254-- 1. Idiomatic: use a scan on the output of readChunksWithBufferOf to total
255-- the array lengths and trim the last array to correct size.
256-- 2. Simply implement it from scratch like readChunksWithBufferOf.
257--
258-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
259--
260-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
261-- reading from the offset `from` in the file and reads up to the offset `to`.
262--
263--
264{-# INLINE_NORMAL readChunksFromToWith #-}
265readChunksFromToWith :: MonadIO m =>
266 Unfold m (Int, Int, Int, Handle) (Array Word8)
267readChunksFromToWith = Unfold step inject
269 where
271 inject (from, to, bufSize, h) = do
272 liftIO $ hSeek h AbsoluteSeek $ fromIntegral from
273 -- XXX Use a strict Tuple?
274 return (to - from + 1, bufSize, h)
276 {-# INLINE_LATE step #-}
277 step (remaining, bufSize, h) =
278 if remaining <= 0
279 then return D.Stop
280 else do
281 arr <- getChunk (min bufSize remaining) h
282 return $
283 case A.length arr of
284 0 -> D.Stop
285 len ->
286 assert (len <= remaining)
287 $ D.Yield arr (remaining - len, bufSize, h)
289-- XXX read 'Array a' instead of Word8
290--
291-- | @toChunks handle@ reads a stream of arrays from the specified file
292-- handle. The maximum size of a single array is limited to
293-- @defaultChunkSize@. The actual size read may be less than or equal to
294-- @defaultChunkSize@.
295--
296-- >>> toChunks = Handle.toChunksWithBufferOf IO.defaultChunkSize
297--
298-- @since 0.7.0
299{-# INLINE toChunks #-}
300toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
301toChunks = toChunksWithBufferOf defaultChunkSize
303-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
304-- device are performed using a buffer of size
305-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'. The
306-- size of arrays in the resulting stream are therefore less than or equal to
307-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'.
308--
309-- >>> readChunks = Unfold.supplyFirst IO.defaultChunkSize Handle.readChunksWithBufferOf
310--
311-- @since 0.7.0
312{-# INLINE readChunks #-}
313readChunks :: MonadIO m => Unfold m Handle (Array Word8)
314readChunks = UF.supplyFirst defaultChunkSize readChunksWithBufferOf
316-------------------------------------------------------------------------------
317-- Read File to Stream
318-------------------------------------------------------------------------------
320-- TODO for concurrent streams implement readahead IO. We can send multiple
321-- read requests at the same time. For serial case we can use async IO. We can
322-- also control the read throughput in mbps or IOPS.
324-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
325-- to the IO device are performed using buffers of @bufsize@.
326--
327-- >>> readWithBufferOf = Unfold.many Handle.readChunksWithBufferOf Array.read
328--
329-- @since 0.7.0
330{-# INLINE readWithBufferOf #-}
331readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
332readWithBufferOf = UF.many readChunksWithBufferOf A.read
334-- | @toBytesWithBufferOf bufsize handle@ reads a byte stream from a file
335-- handle, reads are performed in chunks of up to @bufsize@.
336--
337-- >>> toBytesWithBufferOf size h = Stream.unfoldMany Array.read $ Handle.toChunksWithBufferOf size h
338--
339-- /Pre-release/
340{-# INLINE toBytesWithBufferOf #-}
341toBytesWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
342toBytesWithBufferOf size h = AS.concat $ toChunksWithBufferOf size h
344-- TODO
345-- Generate a stream of elements of the given type from a file 'Handle'.
346-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
347--
348-- | Unfolds a file handle into a byte stream. IO requests to the device are
349-- performed in sizes of
350-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'.
351--
352-- >>> read = Unfold.many Handle.readChunks Array.read
353--
354-- @since 0.7.0
355{-# INLINE read #-}
356read :: MonadIO m => Unfold m Handle Word8
357read = UF.many readChunks A.read
359-- | Generate a byte stream from a file 'Handle'.
360--
361-- >>> toBytes h = Stream.unfoldMany Array.read $ Handle.toChunks h
362--
363-- /Pre-release/
364{-# INLINE toBytes #-}
365toBytes :: (IsStream t, MonadIO m) => Handle -> t m Word8
366toBytes = AS.concat . toChunks
368-------------------------------------------------------------------------------
369-- Writing
370-------------------------------------------------------------------------------
372-------------------------------------------------------------------------------
373-- Array IO (output)
374-------------------------------------------------------------------------------
376-- | Write an 'Array' to a file handle.
377--
378-- @since 0.8.1
379{-# INLINABLE putChunk #-}
380putChunk :: (MonadIO m, Storable a) => Handle -> Array a -> m ()
381putChunk _ arr | A.length arr == 0 = return ()
382putChunk h Array{..} =
383 liftIO $ unsafeWithForeignPtr aStart $ \p -> hPutBuf h p aLen
385 where
387 aLen =
388 let p = unsafeForeignPtrToPtr aStart
389 in aEnd `minusPtr` p
391-------------------------------------------------------------------------------
392-- Stream of Arrays IO
393-------------------------------------------------------------------------------
395-------------------------------------------------------------------------------
396-- Writing
397-------------------------------------------------------------------------------
399-- XXX use an unfold to fromObjects or fromUnfold so that we can put any object
400-- | Write a stream of arrays to a handle.
401--
402-- >>> putChunks h = Stream.mapM_ (Handle.putChunk h)
403--
404-- @since 0.7.0
405{-# INLINE putChunks #-}
406putChunks :: (MonadIO m, Storable a)
407 => Handle -> SerialT m (Array a) -> m ()
408putChunks h = S.mapM_ (putChunk h)
410-- XXX AS.compact can be written idiomatically in terms of foldMany, just like
411-- AS.concat is written in terms of foldMany. Once that is done we can write
412-- idiomatic def in the docs.
413--
414-- | @putChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
415-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
416-- The chunk size is only a maximum and the actual writes could be smaller as
417-- we do not split the arrays to fit exactly to the specified size.
418--
419-- @since 0.7.0
420{-# INLINE putChunksWithBufferOf #-}
421putChunksWithBufferOf :: (MonadIO m, Storable a)
422 => Int -> Handle -> SerialT m (Array a) -> m ()
423putChunksWithBufferOf n h xs = putChunks h $ AS.compact n xs
425-- | @putBytesWithBufferOf bufsize handle stream@ writes @stream@ to @handle@
426-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
427-- collect the required input size.
428--
429-- >>> putBytesWithBufferOf n h m = Handle.putChunks h $ Stream.arraysOf n m
430--
431-- @since 0.7.0
432{-# INLINE putBytesWithBufferOf #-}
433putBytesWithBufferOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
434putBytesWithBufferOf n h m = putChunks h $ S.arraysOf n m
435-- putBytesWithBufferOf n h m = putChunks h $ AS.arraysOf n m
437-- | Write a byte stream to a file handle. Accumulates the input in chunks of
438-- up to 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing.
439--
440-- NOTE: This may perform better than the 'write' fold, you can try this if you
441-- need some extra perf boost.
442--
443-- >>> putBytes = Handle.putBytesWithBufferOf IO.defaultChunkSize
444--
445-- @since 0.7.0
446{-# INLINE putBytes #-}
447putBytes :: MonadIO m => Handle -> SerialT m Word8 -> m ()
448putBytes = putBytesWithBufferOf defaultChunkSize
450-- | Write a stream of arrays to a handle. Each array in the stream is written
451-- to the device as a separate IO request.
452--
453-- writeChunks h = Fold.drainBy (Handle.putChunk h)
454--
455-- @since 0.7.0
456{-# INLINE writeChunks #-}
457writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
458writeChunks h = FL.drainBy (putChunk h)
460-- | Like writeChunks but uses the experimental 'Consumer' API.
461--
462-- /Internal/
463{-# INLINE consumeChunks #-}
464consumeChunks :: (MonadIO m, Storable a) => Consumer m Handle (Array a) ()
465consumeChunks = Consumer.drainBy putChunk
467-- XXX lpackArraysChunksOf should be written idiomatically
468--
469-- | @writeChunksWithBufferOf bufsize handle@ writes a stream of arrays
470-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
471-- We never split an array, if a single array is bigger than the specified size
472-- it emitted as it is. Multiple arrays are coalesed as long as the total size
473-- remains below the specified size.
474--
475-- @since 0.7.0
476{-# INLINE writeChunksWithBufferOf #-}
477writeChunksWithBufferOf :: (MonadIO m, Storable a)
478 => Int -> Handle -> Fold m (Array a) ()
479writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
481-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
482--
483-- XXX test this
484-- Note that if you use a chunk size less than 8K (GHC's default buffer
485-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
486-- do not want buffering to occur at GHC level as well. Same thing applies to
487-- writes as well.
489-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
490--
491-- | @writeWithBufferOf reqSize handle@ writes the input stream to @handle@.
492-- Bytes in the input stream are collected into a buffer until we have a chunk
493-- of @reqSize@ and then written to the IO device.
494--
495-- >>> writeWithBufferOf n h = Fold.chunksOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
496--
497-- @since 0.7.0
498{-# INLINE writeWithBufferOf #-}
499writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
500writeWithBufferOf n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
502-- | Like 'writeWithBufferOf' but uses the experimental 'Consumer' API.
503--
504-- /Internal/
505{-# INLINE consumerWithBufferOf #-}
506consumerWithBufferOf :: MonadIO m => Int -> Consumer m Handle Word8 ()
507consumerWithBufferOf n =
508 FL.consumeMany (FL.take n $ writeNUnsafe n) consumeChunks
510-- | Write a byte stream to a file handle. Accumulates the input in chunks of
511-- up to 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing
512-- to the IO device.
513--
514-- >>> write = Handle.writeWithBufferOf IO.defaultChunkSize
515--
516-- @since 0.7.0
517{-# INLINE write #-}
518write :: MonadIO m => Handle -> Fold m Word8 ()
519write = writeWithBufferOf defaultChunkSize
521-- | Like 'write' but uses the experimental 'Consumer' API.
522--
523-- /Internal/
524{-# INLINE consumer #-}
525consumer :: MonadIO m => Consumer m Handle Word8 ()
526consumer = consumerWithBufferOf defaultChunkSize
528{-
529{-# INLINE write #-}
530write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
531write = toHandleWith A.defaultChunkSize
532-}
534-- XXX mmap a file into an array. This could be useful for in-place operations
535-- on a file. For example, we can quicksort the contents of a file by mmapping
536-- it.
538-------------------------------------------------------------------------------
539-- IO with encoding/decoding Unicode characters
540-------------------------------------------------------------------------------
542{-
543-- |
544-- > readUtf8 = decodeUtf8 . read
545--
546-- Read a UTF8 encoded stream of unicode characters from a file handle.
547--
548-- @since 0.7.0
549{-# INLINE readUtf8 #-}
550readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
551readUtf8 = decodeUtf8 . read
553-- |
554-- > writeUtf8 h s = write h $ encodeUtf8 s
555--
556-- Encode a stream of unicode characters to UTF8 and write it to the given file
557-- handle. Default block buffering applies to the writes.
558--
559-- @since 0.7.0
560{-# INLINE writeUtf8 #-}
561writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
562writeUtf8 h s = write h $ encodeUtf8 s
564-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
565-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
566-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
567-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
568-- option.
569--
570-- @since 0.7.0
571{-# INLINE writeUtf8ByLines #-}
572writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
573writeUtf8ByLines = undefined
575-- | Read UTF-8 lines from a file handle and apply the specified fold to each
576-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
577--
578-- @since 0.7.0
579{-# INLINE readLines #-}
580readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
581readLines h f = foldLines (readUtf8 h) f
583-------------------------------------------------------------------------------
584-- Framing on a sequence
585-------------------------------------------------------------------------------
587-- | Read a stream from a file handle and split it into frames delimited by
588-- the specified sequence of elements. The supplied fold is applied on each
589-- frame.
590--
591-- @since 0.7.0
592{-# INLINE readFrames #-}
593readFrames :: (IsStream t, MonadIO m, Storable a)
594 => Array a -> Handle -> Fold m a b -> t m b
595readFrames = undefined -- foldFrames . read
597-- | Write a stream to the given file handle buffering up to frames separated
598-- by the given sequence or up to a maximum of @defaultChunkSize@.
599--
600-- @since 0.7.0
601{-# INLINE writeByFrames #-}
602writeByFrames :: (IsStream t, MonadIO m, Storable a)
603 => Array a -> Handle -> t m a -> m ()
604writeByFrames = undefined
606-------------------------------------------------------------------------------
607-- Framing by time
608-------------------------------------------------------------------------------
610-- | Write collecting the input in sessions of n seconds or if chunkSize
611-- gets exceeded.
612{-# INLINE writeByChunksOrSessionsOf #-}
613writeByChunksOrSessionsOf :: MonadIO m
614 => Int -> Double -> Handle -> SerialT m Word8 -> m ()
615writeByChunksOrSessionsOf chunkSize sessionSize h m = undefined
617-- | Write collecting the input in sessions of n seconds or if defaultChunkSize
618-- gets exceeded.
619{-# INLINE writeBySessionsOf #-}
620writeBySessionsOf :: MonadIO m => Double -> Handle -> SerialT m Word8 -> m ()
621writeBySessionsOf n = writeByChunksOrSessionsOf defaultChunkSize n
623-------------------------------------------------------------------------------
624-- Random Access IO (Seek)
625-------------------------------------------------------------------------------
627-- XXX handles could be shared, so we may not want to use the handle state at
628-- all for these APIs. we can use pread and pwrite instead. On windows we will
629-- need to use readFile/writeFile with an offset argument.
631-------------------------------------------------------------------------------
633-- | Read the element at the given index treating the file as an array.
634--
635-- @since 0.7.0
636{-# INLINE readIndex #-}
637readIndex :: Storable a => Handle -> Int -> Maybe a
638readIndex arr i = undefined
640-- NOTE: To represent a range to read we have chosen (start, size) instead of
641-- (start, end). This helps in removing the ambiguity of whether "end" is
642-- included in the range or not.
643--
644-- We could avoid specifying the range to be read and instead use "take size"
645-- on the stream, but it may end up reading more and then consume it partially.
647-- | @readSliceWith chunkSize handle pos len@ reads up to @len@ bytes
648-- from @handle@ starting at the offset @pos@ from the beginning of the file.
649--
650-- Reads are performed in chunks of size @chunkSize@. For block devices, to
651-- avoid reading partial blocks @chunkSize@ must align with the block size of
652-- the underlying device. If the underlying block size is unknown, it is a good
653-- idea to keep it a multiple 4KiB. This API ensures that the start of each
654-- chunk is aligned with @chunkSize@ from second chunk onwards.
655--
656{-# INLINE readSliceWith #-}
657readSliceWith :: (IsStream t, MonadIO m, Storable a)
658 => Int -> Handle -> Int -> Int -> t m a
659readSliceWith chunkSize h pos len = undefined
661-- | @readSlice h i count@ streams a slice from the file handle @h@ starting
662-- at index @i@ and reading up to @count@ elements in the forward direction
663-- ending at the index @i + count - 1@.
664--
665-- @since 0.7.0
666{-# INLINE readSlice #-}
667readSlice :: (IsStream t, MonadIO m, Storable a)
668 => Handle -> Int -> Int -> t m a
669readSlice = readSliceWith A.defaultChunkSize
671-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
672-- at index @i@ and reading up to @count@ elements in the reverse direction
673-- ending at the index @i - count + 1@.
674--
675-- @since 0.7.0
676{-# INLINE readSliceRev #-}
677readSliceRev :: (IsStream t, MonadIO m, Storable a)
678 => Handle -> Int -> Int -> t m a
679readSliceRev h i count = undefined
681-- | Write the given element at the given index in the file.
682--
683-- @since 0.7.0
684{-# INLINE writeIndex #-}
685writeIndex :: (MonadIO m, Storable a) => Handle -> Int -> a -> m ()
686writeIndex h i a = undefined
688-- | @writeSlice h i count stream@ writes a stream to the file handle @h@
689-- starting at index @i@ and writing up to @count@ elements in the forward
690-- direction ending at the index @i + count - 1@.
691--
692-- @since 0.7.0
693{-# INLINE writeSlice #-}
694writeSlice :: (IsStream t, Monad m, Storable a)
695 => Handle -> Int -> Int -> t m a -> m ()
696writeSlice h i len s = undefined
698-- | @writeSliceRev h i count stream@ writes a stream to the file handle @h@
699-- starting at index @i@ and writing up to @count@ elements in the reverse
700-- direction ending at the index @i - count + 1@.
701--
702-- @since 0.7.0
703{-# INLINE writeSliceRev #-}
704writeSliceRev :: (IsStream t, Monad m, Storable a)
705 => Handle -> Int -> Int -> t m a -> m ()
706writeSliceRev arr i len s = undefined
707-}