on d28e231 help sign in
699 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 , write2
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.Fold.Type (Fold2(..))
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 (mutableArray)
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.Fold as FL
129import qualified Streamly.Internal.Data.Fold.Type as FL
130import qualified Streamly.Internal.Data.Stream.IsStream as S
131import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
132import qualified Streamly.Internal.Data.Unfold as UF
134-- $setup
135-- >>> import qualified Streamly.Data.Array.Foreign as Array
136-- >>> import qualified Streamly.Data.Fold as Fold
137-- >>> import qualified Streamly.Data.Unfold as Unfold
138-- >>> import qualified Streamly.FileSystem.Handle as Handle
139-- >>> import qualified Streamly.Prelude as Stream
140--
141-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
142-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (supplyFirst)
143-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
144-- >>> import qualified Streamly.Internal.System.IO as IO (defaultChunkSize)
146-------------------------------------------------------------------------------
147-- References
148-------------------------------------------------------------------------------
149--
150-- The following references may be useful to build an understanding about the
151-- file API design:
152--
153-- http://www.linux-mag.com/id/308/ for blocking/non-blocking IO on linux.
154-- https://lwn.net/Articles/612483/ Non-blocking buffered file read operations
155-- https://en.wikipedia.org/wiki/C_file_input/output for C APIs.
156-- https://docs.oracle.com/javase/tutorial/essential/io/file.html for Java API.
157-- https://www.w3.org/TR/FileAPI/ for http file API.
159-------------------------------------------------------------------------------
160-- Array IO (Input)
161-------------------------------------------------------------------------------
163-- | Read a 'ByteArray' consisting of one or more bytes from a file handle. If
164-- no data is available on the handle it blocks until at least one byte becomes
165-- available. If any data is available then it immediately returns that data
166-- without blocking. As a result of this behavior, it may read less than or
167-- equal to the size requested.
168--
169-- @since 0.8.1
170{-# INLINABLE getChunk #-}
171getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
172getChunk size h = liftIO $ do
173 ptr <- mallocPlainForeignPtrBytes size
174 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
175 withForeignPtr ptr $ \p -> do
176 n <- hGetBufSome h p size
177 -- XXX shrink only if the diff is significant
178 return $
179 unsafeFreezeWithShrink $
180 mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
182-- This could be useful in implementing the "reverse" read APIs or if you want
183-- to read arrays of exact size instead of compacting them later. Compacting
184-- later requires more copying.
185--
186-- | Read a 'ByteArray' consisting of exactly the specified number of bytes
187-- from a file handle.
188--
189-- /Unimplemented/
190{-# INLINABLE getChunkOf #-}
191getChunkOf :: Int -> Handle -> IO (Array Word8)
192getChunkOf = undefined
194-------------------------------------------------------------------------------
195-- Stream of Arrays IO
196-------------------------------------------------------------------------------
198-- | @toChunksWithBufferOf size h@ reads a stream of arrays from file handle @h@.
199-- The maximum size of a single array is specified by @size@. The actual size
200-- read may be less than or equal to @size@.
201{-# INLINABLE _toChunksWithBufferOf #-}
202_toChunksWithBufferOf :: (IsStream t, MonadIO m)
203 => Int -> Handle -> t m (Array Word8)
204_toChunksWithBufferOf size h = go
205 where
206 -- XXX use cons/nil instead
207 go = mkStream $ \_ yld _ stp -> do
208 arr <- getChunk size h
209 if A.length arr == 0
210 then stp
211 else yld arr go
213-- | @toChunksWithBufferOf size handle@ reads a stream of arrays from the file
214-- handle @handle@. The maximum size of a single array is limited to @size@.
215-- The actual size read may be less than or equal to @size@.
216--
217-- >>> toChunksWithBufferOf size h = Stream.unfold Handle.readChunksWithBufferOf (size, h)
218--
219-- @since 0.7.0
220{-# INLINE_NORMAL toChunksWithBufferOf #-}
221toChunksWithBufferOf :: (IsStream t, MonadIO m) =>
222 Int -> Handle -> t m (Array Word8)
223toChunksWithBufferOf size h = fromStreamD (D.Stream step ())
224 where
225 {-# INLINE_LATE step #-}
226 step _ _ = do
227 arr <- getChunk size h
228 return $
229 case A.length arr of
230 0 -> D.Stop
231 _ -> D.Yield arr ()
233-- | Unfold the tuple @(bufsize, handle)@ into a stream of 'Word8' arrays.
234-- Read requests to the IO device are performed using a buffer of size
235-- @bufsize@. The size of an array in the resulting stream is always less than
236-- or equal to @bufsize@.
237--
238-- @since 0.7.0
239{-# INLINE_NORMAL readChunksWithBufferOf #-}
240readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
241readChunksWithBufferOf = Unfold step return
242 where
243 {-# INLINE_LATE step #-}
244 step (size, h) = do
245 arr <- getChunk size h
246 return $
247 case A.length arr of
248 0 -> D.Stop
249 _ -> D.Yield arr (size, h)
251-- There are two ways to implement this.
252--
253-- 1. Idiomatic: use a scan on the output of readChunksWithBufferOf to total
254-- the array lengths and trim the last array to correct size.
255-- 2. Simply implement it from scratch like readChunksWithBufferOf.
256--
257-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
258--
259-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
260-- reading from the offset `from` in the file and reads up to the offset `to`.
261--
262--
263{-# INLINE_NORMAL readChunksFromToWith #-}
264readChunksFromToWith :: MonadIO m =>
265 Unfold m (Int, Int, Int, Handle) (Array Word8)
266readChunksFromToWith = Unfold step inject
268 where
270 inject (from, to, bufSize, h) = do
271 liftIO $ hSeek h AbsoluteSeek $ fromIntegral from
272 -- XXX Use a strict Tuple?
273 return (to - from + 1, bufSize, h)
275 {-# INLINE_LATE step #-}
276 step (remaining, bufSize, h) =
277 if remaining <= 0
278 then return D.Stop
279 else do
280 arr <- getChunk (min bufSize remaining) h
281 return $
282 case A.length arr of
283 0 -> D.Stop
284 len ->
285 assert (len <= remaining)
286 $ D.Yield arr (remaining - len, bufSize, h)
288-- XXX read 'Array a' instead of Word8
289--
290-- | @toChunks handle@ reads a stream of arrays from the specified file
291-- handle. The maximum size of a single array is limited to
292-- @defaultChunkSize@. The actual size read may be less than or equal to
293-- @defaultChunkSize@.
294--
295-- >>> toChunks = Handle.toChunksWithBufferOf IO.defaultChunkSize
296--
297-- @since 0.7.0
298{-# INLINE toChunks #-}
299toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
300toChunks = toChunksWithBufferOf defaultChunkSize
302-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
303-- device are performed using a buffer of size
304-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'. The
305-- size of arrays in the resulting stream are therefore less than or equal to
306-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'.
307--
308-- >>> readChunks = Unfold.supplyFirst IO.defaultChunkSize Handle.readChunksWithBufferOf
309--
310-- @since 0.7.0
311{-# INLINE readChunks #-}
312readChunks :: MonadIO m => Unfold m Handle (Array Word8)
313readChunks = UF.supplyFirst defaultChunkSize readChunksWithBufferOf
315-------------------------------------------------------------------------------
316-- Read File to Stream
317-------------------------------------------------------------------------------
319-- TODO for concurrent streams implement readahead IO. We can send multiple
320-- read requests at the same time. For serial case we can use async IO. We can
321-- also control the read throughput in mbps or IOPS.
323-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
324-- to the IO device are performed using buffers of @bufsize@.
325--
326-- >>> readWithBufferOf = Unfold.many Handle.readChunksWithBufferOf Array.read
327--
328-- @since 0.7.0
329{-# INLINE readWithBufferOf #-}
330readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
331readWithBufferOf = UF.many readChunksWithBufferOf A.read
333-- | @toBytesWithBufferOf bufsize handle@ reads a byte stream from a file
334-- handle, reads are performed in chunks of up to @bufsize@.
335--
336-- >>> toBytesWithBufferOf size h = Stream.unfoldMany Array.read $ Handle.toChunksWithBufferOf size h
337--
338-- /Pre-release/
339{-# INLINE toBytesWithBufferOf #-}
340toBytesWithBufferOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
341toBytesWithBufferOf size h = AS.concat $ toChunksWithBufferOf size h
343-- TODO
344-- Generate a stream of elements of the given type from a file 'Handle'.
345-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
346--
347-- | Unfolds a file handle into a byte stream. IO requests to the device are
348-- performed in sizes of
349-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'.
350--
351-- >>> read = Unfold.many Handle.readChunks Array.read
352--
353-- @since 0.7.0
354{-# INLINE read #-}
355read :: MonadIO m => Unfold m Handle Word8
356read = UF.many readChunks A.read
358-- | Generate a byte stream from a file 'Handle'.
359--
360-- >>> toBytes h = Stream.unfoldMany Array.read $ Handle.toChunks h
361--
362-- /Pre-release/
363{-# INLINE toBytes #-}
364toBytes :: (IsStream t, MonadIO m) => Handle -> t m Word8
365toBytes = AS.concat . toChunks
367-------------------------------------------------------------------------------
368-- Writing
369-------------------------------------------------------------------------------
371-------------------------------------------------------------------------------
372-- Array IO (output)
373-------------------------------------------------------------------------------
375-- | Write an 'Array' to a file handle.
376--
377-- @since 0.8.1
378{-# INLINABLE putChunk #-}
379putChunk :: (MonadIO m, Storable a) => Handle -> Array a -> m ()
380putChunk _ arr | A.length arr == 0 = return ()
381putChunk h Array{..} =
382 liftIO $ unsafeWithForeignPtr aStart $ \p -> hPutBuf h p aLen
384 where
386 aLen =
387 let p = unsafeForeignPtrToPtr aStart
388 in aEnd `minusPtr` p
390-------------------------------------------------------------------------------
391-- Stream of Arrays IO
392-------------------------------------------------------------------------------
394-------------------------------------------------------------------------------
395-- Writing
396-------------------------------------------------------------------------------
398-- XXX use an unfold to fromObjects or fromUnfold so that we can put any object
399-- | Write a stream of arrays to a handle.
400--
401-- >>> putChunks h = Stream.mapM_ (Handle.putChunk h)
402--
403-- @since 0.7.0
404{-# INLINE putChunks #-}
405putChunks :: (MonadIO m, Storable a)
406 => Handle -> SerialT m (Array a) -> m ()
407putChunks h = S.mapM_ (putChunk h)
409-- XXX AS.compact can be written idiomatically in terms of foldMany, just like
410-- AS.concat is written in terms of foldMany. Once that is done we can write
411-- idiomatic def in the docs.
412--
413-- | @putChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
414-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
415-- The chunk size is only a maximum and the actual writes could be smaller as
416-- we do not split the arrays to fit exactly to the specified size.
417--
418-- @since 0.7.0
419{-# INLINE putChunksWithBufferOf #-}
420putChunksWithBufferOf :: (MonadIO m, Storable a)
421 => Int -> Handle -> SerialT m (Array a) -> m ()
422putChunksWithBufferOf n h xs = putChunks h $ AS.compact n xs
424-- | @putBytesWithBufferOf bufsize handle stream@ writes @stream@ to @handle@
425-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
426-- collect the required input size.
427--
428-- >>> putBytesWithBufferOf n h m = Handle.putChunks h $ Stream.arraysOf n m
429--
430-- @since 0.7.0
431{-# INLINE putBytesWithBufferOf #-}
432putBytesWithBufferOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
433putBytesWithBufferOf n h m = putChunks h $ S.arraysOf n m
434-- putBytesWithBufferOf n h m = putChunks h $ AS.arraysOf n m
436-- | Write a byte stream to a file handle. Accumulates the input in chunks of
437-- up to 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing.
438--
439-- NOTE: This may perform better than the 'write' fold, you can try this if you
440-- need some extra perf boost.
441--
442-- >>> putBytes = Handle.putBytesWithBufferOf IO.defaultChunkSize
443--
444-- @since 0.7.0
445{-# INLINE putBytes #-}
446putBytes :: MonadIO m => Handle -> SerialT m Word8 -> m ()
447putBytes = putBytesWithBufferOf defaultChunkSize
449-- | Write a stream of arrays to a handle. Each array in the stream is written
450-- to the device as a separate IO request.
451--
452-- writeChunks h = Fold.drainBy (Handle.putChunk h)
453--
454-- @since 0.7.0
455{-# INLINE writeChunks #-}
456writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
457writeChunks h = FL.drainBy (putChunk h)
459{-# INLINE writeChunks2 #-}
460writeChunks2 :: (MonadIO m, Storable a) => Fold2 m Handle (Array a) ()
461writeChunks2 =
462 Fold2 (\h arr -> liftIO $ putChunk h arr >> return h)
463 return
464 (\_ -> return ())
466-- XXX lpackArraysChunksOf should be written idiomatically
467--
468-- | @writeChunksWithBufferOf bufsize handle@ writes a stream of arrays
469-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
470-- We never split an array, if a single array is bigger than the specified size
471-- it emitted as it is. Multiple arrays are coalesed as long as the total size
472-- remains below the specified size.
473--
474-- @since 0.7.0
475{-# INLINE writeChunksWithBufferOf #-}
476writeChunksWithBufferOf :: (MonadIO m, Storable a)
477 => Int -> Handle -> Fold m (Array a) ()
478writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
480-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
481--
482-- XXX test this
483-- Note that if you use a chunk size less than 8K (GHC's default buffer
484-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
485-- do not want buffering to occur at GHC level as well. Same thing applies to
486-- writes as well.
488-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
489--
490-- | @writeWithBufferOf reqSize handle@ writes the input stream to @handle@.
491-- Bytes in the input stream are collected into a buffer until we have a chunk
492-- of @reqSize@ and then written to the IO device.
493--
494-- >>> writeWithBufferOf n h = Fold.chunksOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
495--
496-- @since 0.7.0
497{-# INLINE writeWithBufferOf #-}
498writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
499writeWithBufferOf n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
501{-# INLINE writeWithBufferOf2 #-}
502writeWithBufferOf2 :: MonadIO m => Int -> Fold2 m Handle Word8 ()
503writeWithBufferOf2 n = FL.chunksOf2 n (writeNUnsafe n) writeChunks2
505-- | Write a byte stream to a file handle. Accumulates the input in chunks of
506-- up to 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize' before writing
507-- to the IO device.
508--
509-- >>> write = Handle.writeWithBufferOf IO.defaultChunkSize
510--
511-- @since 0.7.0
512{-# INLINE write #-}
513write :: MonadIO m => Handle -> Fold m Word8 ()
514write = writeWithBufferOf defaultChunkSize
516{-# INLINE write2 #-}
517write2 :: MonadIO m => Fold2 m Handle Word8 ()
518write2 = writeWithBufferOf2 defaultChunkSize
520{-
521{-# INLINE write #-}
522write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
523write = toHandleWith A.defaultChunkSize
524-}
526-- XXX mmap a file into an array. This could be useful for in-place operations
527-- on a file. For example, we can quicksort the contents of a file by mmapping
528-- it.
530-------------------------------------------------------------------------------
531-- IO with encoding/decoding Unicode characters
532-------------------------------------------------------------------------------
534{-
535-- |
536-- > readUtf8 = decodeUtf8 . read
537--
538-- Read a UTF8 encoded stream of unicode characters from a file handle.
539--
540-- @since 0.7.0
541{-# INLINE readUtf8 #-}
542readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
543readUtf8 = decodeUtf8 . read
545-- |
546-- > writeUtf8 h s = write h $ encodeUtf8 s
547--
548-- Encode a stream of unicode characters to UTF8 and write it to the given file
549-- handle. Default block buffering applies to the writes.
550--
551-- @since 0.7.0
552{-# INLINE writeUtf8 #-}
553writeUtf8 :: MonadIO m => Handle -> SerialT m Char -> m ()
554writeUtf8 h s = write h $ encodeUtf8 s
556-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
557-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
558-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
559-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
560-- option.
561--
562-- @since 0.7.0
563{-# INLINE writeUtf8ByLines #-}
564writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
565writeUtf8ByLines = undefined
567-- | Read UTF-8 lines from a file handle and apply the specified fold to each
568-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
569--
570-- @since 0.7.0
571{-# INLINE readLines #-}
572readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
573readLines h f = foldLines (readUtf8 h) f
575-------------------------------------------------------------------------------
576-- Framing on a sequence
577-------------------------------------------------------------------------------
579-- | Read a stream from a file handle and split it into frames delimited by
580-- the specified sequence of elements. The supplied fold is applied on each
581-- frame.
582--
583-- @since 0.7.0
584{-# INLINE readFrames #-}
585readFrames :: (IsStream t, MonadIO m, Storable a)
586 => Array a -> Handle -> Fold m a b -> t m b
587readFrames = undefined -- foldFrames . read
589-- | Write a stream to the given file handle buffering up to frames separated
590-- by the given sequence or up to a maximum of @defaultChunkSize@.
591--
592-- @since 0.7.0
593{-# INLINE writeByFrames #-}
594writeByFrames :: (IsStream t, MonadIO m, Storable a)
595 => Array a -> Handle -> t m a -> m ()
596writeByFrames = undefined
598-------------------------------------------------------------------------------
599-- Framing by time
600-------------------------------------------------------------------------------
602-- | Write collecting the input in sessions of n seconds or if chunkSize
603-- gets exceeded.
604{-# INLINE writeByChunksOrSessionsOf #-}
605writeByChunksOrSessionsOf :: MonadIO m
606 => Int -> Double -> Handle -> SerialT m Word8 -> m ()
607writeByChunksOrSessionsOf chunkSize sessionSize h m = undefined
609-- | Write collecting the input in sessions of n seconds or if defaultChunkSize
610-- gets exceeded.
611{-# INLINE writeBySessionsOf #-}
612writeBySessionsOf :: MonadIO m => Double -> Handle -> SerialT m Word8 -> m ()
613writeBySessionsOf n = writeByChunksOrSessionsOf defaultChunkSize n
615-------------------------------------------------------------------------------
616-- Random Access IO (Seek)
617-------------------------------------------------------------------------------
619-- XXX handles could be shared, so we may not want to use the handle state at
620-- all for these APIs. we can use pread and pwrite instead. On windows we will
621-- need to use readFile/writeFile with an offset argument.
623-------------------------------------------------------------------------------
625-- | Read the element at the given index treating the file as an array.
626--
627-- @since 0.7.0
628{-# INLINE readIndex #-}
629readIndex :: Storable a => Handle -> Int -> Maybe a
630readIndex arr i = undefined
632-- NOTE: To represent a range to read we have chosen (start, size) instead of
633-- (start, end). This helps in removing the ambiguity of whether "end" is
634-- included in the range or not.
635--
636-- We could avoid specifying the range to be read and instead use "take size"
637-- on the stream, but it may end up reading more and then consume it partially.
639-- | @readSliceWith chunkSize handle pos len@ reads up to @len@ bytes
640-- from @handle@ starting at the offset @pos@ from the beginning of the file.
641--
642-- Reads are performed in chunks of size @chunkSize@. For block devices, to
643-- avoid reading partial blocks @chunkSize@ must align with the block size of
644-- the underlying device. If the underlying block size is unknown, it is a good
645-- idea to keep it a multiple 4KiB. This API ensures that the start of each
646-- chunk is aligned with @chunkSize@ from second chunk onwards.
647--
648{-# INLINE readSliceWith #-}
649readSliceWith :: (IsStream t, MonadIO m, Storable a)
650 => Int -> Handle -> Int -> Int -> t m a
651readSliceWith chunkSize h pos len = undefined
653-- | @readSlice h i count@ streams a slice from the file handle @h@ starting
654-- at index @i@ and reading up to @count@ elements in the forward direction
655-- ending at the index @i + count - 1@.
656--
657-- @since 0.7.0
658{-# INLINE readSlice #-}
659readSlice :: (IsStream t, MonadIO m, Storable a)
660 => Handle -> Int -> Int -> t m a
661readSlice = readSliceWith A.defaultChunkSize
663-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
664-- at index @i@ and reading up to @count@ elements in the reverse direction
665-- ending at the index @i - count + 1@.
666--
667-- @since 0.7.0
668{-# INLINE readSliceRev #-}
669readSliceRev :: (IsStream t, MonadIO m, Storable a)
670 => Handle -> Int -> Int -> t m a
671readSliceRev h i count = undefined
673-- | Write the given element at the given index in the file.
674--
675-- @since 0.7.0
676{-# INLINE writeIndex #-}
677writeIndex :: (MonadIO m, Storable a) => Handle -> Int -> a -> m ()
678writeIndex h i a = undefined
680-- | @writeSlice h i count stream@ writes a stream to the file handle @h@
681-- starting at index @i@ and writing up to @count@ elements in the forward
682-- direction ending at the index @i + count - 1@.
683--
684-- @since 0.7.0
685{-# INLINE writeSlice #-}
686writeSlice :: (IsStream t, Monad m, Storable a)
687 => Handle -> Int -> Int -> t m a -> m ()
688writeSlice h i len s = undefined
690-- | @writeSliceRev h i count stream@ writes a stream to the file handle @h@
691-- starting at index @i@ and writing up to @count@ elements in the reverse
692-- direction ending at the index @i - count + 1@.
693--
694-- @since 0.7.0
695{-# INLINE writeSliceRev #-}
696writeSliceRev :: (IsStream t, Monad m, Storable a)
697 => Handle -> Int -> Int -> t m a -> m ()
698writeSliceRev arr i len s = undefined
699-}