on 81bfdec help sign in
711 lines 27 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-- 'readChunksWith' and 'writeChunks'. Rest of this module is just
15-- combinatorial programming using these.
16--
17-- We can achieve line buffering by folding lines in the input stream into a
18-- stream of arrays using Stream.splitOn or Fold.takeEndBy_ and similar
19-- operations. One can wrap the input stream in 'Maybe' type and then use
20-- 'writeMaybesWith' to achieve user controlled buffering.
22-- TODO: Need a separate module for pread/pwrite based reading writing for
23-- seekable devices. Stateless read/write can be helpful in multithreaded
24-- applications.
25--
26module Streamly.Internal.FileSystem.Handle
27 (
28 -- * Singleton APIs
29 getChunk
30 , getChunkOf
31 , putChunk
33 -- * Streams
34 , read
35 , readWith
36 , readChunksWith
37 , readChunks
39 -- * Unfolds
40 , reader
41 -- , readUtf8
42 -- , readLines
43 -- , readFrames
44 , readerWith
45 , chunkReader
46 , chunkReaderWith
48 -- * Folds
49 , write
50 -- , writeUtf8
51 -- , writeUtf8ByLines
52 -- , writeByFrames
53 -- , writeLines
54 , writeWith
55 , writeChunks
56 , writeChunksWith
57 , writeMaybesWith
59 -- * Refolds
60 , writer
61 , writerWith
62 , chunkWriter
63 -- , chunkWriterWith
65 -- * Stream writes
66 , putBytes
67 , putBytesWith
68 , putChunksWith
69 , putChunks
71 -- * Random Access (Seek)
72 -- | Unlike the streaming APIs listed above, these APIs apply to devices or
73 -- files that have random access or seek capability. This type of devices
74 -- include disks, files, memory devices and exclude terminals, pipes,
75 -- sockets and fifos.
77 -- We can also generate the request pattern using a funciton.
78 --
79 -- , readIndex
80 -- , readFrom -- read from a given position to the end of file
81 -- , readFromRev -- read from a given position the beginning of file
82 -- , readTo -- read from beginning up to the given position
83 -- , readToRev -- read from end to the given position in file
84 -- , readFromTo
85 -- , readFromThenTo
87 -- , readChunksFrom
88 -- , readChunksFromTo
89 , chunkReaderFromToWith
90 -- , readChunksFromThenToWith
92 -- , writeIndex
93 -- , writeFrom -- start writing at the given position
94 -- , writeFromRev
95 -- , writeTo -- write from beginning up to the given position
96 -- , writeToRev
97 -- , writeFromTo
98 -- , writeFromThenTo
99 --
100 -- , writeChunksFrom
101 -- , writeChunksFromTo
102 -- , writeChunksFromToWith
103 -- , writeChunksFromThenToWith
105 -- * Deprecated
106 , readChunksWithBufferOf
107 , readWithBufferOf
108 , writeChunksWithBufferOf
109 , writeWithBufferOf
110 )
111where
113import Control.Exception (assert)
114import Control.Monad.IO.Class (MonadIO(..))
115import Data.Function ((&))
116import Data.Maybe (isNothing, fromJust)
117import Data.Word (Word8)
118import Streamly.Internal.Data.Unboxed (Unbox)
119import System.IO (Handle, SeekMode(..), hGetBufSome, hPutBuf, hSeek)
120import Prelude hiding (read)
122import Streamly.Internal.Data.Fold (Fold)
123import Streamly.Internal.Data.Refold.Type (Refold(..))
124import Streamly.Internal.Data.Unfold.Type (Unfold(..))
125import Streamly.Internal.Data.Array.Type
126 (Array(..), writeNUnsafe, unsafeFreezeWithShrink, byteLength)
127import Streamly.Internal.Data.Stream.Type (Stream)
128import Streamly.Internal.Data.Stream.Chunked (lpackArraysChunksOf)
129-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
130import Streamly.Internal.System.IO (defaultChunkSize)
132import qualified Streamly.Data.Fold as FL
133import qualified Streamly.Data.Array as A
134import qualified Streamly.Internal.Data.Array.Type as A
135import qualified Streamly.Internal.Data.Stream.Chunked as AS
136import qualified Streamly.Internal.Data.Array.Mut.Type as MArray
137import qualified Streamly.Internal.Data.Refold.Type as Refold
138import qualified Streamly.Internal.Data.Fold.Type as FL(refoldMany)
139import qualified Streamly.Internal.Data.Stream as S
140import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
141 (Stream(..), Step(..))
142import qualified Streamly.Internal.Data.Unfold as UF
143import qualified Streamly.Internal.Data.Stream.StreamK.Type as K (mkStream)
145-- $setup
146-- >>> import qualified Streamly.Data.Array as Array
147-- >>> import qualified Streamly.Data.Fold as Fold
148-- >>> import qualified Streamly.Data.Unfold as Unfold
149-- >>> import qualified Streamly.Data.Stream as Stream
150--
151-- >>> import qualified Streamly.Internal.Data.Array.Type as Array (writeNUnsafe)
152-- >>> import qualified Streamly.Internal.Data.Stream as Stream
153-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (first)
154-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
155-- >>> import qualified Streamly.Internal.System.IO as IO (defaultChunkSize)
157-------------------------------------------------------------------------------
158-- References
159-------------------------------------------------------------------------------
160--
161-- The following references may be useful to build an understanding about the
162-- file API design:
163--
164-- http://www.linux-mag.com/id/308/ for blocking/non-blocking IO on linux.
165-- https://lwn.net/Articles/612483/ Non-blocking buffered file read operations
166-- https://en.wikipedia.org/wiki/C_file_input/output for C APIs.
167-- https://docs.oracle.com/javase/tutorial/essential/io/file.html for Java API.
168-- https://www.w3.org/TR/FileAPI/ for http file API.
170-------------------------------------------------------------------------------
171-- Array IO (Input)
172-------------------------------------------------------------------------------
174-- | Read a 'ByteArray' consisting of one or more bytes from a file handle. If
175-- no data is available on the handle it blocks until at least one byte becomes
176-- available. If any data is available then it immediately returns that data
177-- without blocking. As a result of this behavior, it may read less than or
178-- equal to the size requested.
179--
180{-# INLINABLE getChunk #-}
181getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
182getChunk size h = liftIO $ do
183 arr <- MArray.newPinnedBytes size
184 -- ptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: Word8))
185 MArray.asPtrUnsafe arr $ \p -> do
186 n <- hGetBufSome h p size
187 -- XXX shrink only if the diff is significant
188 return $
189 unsafeFreezeWithShrink $
190 arr { MArray.arrEnd = n, MArray.arrBound = size }
192-- This could be useful in implementing the "reverse" read APIs or if you want
193-- to read arrays of exact size instead of compacting them later. Compacting
194-- later requires more copying.
195--
196-- | Read a 'ByteArray' consisting of exactly the specified number of bytes
197-- from a file handle.
198--
199-- /Unimplemented/
200{-# INLINABLE getChunkOf #-}
201getChunkOf :: Int -> Handle -> IO (Array Word8)
202getChunkOf = undefined
204-------------------------------------------------------------------------------
205-- Stream of Arrays IO
206-------------------------------------------------------------------------------
208-- | @getChunksWith size h@ reads a stream of arrays from file handle @h@.
209-- The maximum size of a single array is specified by @size@. The actual size
210-- read may be less than or equal to @size@.
211--
212{-# INLINE _getChunksWith #-}
213_getChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
214_getChunksWith size h = S.fromStreamK go
215 where
216 -- XXX use cons/nil instead
217 go = K.mkStream $ \_ yld _ stp -> do
218 arr <- getChunk size h
219 if byteLength arr == 0
220 then stp
221 else yld arr go
223-- | @readChunksWith size handle@ reads a stream of arrays from the file
224-- handle @handle@. The maximum size of a single array is limited to @size@.
225-- The actual size read may be less than or equal to @size@.
226--
227-- >>> readChunksWith size h = Stream.unfold Handle.chunkReaderWith (size, h)
228--
229{-# INLINE_NORMAL readChunksWith #-}
230readChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
231readChunksWith size h = S.fromStreamD (D.Stream step ())
232 where
233 {-# INLINE_LATE step #-}
234 step _ _ = do
235 arr <- getChunk size h
236 return $
237 case byteLength arr of
238 0 -> D.Stop
239 _ -> D.Yield arr ()
241-- | Unfold the tuple @(bufsize, handle)@ into a stream of 'Word8' arrays.
242-- Read requests to the IO device are performed using a buffer of size
243-- @bufsize@. The size of an array in the resulting stream is always less than
244-- or equal to @bufsize@.
245--
246{-# INLINE_NORMAL chunkReaderWith #-}
247chunkReaderWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
248chunkReaderWith =
249 UF.lmap (uncurry getChunk) UF.repeatM
250 & UF.takeWhile ((/= 0) . byteLength)
252-- | Same as 'chunkReaderWith'
253--
254{-# DEPRECATED readChunksWithBufferOf "Please use chunkReaderWith instead." #-}
255{-# INLINE_NORMAL readChunksWithBufferOf #-}
256readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
257readChunksWithBufferOf = chunkReaderWith
259-- There are two ways to implement this.
260--
261-- 1. Idiomatic: use a scan on the output of readChunksWith to total
262-- the array lengths and trim the last array to correct size.
263-- 2. Simply implement it from scratch like readChunksWith.
264--
265-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
267-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
268-- reading from the offset `from` in the file and reads up to the offset `to`.
269--
270{-# INLINE_NORMAL chunkReaderFromToWith #-}
271chunkReaderFromToWith :: MonadIO m =>
272 Unfold m (Int, Int, Int, Handle) (Array Word8)
273chunkReaderFromToWith = Unfold step inject
275 where
277 inject (from, to, bufSize, h) = do
278 liftIO $ hSeek h AbsoluteSeek $ fromIntegral from
279 -- XXX Use a strict Tuple?
280 return (to - from + 1, bufSize, h)
282 {-# INLINE_LATE step #-}
283 step (remaining, bufSize, h) =
284 if remaining <= 0
285 then return D.Stop
286 else do
287 arr <- getChunk (min bufSize remaining) h
288 return $
289 case byteLength arr of
290 0 -> D.Stop
291 len ->
292 assert (len <= remaining)
293 $ D.Yield arr (remaining - len, bufSize, h)
295-- | @getChunks handle@ reads a stream of arrays from the specified file
296-- handle. The maximum size of a single array is limited to
297-- @defaultChunkSize@. The actual size read may be less than or equal to
298-- @defaultChunkSize@.
299--
300-- >>> readChunks = Handle.readChunksWith IO.defaultChunkSize
301--
302-- /Pre-release/
303{-# INLINE readChunks #-}
304readChunks :: MonadIO m => Handle -> Stream m (Array Word8)
305readChunks = readChunksWith defaultChunkSize
307-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
308-- device are performed using a buffer of size
309-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'. The
310-- size of arrays in the resulting stream are therefore less than or equal to
311-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'.
312--
313-- >>> chunkReader = Unfold.first IO.defaultChunkSize Handle.chunkReaderWith
314--
315{-# INLINE chunkReader #-}
316chunkReader :: MonadIO m => Unfold m Handle (Array Word8)
317chunkReader = UF.first defaultChunkSize chunkReaderWith
319-------------------------------------------------------------------------------
320-- Read File to Stream
321-------------------------------------------------------------------------------
323-- TODO for concurrent streams implement readahead IO. We can send multiple
324-- read requests at the same time. For serial case we can use async IO. We can
325-- also control the read throughput in mbps or IOPS.
327-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
328-- to the IO device are performed using buffers of @bufsize@.
329--
330-- >>> readerWith = Unfold.many Array.reader Handle.chunkReaderWith
331--
332{-# INLINE readerWith #-}
333readerWith :: MonadIO m => Unfold m (Int, Handle) Word8
334readerWith = UF.many A.reader chunkReaderWith
336-- | Same as 'readerWith'
337--
338{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead." #-}
339{-# INLINE readWithBufferOf #-}
340readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
341readWithBufferOf = readerWith
343-- | @readWith bufsize handle@ reads a byte stream from a file
344-- handle, reads are performed in chunks of up to @bufsize@.
345--
346-- >>> readWith size h = Stream.unfoldMany Array.reader $ Handle.readChunksWith size h
347--
348-- /Pre-release/
349{-# INLINE readWith #-}
350readWith :: MonadIO m => Int -> Handle -> Stream m Word8
351readWith size h = AS.concat $ readChunksWith size h
353-- | Unfolds a file handle into a byte stream. IO requests to the device are
354-- performed in sizes of
355-- 'Streamly.Internal.Data.Array.Type.defaultChunkSize'.
356--
357-- >>> reader = Unfold.many Array.reader chunkReader
358--
359{-# INLINE reader #-}
360reader :: MonadIO m => Unfold m Handle Word8
361reader = UF.many A.reader chunkReader
363-- | Generate a byte stream from a file 'Handle'.
364--
365-- >>> read h = Stream.unfoldMany Array.reader $ Handle.readChunks h
366--
367-- /Pre-release/
368{-# INLINE read #-}
369read :: MonadIO m => Handle -> Stream m Word8
370read = AS.concat . readChunks
372-------------------------------------------------------------------------------
373-- Writing
374-------------------------------------------------------------------------------
376-------------------------------------------------------------------------------
377-- Array IO (output)
378-------------------------------------------------------------------------------
380-- | Write an 'Array' to a file handle.
381--
382{-# INLINABLE putChunk #-}
383putChunk :: MonadIO m => Handle -> Array a -> m ()
384putChunk _ arr | byteLength arr == 0 = return ()
385putChunk h arr = A.asPtrUnsafe arr $ \ptr ->
386 liftIO $ hPutBuf h ptr aLen
388 where
390 -- XXX We should have the length passed by asPtrUnsafe itself.
391 aLen = A.byteLength arr
393-------------------------------------------------------------------------------
394-- Stream of Arrays IO
395-------------------------------------------------------------------------------
397-------------------------------------------------------------------------------
398-- Writing
399-------------------------------------------------------------------------------
401-- XXX use an unfold to fromObjects or fromUnfold so that we can put any object
402-- | Write a stream of arrays to a handle.
403--
404-- >>> putChunks h = Stream.fold (Fold.drainBy (Handle.putChunk h))
405--
406{-# INLINE putChunks #-}
407putChunks :: MonadIO m => Handle -> Stream m (Array a) -> m ()
408putChunks h = S.fold (FL.drainMapM (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-- | @putChunksWith 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{-# INLINE putChunksWith #-}
420putChunksWith :: (MonadIO m, Unbox a)
421 => Int -> Handle -> Stream m (Array a) -> m ()
422putChunksWith n h xs = putChunks h $ AS.compact n xs
424-- | @putBytesWith 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-- >>> putBytesWith n h m = Handle.putChunks h $ Stream.arraysOf n m
429--
430{-# INLINE putBytesWith #-}
431putBytesWith :: MonadIO m => Int -> Handle -> Stream m Word8 -> m ()
432putBytesWith n h m = putChunks h $ S.arraysOf n m
434-- putBytesWith 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.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.putBytesWith IO.defaultChunkSize
443--
444{-# INLINE putBytes #-}
445putBytes :: MonadIO m => Handle -> Stream m Word8 -> m ()
446putBytes = putBytesWith defaultChunkSize
448-- | Write a stream of arrays to a handle. Each array in the stream is written
449-- to the device as a separate IO request.
450--
451-- writeChunks h = Fold.drainBy (Handle.putChunk h)
452--
453{-# INLINE writeChunks #-}
454writeChunks :: MonadIO m => Handle -> Fold m (Array a) ()
455writeChunks h = FL.drainMapM (putChunk h)
457-- | Like writeChunks but uses the experimental 'Refold' API.
458--
459-- /Internal/
460{-# INLINE chunkWriter #-}
461chunkWriter :: MonadIO m => Refold m Handle (Array a) ()
462chunkWriter = Refold.drainBy putChunk
464-- XXX lpackArraysChunksOf should be written idiomatically
466-- | @writeChunksWith bufsize handle@ writes a stream of arrays
467-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
468-- We never split an array, if a single array is bigger than the specified size
469-- it emitted as it is. Multiple arrays are coalesed as long as the total size
470-- remains below the specified size.
471--
472{-# INLINE writeChunksWith #-}
473writeChunksWith :: (MonadIO m, Unbox a)
474 => Int -> Handle -> Fold m (Array a) ()
475writeChunksWith n h = lpackArraysChunksOf n (writeChunks h)
477-- | Same as 'writeChunksWith'
478--
479{-# DEPRECATED writeChunksWithBufferOf "Please use writeChunksWith instead." #-}
480{-# INLINE writeChunksWithBufferOf #-}
481writeChunksWithBufferOf :: (MonadIO m, Unbox a)
482 => Int -> Handle -> Fold m (Array a) ()
483writeChunksWithBufferOf = writeChunksWith
485-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
486--
487-- XXX test this
488-- Note that if you use a chunk size less than 8K (GHC's default buffer
489-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
490-- do not want buffering to occur at GHC level as well. Same thing applies to
491-- writes as well.
493-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
495-- | @writeWith reqSize handle@ writes the input stream to @handle@.
496-- Bytes in the input stream are collected into a buffer until we have a chunk
497-- of @reqSize@ and then written to the IO device.
498--
499-- >>> writeWith n h = Fold.chunksOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
500--
501{-# INLINE writeWith #-}
502writeWith :: MonadIO m => Int -> Handle -> Fold m Word8 ()
503writeWith n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
505-- | Same as 'writeWith'
506--
507{-# DEPRECATED writeWithBufferOf "Please use writeWith instead." #-}
508{-# INLINE writeWithBufferOf #-}
509writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
510writeWithBufferOf = writeWith
512-- | Write a stream of 'Maybe' values. Keep buffering the just values in an
513-- array until a 'Nothing' is encountered or the buffer size exceeds the
514-- specified limit, at that point flush the buffer to the handle.
515--
516-- /Pre-release/
517{-# INLINE writeMaybesWith #-}
518writeMaybesWith :: (MonadIO m )
519 => Int -> Handle -> Fold m (Maybe Word8) ()
520writeMaybesWith n h =
521 let writeNJusts = FL.lmap fromJust $ A.writeN n
522 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
523 in FL.many writeOnNothing (writeChunks h)
525-- | Like 'writeWith' but uses the experimental 'Refold' API.
526--
527-- /Internal/
528{-# INLINE writerWith #-}
529writerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
530writerWith n =
531 FL.refoldMany (FL.take n $ writeNUnsafe n) chunkWriter
533-- | Write a byte stream to a file handle. Accumulates the input in chunks of
534-- up to 'Streamly.Internal.Data.Array.Type.defaultChunkSize' before writing
535-- to the IO device.
536--
537-- >>> write = Handle.writeWith IO.defaultChunkSize
538--
539{-# INLINE write #-}
540write :: MonadIO m => Handle -> Fold m Word8 ()
541write = writeWith defaultChunkSize
543-- | Like 'write' but uses the experimental 'Refold' API.
544--
545-- /Internal/
546{-# INLINE writer #-}
547writer :: MonadIO m => Refold m Handle Word8 ()
548writer = writerWith defaultChunkSize
550-- XXX mmap a file into an array. This could be useful for in-place operations
551-- on a file. For example, we can quicksort the contents of a file by mmapping
552-- it.
554-------------------------------------------------------------------------------
555-- IO with encoding/decoding Unicode characters
556-------------------------------------------------------------------------------
558{-
559-- |
560-- > readUtf8 = decodeUtf8 . read
561--
562-- Read a UTF8 encoded stream of unicode characters from a file handle.
563--
564{-# INLINE readUtf8 #-}
565readUtf8 :: MonadIO m => Handle -> Stream m Char
566readUtf8 = decodeUtf8 . read
568-- |
569-- > writeUtf8 h s = write h $ encodeUtf8 s
570--
571-- Encode a stream of unicode characters to UTF8 and write it to the given file
572-- handle. Default block buffering applies to the writes.
573--
574{-# INLINE writeUtf8 #-}
575writeUtf8 :: MonadIO m => Handle -> Stream m Char -> m ()
576writeUtf8 h s = write h $ encodeUtf8 s
578-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
579-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
580-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
581-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
582-- option.
583--
584{-# INLINE writeUtf8ByLines #-}
585writeUtf8ByLines :: MonadIO m => Handle -> Stream m Char -> m ()
586writeUtf8ByLines = undefined
588-- | Read UTF-8 lines from a file handle and apply the specified fold to each
589-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
590--
591{-# INLINE readLines #-}
592readLines :: MonadIO m => Handle -> Fold m Char b -> Stream m b
593readLines h f = foldLines (readUtf8 h) f
595-------------------------------------------------------------------------------
596-- Framing on a sequence
597-------------------------------------------------------------------------------
599-- | Read a stream from a file handle and split it into frames delimited by
600-- the specified sequence of elements. The supplied fold is applied on each
601-- frame.
602--
603{-# INLINE readFrames #-}
604readFrames :: (MonadIO m, Unboxed a)
605 => Array a -> Handle -> Fold m a b -> Stream m b
606readFrames = undefined -- foldFrames . read
608-- | Write a stream to the given file handle buffering up to frames separated
609-- by the given sequence or up to a maximum of @defaultChunkSize@.
610--
611{-# INLINE writeByFrames #-}
612writeByFrames :: (MonadIO m, Unboxed a)
613 => Array a -> Handle -> Stream m a -> m ()
614writeByFrames = undefined
616-------------------------------------------------------------------------------
617-- Framing by time
618-------------------------------------------------------------------------------
620-- | Write collecting the input in sessions of n seconds or if chunkSize
621-- gets exceeded.
622{-# INLINE writeByChunksOrSessionsOf #-}
623writeByChunksOrSessionsOf :: MonadIO m
624 => Int -> Double -> Handle -> Stream m Word8 -> m ()
625writeByChunksOrSessionsOf chunkSize sessionSize h m = undefined
627-- | Write collecting the input in sessions of n seconds or if defaultChunkSize
628-- gets exceeded.
629{-# INLINE writeBySessionsOf #-}
630writeBySessionsOf :: MonadIO m => Double -> Handle -> Stream m Word8 -> m ()
631writeBySessionsOf n = writeByChunksOrSessionsOf defaultChunkSize n
633-------------------------------------------------------------------------------
634-- Random Access IO (Seek)
635-------------------------------------------------------------------------------
637-- XXX handles could be shared, so we may not want to use the handle state at
638-- all for these APIs. we can use pread and pwrite instead. On windows we will
639-- need to use readFile/writeFile with an offset argument.
641-------------------------------------------------------------------------------
643-- | Read the element at the given index treating the file as an array.
644--
645{-# INLINE readIndex #-}
646readIndex :: Unboxed a => Handle -> Int -> Maybe a
647readIndex arr i = undefined
649-- NOTE: To represent a range to read we have chosen (start, size) instead of
650-- (start, end). This helps in removing the ambiguity of whether "end" is
651-- included in the range or not.
652--
653-- We could avoid specifying the range to be read and instead use "take size"
654-- on the stream, but it may end up reading more and then consume it partially.
656-- | @readSliceWith chunkSize handle pos len@ reads up to @len@ bytes
657-- from @handle@ starting at the offset @pos@ from the beginning of the file.
658--
659-- Reads are performed in chunks of size @chunkSize@. For block devices, to
660-- avoid reading partial blocks @chunkSize@ must align with the block size of
661-- the underlying device. If the underlying block size is unknown, it is a good
662-- idea to keep it a multiple 4KiB. This API ensures that the start of each
663-- chunk is aligned with @chunkSize@ from second chunk onwards.
664--
665{-# INLINE readSliceWith #-}
666readSliceWith :: (MonadIO m, Unboxed a)
667 => Int -> Handle -> Int -> Int -> Stream m a
668readSliceWith chunkSize h pos len = undefined
670-- | @readSlice h i count@ streams a slice from the file handle @h@ starting
671-- at index @i@ and reading up to @count@ elements in the forward direction
672-- ending at the index @i + count - 1@.
673--
674{-# INLINE readSlice #-}
675readSlice :: (MonadIO m, Unboxed a)
676 => Handle -> Int -> Int -> Stream m a
677readSlice = readSliceWith A.defaultChunkSize
679-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
680-- at index @i@ and reading up to @count@ elements in the reverse direction
681-- ending at the index @i - count + 1@.
682--
683{-# INLINE readSliceRev #-}
684readSliceRev :: (MonadIO m, Unboxed a)
685 => Handle -> Int -> Int -> Stream m a
686readSliceRev h i count = undefined
688-- | Write the given element at the given index in the file.
689--
690{-# INLINE writeIndex #-}
691writeIndex :: (MonadIO m, Unboxed a) => Handle -> Int -> a -> m ()
692writeIndex h i a = undefined
694-- | @writeSlice h i count stream@ writes a stream to the file handle @h@
695-- starting at index @i@ and writing up to @count@ elements in the forward
696-- direction ending at the index @i + count - 1@.
697--
698{-# INLINE writeSlice #-}
699writeSlice :: (Monad m, Unboxed a)
700 => Handle -> Int -> Int -> Stream m a -> m ()
701writeSlice h i len s = undefined
703-- | @writeSliceRev h i count stream@ writes a stream to the file handle @h@
704-- starting at index @i@ and writing up to @count@ elements in the reverse
705-- direction ending at the index @i - count + 1@.
706--
707{-# INLINE writeSliceRev #-}
708writeSliceRev :: (Monad m, Unboxed a)
709 => Handle -> Int -> Int -> Stream m a -> m ()
710writeSliceRev arr i len s = undefined
711-}