@@ -30,41 +30,41 @@ module Streamly.Internal.FileSystem.Handle
30 , getChunkOf
30 , getChunkOf
31 , putChunk
31 , putChunk
33− -- * Byte Stream Read
33+ -- * Streams
38 , readWith
35 , readWith
43− -- * Chunked Stream Read
45 , readChunksWith
36 , readChunksWith
47− , getChunksWith
39+ -- * Unfolds
48− , getChunks
40+ , reader
50− -- * Byte Stream Write
51− -- Byte stream write (Folds)
48+ -- * Folds
54 -- , writeUtf8
50 -- , writeUtf8
55 -- , writeUtf8ByLines
51 -- , writeUtf8ByLines
56 -- , writeByFrames
52 -- , writeByFrames
57 -- , writeLines
53 -- , writeLines
58 , writeWith
54 , writeWith
59 , writeMaybesWith
57 , writeMaybesWith
61 , putBytes
66 , putBytes
62 , putBytesWith
67 , putBytesWith
64− -- * Chunked Stream Write
68 , putChunksWith
68 , putChunksWith
69 , putChunks
69 , putChunks
@@ -86,7 +86,7 @@ module Streamly.Internal.FileSystem.Handle
87 -- , readChunksFrom
87 -- , readChunksFrom
88 -- , readChunksFromTo
88 -- , readChunksFromTo
89− , readChunksFromToWith
89+ , chunkReaderFromToWith
90 -- , readChunksFromThenToWith
90 -- , readChunksFromThenToWith
92 -- , writeIndex
92 -- , writeIndex
@@ -101,6 +101,7 @@ module Streamly.Internal.FileSystem.Handle
101 -- , writeChunksFromTo
101 -- , writeChunksFromTo
102 -- , writeChunksFromToWith
102 -- , writeChunksFromToWith
103 -- , writeChunksFromThenToWith
103 -- , writeChunksFromThenToWith
104 -- * Deprecated
105 -- * Deprecated
105 , readChunksWithBufferOf
106 , readChunksWithBufferOf
106 , readWithBufferOf
107 , readWithBufferOf
@@ -145,9 +146,9 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K (mkStream)
145-- >>> import qualified Streamly.Data.Array.Unboxed as Array
146-- >>> import qualified Streamly.Data.Array.Unboxed as Array
146-- >>> import qualified Streamly.Data.Fold as Fold
147-- >>> import qualified Streamly.Data.Fold as Fold
147-- >>> import qualified Streamly.Data.Unfold as Unfold
148-- >>> import qualified Streamly.Data.Unfold as Unfold
148−-- >>> import qualified Streamly.FileSystem.Handle as Handle
149-- >>> import qualified Streamly.Prelude as Stream
149-- >>> import qualified Streamly.Prelude as Stream
151+-- >>> import qualified Streamly.Internal.Data.Array.Unboxed.Type as Array (writeNUnsafe)
151-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
152-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
152-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (first)
153-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (first)
153-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
154-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
@@ -220,16 +221,16 @@ _getChunksWith size h = S.fromStreamK go
221 else yld arr go
222 else yld arr go
223−-- | @getChunksWith size handle@ reads a stream of arrays from the file
224+-- | @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-- 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-- The actual size read may be less than or equal to @size@.
227−-- >>> getChunksWith size h = Stream.unfold Handle.readChunksWith (size, h)
228+-- >>> readChunksWith size h = Stream.unfold Handle.chunkReaderWith (size, h)
229-- @since 0.9.0
230-- @since 0.9.0
230−{-# INLINE_NORMAL getChunksWith #-}
231+{-# INLINE_NORMAL readChunksWith #-}
231−getChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
232+readChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
232−getChunksWith size h = S.fromStreamD (D.Stream step ())
233+readChunksWith size h = S.fromStreamD (D.Stream step ())
234 {-# INLINE_LATE step #-}
235 {-# INLINE_LATE step #-}
235 step _ _ = do
236 step _ _ = do
@@ -245,20 +246,19 @@ getChunksWith size h = S.fromStreamD (D.Stream step ())
245-- or equal to @bufsize@.
246-- or equal to @bufsize@.
247-- @since 0.9.0
248-- @since 0.9.0
248−{-# INLINE_NORMAL readChunksWith #-}
249+{-# INLINE_NORMAL chunkReaderWith #-}
249−readChunksWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
250+chunkReaderWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
250−readChunksWith =
251+chunkReaderWith =
251 UF.lmap (uncurry getChunk) UF.repeatM
252 UF.lmap (uncurry getChunk) UF.repeatM
252 & UF.takeWhile ((/= 0) . byteLength)
253 & UF.takeWhile ((/= 0) . byteLength)
254−
255+-- | Same as 'chunkReaderWith'
255−-- | Same as 'readChunksWith'
257-- @since 0.7.0
257-- @since 0.7.0
258−{-# DEPRECATED readChunksWithBufferOf "Please use readChunksWith instead." #-}
258+{-# DEPRECATED readChunksWithBufferOf "Please use chunkReaderWith instead." #-}
259{-# INLINE_NORMAL readChunksWithBufferOf #-}
259{-# INLINE_NORMAL readChunksWithBufferOf #-}
260readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
260readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
261−readChunksWithBufferOf = readChunksWith
261+readChunksWithBufferOf = chunkReaderWith
263-- There are two ways to implement this.
263-- There are two ways to implement this.
@@ -267,15 +267,14 @@ readChunksWithBufferOf = readChunksWith
267-- 2. Simply implement it from scratch like readChunksWith.
267-- 2. Simply implement it from scratch like readChunksWith.
269-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
269-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
271-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
271-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
272-- reading from the offset `from` in the file and reads up to the offset `to`.
272-- reading from the offset `from` in the file and reads up to the offset `to`.
274−--
274+{-# INLINE_NORMAL chunkReaderFromToWith #-}
275−{-# INLINE_NORMAL readChunksFromToWith #-}
275+chunkReaderFromToWith :: MonadIO m =>
276−readChunksFromToWith :: MonadIO m =>
277 Unfold m (Int, Int, Int, Handle) (Array Word8)
276 Unfold m (Int, Int, Int, Handle) (Array Word8)
278−readChunksFromToWith = Unfold step inject
277+chunkReaderFromToWith = Unfold step inject
@@ -297,20 +296,17 @@ readChunksFromToWith = Unfold step inject
297 assert (len <= remaining)
296 assert (len <= remaining)
298 $ D.Yield arr (remaining - len, bufSize, h)
297 $ D.Yield arr (remaining - len, bufSize, h)
301−-- XXX read 'Array a' instead of Word8
303-- | @getChunks handle@ reads a stream of arrays from the specified file
299-- | @getChunks handle@ reads a stream of arrays from the specified file
304-- handle. The maximum size of a single array is limited to
300-- handle. The maximum size of a single array is limited to
305-- @defaultChunkSize@. The actual size read may be less than or equal to
301-- @defaultChunkSize@. The actual size read may be less than or equal to
306-- @defaultChunkSize@.
302-- @defaultChunkSize@.
308−-- >>> getChunks = Handle.getChunksWith IO.defaultChunkSize
304+-- >>> readChunks = Handle.readChunksWith IO.defaultChunkSize
310−-- @since 0.9.0
306+-- /Pre-release/
311−{-# INLINE getChunks #-}
307+{-# INLINE readChunks #-}
312−getChunks :: MonadIO m => Handle -> Stream m (Array Word8)
308+readChunks :: MonadIO m => Handle -> Stream m (Array Word8)
313−getChunks = getChunksWith defaultChunkSize
309+readChunks = readChunksWith defaultChunkSize
315-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
311-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
316-- device are performed using a buffer of size
312-- device are performed using a buffer of size
@@ -318,12 +314,12 @@ getChunks = getChunksWith defaultChunkSize
318-- size of arrays in the resulting stream are therefore less than or equal to
314-- size of arrays in the resulting stream are therefore less than or equal to
319-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
315-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
321−-- >>> readChunks = Unfold.first IO.defaultChunkSize Handle.readChunksWith
317+-- >>> chunkReader = Unfold.first IO.defaultChunkSize Handle.chunkReaderWith
323−-- @since 0.7.0
319+-- @since 0.9.0
324−{-# INLINE readChunks #-}
320+{-# INLINE chunkReader #-}
325−readChunks :: MonadIO m => Unfold m Handle (Array Word8)
321+chunkReader :: MonadIO m => Unfold m Handle (Array Word8)
326−readChunks = UF.first defaultChunkSize readChunksWith
322+chunkReader = UF.first defaultChunkSize chunkReaderWith
328-------------------------------------------------------------------------------
324-------------------------------------------------------------------------------
329-- Read File to Stream
325-- Read File to Stream
@@ -336,54 +332,50 @@ readChunks = UF.first defaultChunkSize readChunksWith
336-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
332-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
337-- to the IO device are performed using buffers of @bufsize@.
333-- to the IO device are performed using buffers of @bufsize@.
339−-- >>> readWith = Unfold.many Array.read Handle.readChunksWith
335+-- >>> readerWith = Unfold.many Array.read Handle.chunkReaderWith
341-- @since 0.9.0
337-- @since 0.9.0
342−{-# INLINE readWith #-}
338+{-# INLINE readerWith #-}
343−readWith :: MonadIO m => Unfold m (Int, Handle) Word8
339+readerWith :: MonadIO m => Unfold m (Int, Handle) Word8
344−readWith = UF.many A.read readChunksWith
340+readerWith = UF.many A.read chunkReaderWith
346−-- | Same as 'readWith'
342+-- | Same as 'readerWith'
348-- @since 0.7.0
344-- @since 0.7.0
349−{-# DEPRECATED readWithBufferOf "Please use readWith instead." #-}
345+{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead." #-}
350{-# INLINE readWithBufferOf #-}
346{-# INLINE readWithBufferOf #-}
351readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
347readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
352−readWithBufferOf = readWith
348+readWithBufferOf = readerWith
354−-- | @getBytesWith bufsize handle@ reads a byte stream from a file
350+-- | @readWith bufsize handle@ reads a byte stream from a file
355-- handle, reads are performed in chunks of up to @bufsize@.
351-- handle, reads are performed in chunks of up to @bufsize@.
357−-- >>> getBytesWith size h = Stream.unfoldMany Array.read $ Handle.getChunksWith size h
353+-- >>> readWith size h = Stream.unfoldMany Array.read $ Handle.readChunksWith size h
359-- /Pre-release/
355-- /Pre-release/
360−{-# INLINE getBytesWith #-}
356+{-# INLINE readWith #-}
361−getBytesWith :: MonadIO m => Int -> Handle -> Stream m Word8
357+readWith :: MonadIO m => Int -> Handle -> Stream m Word8
362−getBytesWith size h = AS.concat $ getChunksWith size h
358+readWith size h = AS.concat $ readChunksWith size h
365−-- Generate a stream of elements of the given type from a file 'Handle'.
366−-- read :: (MonadIO m, Unboxed a) => Handle -> Stream m a
368-- | Unfolds a file handle into a byte stream. IO requests to the device are
360-- | Unfolds a file handle into a byte stream. IO requests to the device are
369-- performed in sizes of
361-- performed in sizes of
370-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
362-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
372−-- >>> read = Unfold.many Handle.readChunks Array.read
364+-- >>> reader = Unfold.many Array.read chunkReader
374−-- @since 0.7.0
366+-- @since 0.9.0
375−{-# INLINE read #-}
367+{-# INLINE reader #-}
376−read :: MonadIO m => Unfold m Handle Word8
368+reader :: MonadIO m => Unfold m Handle Word8
377−read = UF.many A.read readChunks
369+reader = UF.many A.read chunkReader
379-- | Generate a byte stream from a file 'Handle'.
371-- | Generate a byte stream from a file 'Handle'.
381−-- >>> getBytes h = Stream.unfoldMany Array.read $ Handle.getChunks h
373+-- >>> read h = Stream.unfoldMany Array.read $ Handle.readChunks h
383-- /Pre-release/
375-- /Pre-release/
384−{-# INLINE getBytes #-}
376+{-# INLINE read #-}
385−getBytes :: MonadIO m => Handle -> Stream m Word8
377+read :: MonadIO m => Handle -> Stream m Word8
386−getBytes = AS.concat . getChunks
378+read = AS.concat . readChunks
388-------------------------------------------------------------------------------
380-------------------------------------------------------------------------------
389-- Writing
381-- Writing
@@ -479,12 +471,12 @@ writeChunks h = FL.drainBy (putChunk h)
479-- | Like writeChunks but uses the experimental 'Refold' API.
471-- | Like writeChunks but uses the experimental 'Refold' API.
481-- /Internal/
473-- /Internal/
482−{-# INLINE consumeChunks #-}
474+{-# INLINE chunkWriter #-}
483−consumeChunks :: MonadIO m => Refold m Handle (Array a) ()
475+chunkWriter :: MonadIO m => Refold m Handle (Array a) ()
484−consumeChunks = Refold.drainBy putChunk
476+chunkWriter = Refold.drainBy putChunk
486-- XXX lpackArraysChunksOf should be written idiomatically
478-- XXX lpackArraysChunksOf should be written idiomatically
488-- | @writeChunksWith bufsize handle@ writes a stream of arrays
480-- | @writeChunksWith bufsize handle@ writes a stream of arrays
489-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
481-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
490-- We never split an array, if a single array is bigger than the specified size
482-- We never split an array, if a single array is bigger than the specified size
@@ -515,7 +507,7 @@ writeChunksWithBufferOf = writeChunksWith
515-- writes as well.
507-- writes as well.
517-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
509-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
519-- | @writeWith reqSize handle@ writes the input stream to @handle@.
511-- | @writeWith reqSize handle@ writes the input stream to @handle@.
520-- Bytes in the input stream are collected into a buffer until we have a chunk
512-- Bytes in the input stream are collected into a buffer until we have a chunk
521-- of @reqSize@ and then written to the IO device.
513-- of @reqSize@ and then written to the IO device.
@@ -548,13 +540,13 @@ writeMaybesWith n h =
548 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
540 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
549 in FL.many writeOnNothing (writeChunks h)
541 in FL.many writeOnNothing (writeChunks h)
551−-- | Like 'writeWith' but uses the experimental 'Refold' API.
543+-- | Like 'writeWith' but uses the experimental 'Refold' API.
553-- /Internal/
545-- /Internal/
554−{-# INLINE consumerWith #-}
546+{-# INLINE writerWith #-}
555−consumerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
547+writerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
556−consumerWith n =
548+writerWith n =
557− FL.refoldMany (FL.take n $ writeNUnsafe n) consumeChunks
549+ FL.refoldMany (FL.take n $ writeNUnsafe n) chunkWriter
559-- | Write a byte stream to a file handle. Accumulates the input in chunks of
551-- | Write a byte stream to a file handle. Accumulates the input in chunks of
560-- up to 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize' before writing
552-- up to 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize' before writing
@@ -570,15 +562,9 @@ write = writeWith defaultChunkSize
570-- | Like 'write' but uses the experimental 'Refold' API.
562-- | Like 'write' but uses the experimental 'Refold' API.
572-- /Internal/
564-- /Internal/
573−{-# INLINE consumer #-}
565+{-# INLINE writer #-}
574−consumer :: MonadIO m => Refold m Handle Word8 ()
566+writer :: MonadIO m => Refold m Handle Word8 ()
575−consumer = consumerWith defaultChunkSize
579−write :: (MonadIO m, Unboxed a) => Handle -> Stream m a -> m ()
580−write = toHandleWith A.defaultChunkSize
567+writer = writerWith defaultChunkSize
583-- XXX mmap a file into an array. This could be useful for in-place operations
569-- XXX mmap a file into an array. This could be useful for in-place operations
584-- on a file. For example, we can quicksort the contents of a file by mmapping
570-- on a file. For example, we can quicksort the contents of a file by mmapping