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