on ea224a4 help sign in
758 lines 28 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 -- * Byte Stream Read
34 , read
35 -- , readUtf8
36 -- , readLines
37 -- , readFrames
38 , readWith
40 , getBytes
41 , getBytesWith
43 -- * Chunked Stream Read
44 , readChunks
45 , readChunksWith
47 , getChunksWith
48 , getChunks
50 -- * Byte Stream Write
51 -- Byte stream write (Folds)
52 , write
53 , consumer
54 -- , writeUtf8
55 -- , writeUtf8ByLines
56 -- , writeByFrames
57 -- , writeLines
58 , writeWith
59 , writeMaybesWith
61 , putBytes
62 , putBytesWith
64 -- * Chunked Stream Write
65 , writeChunks
66 , writeChunksWith
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 , readChunksFromToWith
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
104 -- * Deprecated
105 , readChunksWithBufferOf
106 , readWithBufferOf
107 , writeChunksWithBufferOf
108 , writeWithBufferOf
109 )
110where
112import Control.Exception (assert)
113import Control.Monad.IO.Class (MonadIO(..))
114import Data.Function ((&))
115import Data.Maybe (isNothing, fromJust)
116import Data.Word (Word8)
117import Streamly.Internal.Data.Unboxed (Unboxed)
118import System.IO (Handle, SeekMode(..), hGetBufSome, hPutBuf, hSeek)
119import Prelude hiding (read)
121import Streamly.Internal.Data.Fold (Fold)
122import Streamly.Internal.Data.Refold.Type (Refold(..))
123import Streamly.Internal.Data.Unfold.Type (Unfold(..))
124import Streamly.Internal.Data.Array.Unboxed.Type
125 (Array(..), writeNUnsafe, unsafeFreezeWithShrink, byteLength)
126import Streamly.Internal.Data.Stream.Type (Stream)
127import Streamly.Internal.Data.Stream.IsStream.Type
128 (IsStream, mkStream, fromStreamD)
129import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
130-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
131import Streamly.Internal.System.IO (defaultChunkSize)
133import qualified Streamly.Internal.Data.Array.Unboxed as A
134import qualified Streamly.Internal.Data.Array.Unboxed.Type as A
135import qualified Streamly.Internal.Data.Array.Unboxed.Mut.Type as MArray
136import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
137import qualified Streamly.Internal.Data.Refold.Type as Refold
138import qualified Streamly.Internal.Data.Fold as FL
139import qualified Streamly.Internal.Data.Fold.Type as FL
140import qualified Streamly.Internal.Data.Stream.IsStream as S
141import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
142import qualified Streamly.Internal.Data.Unfold as UF
144-- $setup
145-- >>> import qualified Streamly.Data.Array.Unboxed as Array
146-- >>> import qualified Streamly.Data.Fold as Fold
147-- >>> import qualified Streamly.Data.Unfold as Unfold
148-- >>> import qualified Streamly.FileSystem.Handle as Handle
149-- >>> import qualified Streamly.Prelude as Stream
150--
151-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
152-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (first)
153-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
154-- >>> import qualified Streamly.Internal.System.IO as IO (defaultChunkSize)
156-------------------------------------------------------------------------------
157-- References
158-------------------------------------------------------------------------------
159--
160-- The following references may be useful to build an understanding about the
161-- file API design:
162--
163-- http://www.linux-mag.com/id/308/ for blocking/non-blocking IO on linux.
164-- https://lwn.net/Articles/612483/ Non-blocking buffered file read operations
165-- https://en.wikipedia.org/wiki/C_file_input/output for C APIs.
166-- https://docs.oracle.com/javase/tutorial/essential/io/file.html for Java API.
167-- https://www.w3.org/TR/FileAPI/ for http file API.
169-------------------------------------------------------------------------------
170-- Array IO (Input)
171-------------------------------------------------------------------------------
173-- | Read a 'ByteArray' consisting of one or more bytes from a file handle. If
174-- no data is available on the handle it blocks until at least one byte becomes
175-- available. If any data is available then it immediately returns that data
176-- without blocking. As a result of this behavior, it may read less than or
177-- equal to the size requested.
178--
179-- @since 0.8.1
180{-# INLINABLE getChunk #-}
181getChunk :: MonadIO m => Int -> Handle -> m (Array Word8)
182getChunk size h = liftIO $ do
183 arr <- MArray.newPinnedArrayBytes 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-- @since 0.9.0
212{-# INLINE _getChunksWith #-}
213_getChunksWith :: (IsStream t, MonadIO m)
214 => Int -> Handle -> t m (Array Word8)
215_getChunksWith size h = go
216 where
217 -- XXX use cons/nil instead
218 go = mkStream $ \_ yld _ stp -> do
219 arr <- getChunk size h
220 if byteLength arr == 0
221 then stp
222 else yld arr go
224-- | @getChunksWith size handle@ reads a stream of arrays from the file
225-- handle @handle@. The maximum size of a single array is limited to @size@.
226-- The actual size read may be less than or equal to @size@.
227--
228-- >>> getChunksWith size h = Stream.unfold Handle.readChunksWith (size, h)
229--
230-- @since 0.9.0
231{-# INLINE_NORMAL getChunksWith #-}
232getChunksWith :: (IsStream t, MonadIO m) =>
233 Int -> Handle -> t m (Array Word8)
234getChunksWith size h = fromStreamD (D.Stream step ())
235 where
236 {-# INLINE_LATE step #-}
237 step _ _ = do
238 arr <- getChunk size h
239 return $
240 case byteLength arr of
241 0 -> D.Stop
242 _ -> D.Yield arr ()
244-- | Unfold the tuple @(bufsize, handle)@ into a stream of 'Word8' arrays.
245-- Read requests to the IO device are performed using a buffer of size
246-- @bufsize@. The size of an array in the resulting stream is always less than
247-- or equal to @bufsize@.
248--
249-- @since 0.9.0
250{-# INLINE_NORMAL readChunksWith #-}
251readChunksWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
252readChunksWith =
253 UF.lmap (uncurry getChunk) UF.repeatM
254 & UF.takeWhile ((/= 0) . byteLength)
257-- | Same as 'readChunksWith'
258--
259-- @since 0.7.0
260{-# DEPRECATED readChunksWithBufferOf "Please use readChunksWith instead." #-}
261{-# INLINE_NORMAL readChunksWithBufferOf #-}
262readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
263readChunksWithBufferOf = readChunksWith
265-- There are two ways to implement this.
266--
267-- 1. Idiomatic: use a scan on the output of readChunksWith to total
268-- the array lengths and trim the last array to correct size.
269-- 2. Simply implement it from scratch like readChunksWith.
270--
271-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
272--
273-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
274-- reading from the offset `from` in the file and reads up to the offset `to`.
275--
276--
277{-# INLINE_NORMAL readChunksFromToWith #-}
278readChunksFromToWith :: MonadIO m =>
279 Unfold m (Int, Int, Int, Handle) (Array Word8)
280readChunksFromToWith = Unfold step inject
282 where
284 inject (from, to, bufSize, h) = do
285 liftIO $ hSeek h AbsoluteSeek $ fromIntegral from
286 -- XXX Use a strict Tuple?
287 return (to - from + 1, bufSize, h)
289 {-# INLINE_LATE step #-}
290 step (remaining, bufSize, h) =
291 if remaining <= 0
292 then return D.Stop
293 else do
294 arr <- getChunk (min bufSize remaining) h
295 return $
296 case byteLength arr of
297 0 -> D.Stop
298 len ->
299 assert (len <= remaining)
300 $ D.Yield arr (remaining - len, bufSize, h)
303-- XXX read 'Array a' instead of Word8
304--
305-- | @getChunks handle@ reads a stream of arrays from the specified file
306-- handle. The maximum size of a single array is limited to
307-- @defaultChunkSize@. The actual size read may be less than or equal to
308-- @defaultChunkSize@.
309--
310-- >>> getChunks = Handle.getChunksWith IO.defaultChunkSize
311--
312-- @since 0.9.0
313{-# INLINE getChunks #-}
314getChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
315getChunks = getChunksWith defaultChunkSize
317-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
318-- device are performed using a buffer of size
319-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'. The
320-- size of arrays in the resulting stream are therefore less than or equal to
321-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
322--
323-- >>> readChunks = Unfold.first IO.defaultChunkSize Handle.readChunksWith
324--
325-- @since 0.7.0
326{-# INLINE readChunks #-}
327readChunks :: MonadIO m => Unfold m Handle (Array Word8)
328readChunks = UF.first defaultChunkSize readChunksWith
330-------------------------------------------------------------------------------
331-- Read File to Stream
332-------------------------------------------------------------------------------
334-- TODO for concurrent streams implement readahead IO. We can send multiple
335-- read requests at the same time. For serial case we can use async IO. We can
336-- also control the read throughput in mbps or IOPS.
338-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
339-- to the IO device are performed using buffers of @bufsize@.
340--
341-- >>> readWith = Unfold.many Array.read Handle.readChunksWith
342--
343-- @since 0.9.0
344{-# INLINE readWith #-}
345readWith :: MonadIO m => Unfold m (Int, Handle) Word8
346readWith = UF.many A.read readChunksWith
348-- | Same as 'readWith'
349--
350-- @since 0.7.0
351{-# DEPRECATED readWithBufferOf "Please use readWith instead." #-}
352{-# INLINE readWithBufferOf #-}
353readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
354readWithBufferOf = readWith
356-- | @getBytesWith bufsize handle@ reads a byte stream from a file
357-- handle, reads are performed in chunks of up to @bufsize@.
358--
359-- >>> getBytesWith size h = Stream.unfoldMany Array.read $ Handle.getChunksWith size h
360--
361-- /Pre-release/
362{-# INLINE getBytesWith #-}
363getBytesWith :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
364getBytesWith size h = AS.concat $ getChunksWith size h
366-- TODO
367-- Generate a stream of elements of the given type from a file 'Handle'.
368-- read :: (IsStream t, MonadIO m, Unboxed a) => Handle -> t m a
369--
370-- | Unfolds a file handle into a byte stream. IO requests to the device are
371-- performed in sizes of
372-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
373--
374-- >>> read = Unfold.many Handle.readChunks Array.read
375--
376-- @since 0.7.0
377{-# INLINE read #-}
378read :: MonadIO m => Unfold m Handle Word8
379read = UF.many A.read readChunks
381-- | Generate a byte stream from a file 'Handle'.
382--
383-- >>> getBytes h = Stream.unfoldMany Array.read $ Handle.getChunks h
384--
385-- /Pre-release/
386{-# INLINE getBytes #-}
387getBytes :: (IsStream t, MonadIO m) => Handle -> t m Word8
388getBytes = AS.concat . getChunks
390-------------------------------------------------------------------------------
391-- Writing
392-------------------------------------------------------------------------------
394-------------------------------------------------------------------------------
395-- Array IO (output)
396-------------------------------------------------------------------------------
398-- | Write an 'Array' to a file handle.
399--
400-- @since 0.8.1
401{-# INLINABLE putChunk #-}
402putChunk :: MonadIO m => Handle -> Array a -> m ()
403putChunk _ arr | byteLength arr == 0 = return ()
404putChunk h arr = A.asPtrUnsafe arr $ \ptr ->
405 liftIO $ hPutBuf h ptr aLen
407 where
409 -- XXX We should have the length passed by asPtrUnsafe itself.
410 aLen = A.byteLength arr
412-------------------------------------------------------------------------------
413-- Stream of Arrays IO
414-------------------------------------------------------------------------------
416-------------------------------------------------------------------------------
417-- Writing
418-------------------------------------------------------------------------------
420-- XXX use an unfold to fromObjects or fromUnfold so that we can put any object
421-- | Write a stream of arrays to a handle.
422--
423-- >>> putChunks h = Stream.mapM_ (Handle.putChunk h)
424--
425-- @since 0.7.0
426{-# INLINE putChunks #-}
427putChunks :: MonadIO m => Handle -> Stream m (Array a) -> m ()
428putChunks h = S.mapM_ (putChunk h)
430-- XXX AS.compact can be written idiomatically in terms of foldMany, just like
431-- AS.concat is written in terms of foldMany. Once that is done we can write
432-- idiomatic def in the docs.
433--
434-- | @putChunksWith bufsize handle stream@ writes a stream of arrays
435-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
436-- The chunk size is only a maximum and the actual writes could be smaller as
437-- we do not split the arrays to fit exactly to the specified size.
438--
439-- @since 0.9.0
440{-# INLINE putChunksWith #-}
441putChunksWith :: (MonadIO m, Unboxed a)
442 => Int -> Handle -> Stream m (Array a) -> m ()
443putChunksWith n h xs = putChunks h $ AS.compact n xs
445-- | @putBytesWith bufsize handle stream@ writes @stream@ to @handle@
446-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
447-- collect the required input size.
448--
449-- >>> putBytesWith n h m = Handle.putChunks h $ Stream.arraysOf n m
450--
451-- @since 0.9.0
452{-# INLINE putBytesWith #-}
453putBytesWith :: MonadIO m => Int -> Handle -> Stream m Word8 -> m ()
454putBytesWith n h m = putChunks h $ S.arraysOf n m
456-- putBytesWith n h m = putChunks h $ AS.arraysOf n m
458-- | Write a byte stream to a file handle. Accumulates the input in chunks of
459-- up to 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize' before writing.
460--
461-- NOTE: This may perform better than the 'write' fold, you can try this if you
462-- need some extra perf boost.
463--
464-- >>> putBytes = Handle.putBytesWith IO.defaultChunkSize
465--
466-- @since 0.7.0
467{-# INLINE putBytes #-}
468putBytes :: MonadIO m => Handle -> Stream m Word8 -> m ()
469putBytes = putBytesWith defaultChunkSize
471-- | Write a stream of arrays to a handle. Each array in the stream is written
472-- to the device as a separate IO request.
473--
474-- writeChunks h = Fold.drainBy (Handle.putChunk h)
475--
476-- @since 0.7.0
477{-# INLINE writeChunks #-}
478writeChunks :: MonadIO m => Handle -> Fold m (Array a) ()
479writeChunks h = FL.drainBy (putChunk h)
481-- | Like writeChunks but uses the experimental 'Refold' API.
482--
483-- /Internal/
484{-# INLINE consumeChunks #-}
485consumeChunks :: MonadIO m => Refold m Handle (Array a) ()
486consumeChunks = Refold.drainBy putChunk
488-- XXX lpackArraysChunksOf should be written idiomatically
489--
490-- | @writeChunksWith bufsize handle@ writes a stream of arrays
491-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
492-- We never split an array, if a single array is bigger than the specified size
493-- it emitted as it is. Multiple arrays are coalesed as long as the total size
494-- remains below the specified size.
495--
496-- @since 0.9.0
497{-# INLINE writeChunksWith #-}
498writeChunksWith :: (MonadIO m, Unboxed a)
499 => Int -> Handle -> Fold m (Array a) ()
500writeChunksWith n h = lpackArraysChunksOf n (writeChunks h)
502-- | Same as 'writeChunksWith'
503--
504-- @since 0.7.0
505{-# DEPRECATED writeChunksWithBufferOf "Please use writeChunksWith instead." #-}
506{-# INLINE writeChunksWithBufferOf #-}
507writeChunksWithBufferOf :: (MonadIO m, Unboxed a)
508 => Int -> Handle -> Fold m (Array a) ()
509writeChunksWithBufferOf = writeChunksWith
511-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
512--
513-- XXX test this
514-- Note that if you use a chunk size less than 8K (GHC's default buffer
515-- size) then you are advised to use 'NOBuffering' mode on the 'Handle' in case you
516-- do not want buffering to occur at GHC level as well. Same thing applies to
517-- writes as well.
519-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
520--
521-- | @writeWith reqSize handle@ writes the input stream to @handle@.
522-- Bytes in the input stream are collected into a buffer until we have a chunk
523-- of @reqSize@ and then written to the IO device.
524--
525-- >>> writeWith n h = Fold.chunksOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
526--
527-- @since 0.7.0
528{-# INLINE writeWith #-}
529writeWith :: MonadIO m => Int -> Handle -> Fold m Word8 ()
530writeWith n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
532-- | Same as 'writeWith'
533--
534-- @since 0.7.0
535{-# DEPRECATED writeWithBufferOf "Please use writeWith instead." #-}
536{-# INLINE writeWithBufferOf #-}
537writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
538writeWithBufferOf = writeWith
540-- | Write a stream of 'Maybe' values. Keep buffering the just values in an
541-- array until a 'Nothing' is encountered or the buffer size exceeds the
542-- specified limit, at that point flush the buffer to the handle.
543--
544-- /Pre-release/
545{-# INLINE writeMaybesWith #-}
546writeMaybesWith :: (MonadIO m )
547 => Int -> Handle -> Fold m (Maybe Word8) ()
548writeMaybesWith n h =
549 let writeNJusts = FL.lmap fromJust $ A.writeN n
550 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
551 in FL.many writeOnNothing (writeChunks h)
553-- | Like 'writeWith' but uses the experimental 'Refold' API.
554--
555-- /Internal/
556{-# INLINE consumerWith #-}
557consumerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
558consumerWith n =
559 FL.refoldMany (FL.take n $ writeNUnsafe n) consumeChunks
561-- | Write a byte stream to a file handle. Accumulates the input in chunks of
562-- up to 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize' before writing
563-- to the IO device.
564--
565-- >>> write = Handle.writeWith IO.defaultChunkSize
566--
567-- @since 0.7.0
568{-# INLINE write #-}
569write :: MonadIO m => Handle -> Fold m Word8 ()
570write = writeWith defaultChunkSize
572-- | Like 'write' but uses the experimental 'Refold' API.
573--
574-- /Internal/
575{-# INLINE consumer #-}
576consumer :: MonadIO m => Refold m Handle Word8 ()
577consumer = consumerWith defaultChunkSize
579{-
580{-# INLINE write #-}
581write :: (MonadIO m, Unboxed a) => Handle -> Stream m a -> m ()
582write = toHandleWith A.defaultChunkSize
583-}
585-- XXX mmap a file into an array. This could be useful for in-place operations
586-- on a file. For example, we can quicksort the contents of a file by mmapping
587-- it.
589-------------------------------------------------------------------------------
590-- IO with encoding/decoding Unicode characters
591-------------------------------------------------------------------------------
593{-
594-- |
595-- > readUtf8 = decodeUtf8 . read
596--
597-- Read a UTF8 encoded stream of unicode characters from a file handle.
598--
599-- @since 0.7.0
600{-# INLINE readUtf8 #-}
601readUtf8 :: (IsStream t, MonadIO m) => Handle -> t m Char
602readUtf8 = decodeUtf8 . read
604-- |
605-- > writeUtf8 h s = write h $ encodeUtf8 s
606--
607-- Encode a stream of unicode characters to UTF8 and write it to the given file
608-- handle. Default block buffering applies to the writes.
609--
610-- @since 0.7.0
611{-# INLINE writeUtf8 #-}
612writeUtf8 :: MonadIO m => Handle -> Stream m Char -> m ()
613writeUtf8 h s = write h $ encodeUtf8 s
615-- | Write a stream of unicode characters after encoding to UTF-8 in chunks
616-- separated by a linefeed character @'\n'@. If the size of the buffer exceeds
617-- @defaultChunkSize@ and a linefeed is not yet found, the buffer is written
618-- anyway. This is similar to writing to a 'Handle' with the 'LineBuffering'
619-- option.
620--
621-- @since 0.7.0
622{-# INLINE writeUtf8ByLines #-}
623writeUtf8ByLines :: (IsStream t, MonadIO m) => Handle -> t m Char -> m ()
624writeUtf8ByLines = undefined
626-- | Read UTF-8 lines from a file handle and apply the specified fold to each
627-- line. This is similar to reading a 'Handle' with the 'LineBuffering' option.
628--
629-- @since 0.7.0
630{-# INLINE readLines #-}
631readLines :: (IsStream t, MonadIO m) => Handle -> Fold m Char b -> t m b
632readLines h f = foldLines (readUtf8 h) f
634-------------------------------------------------------------------------------
635-- Framing on a sequence
636-------------------------------------------------------------------------------
638-- | Read a stream from a file handle and split it into frames delimited by
639-- the specified sequence of elements. The supplied fold is applied on each
640-- frame.
641--
642-- @since 0.7.0
643{-# INLINE readFrames #-}
644readFrames :: (IsStream t, MonadIO m, Unboxed a)
645 => Array a -> Handle -> Fold m a b -> t m b
646readFrames = undefined -- foldFrames . read
648-- | Write a stream to the given file handle buffering up to frames separated
649-- by the given sequence or up to a maximum of @defaultChunkSize@.
650--
651-- @since 0.7.0
652{-# INLINE writeByFrames #-}
653writeByFrames :: (IsStream t, MonadIO m, Unboxed a)
654 => Array a -> Handle -> t m a -> m ()
655writeByFrames = undefined
657-------------------------------------------------------------------------------
658-- Framing by time
659-------------------------------------------------------------------------------
661-- | Write collecting the input in sessions of n seconds or if chunkSize
662-- gets exceeded.
663{-# INLINE writeByChunksOrSessionsOf #-}
664writeByChunksOrSessionsOf :: MonadIO m
665 => Int -> Double -> Handle -> Stream m Word8 -> m ()
666writeByChunksOrSessionsOf chunkSize sessionSize h m = undefined
668-- | Write collecting the input in sessions of n seconds or if defaultChunkSize
669-- gets exceeded.
670{-# INLINE writeBySessionsOf #-}
671writeBySessionsOf :: MonadIO m => Double -> Handle -> Stream m Word8 -> m ()
672writeBySessionsOf n = writeByChunksOrSessionsOf defaultChunkSize n
674-------------------------------------------------------------------------------
675-- Random Access IO (Seek)
676-------------------------------------------------------------------------------
678-- XXX handles could be shared, so we may not want to use the handle state at
679-- all for these APIs. we can use pread and pwrite instead. On windows we will
680-- need to use readFile/writeFile with an offset argument.
682-------------------------------------------------------------------------------
684-- | Read the element at the given index treating the file as an array.
685--
686-- @since 0.7.0
687{-# INLINE readIndex #-}
688readIndex :: Unboxed a => Handle -> Int -> Maybe a
689readIndex arr i = undefined
691-- NOTE: To represent a range to read we have chosen (start, size) instead of
692-- (start, end). This helps in removing the ambiguity of whether "end" is
693-- included in the range or not.
694--
695-- We could avoid specifying the range to be read and instead use "take size"
696-- on the stream, but it may end up reading more and then consume it partially.
698-- | @readSliceWith chunkSize handle pos len@ reads up to @len@ bytes
699-- from @handle@ starting at the offset @pos@ from the beginning of the file.
700--
701-- Reads are performed in chunks of size @chunkSize@. For block devices, to
702-- avoid reading partial blocks @chunkSize@ must align with the block size of
703-- the underlying device. If the underlying block size is unknown, it is a good
704-- idea to keep it a multiple 4KiB. This API ensures that the start of each
705-- chunk is aligned with @chunkSize@ from second chunk onwards.
706--
707{-# INLINE readSliceWith #-}
708readSliceWith :: (IsStream t, MonadIO m, Unboxed a)
709 => Int -> Handle -> Int -> Int -> t m a
710readSliceWith chunkSize h pos len = undefined
712-- | @readSlice h i count@ streams a slice from the file handle @h@ starting
713-- at index @i@ and reading up to @count@ elements in the forward direction
714-- ending at the index @i + count - 1@.
715--
716-- @since 0.7.0
717{-# INLINE readSlice #-}
718readSlice :: (IsStream t, MonadIO m, Unboxed a)
719 => Handle -> Int -> Int -> t m a
720readSlice = readSliceWith A.defaultChunkSize
722-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
723-- at index @i@ and reading up to @count@ elements in the reverse direction
724-- ending at the index @i - count + 1@.
725--
726-- @since 0.7.0
727{-# INLINE readSliceRev #-}
728readSliceRev :: (IsStream t, MonadIO m, Unboxed a)
729 => Handle -> Int -> Int -> t m a
730readSliceRev h i count = undefined
732-- | Write the given element at the given index in the file.
733--
734-- @since 0.7.0
735{-# INLINE writeIndex #-}
736writeIndex :: (MonadIO m, Unboxed a) => Handle -> Int -> a -> m ()
737writeIndex h i a = undefined
739-- | @writeSlice h i count stream@ writes a stream to the file handle @h@
740-- starting at index @i@ and writing up to @count@ elements in the forward
741-- direction ending at the index @i + count - 1@.
742--
743-- @since 0.7.0
744{-# INLINE writeSlice #-}
745writeSlice :: (IsStream t, Monad m, Unboxed a)
746 => Handle -> Int -> Int -> t m a -> m ()
747writeSlice h i len s = undefined
749-- | @writeSliceRev h i count stream@ writes a stream to the file handle @h@
750-- starting at index @i@ and writing up to @count@ elements in the reverse
751-- direction ending at the index @i - count + 1@.
752--
753-- @since 0.7.0
754{-# INLINE writeSliceRev #-}
755writeSliceRev :: (IsStream t, Monad m, Unboxed a)
756 => Handle -> Int -> Int -> t m a -> m ()
757writeSliceRev arr i len s = undefined
758-}