@@ -30,41 +30,41 @@ module Streamly.Internal.FileSystem.Handle
3030 , getChunkOf
3131 , putChunk
3232
33− -- * Byte Stream Read
33+ -- * Streams
3434 , read
35− -- , readUtf8
36− -- , readLines
37− -- , readFrames
3835 , readWith
39−
40− , getBytes
41− , getBytesWith
42−
43− -- * Chunked Stream Read
44− , readChunks
4536 , readChunksWith
37+ , readChunks
4638
47− , getChunksWith
48− , getChunks
39+ -- * Unfolds
40+ , reader
41+ -- , readUtf8
42+ -- , readLines
43+ -- , readFrames
44+ , readerWith
45+ , chunkReader
46+ , chunkReaderWith
4947
50− -- * Byte Stream Write
51− -- Byte stream write (Folds)
48+ -- * Folds
5249 , write
53− , consumer
5450 -- , writeUtf8
5551 -- , writeUtf8ByLines
5652 -- , writeByFrames
5753 -- , writeLines
5854 , writeWith
55+ , writeChunks
56+ , writeChunksWith
5957 , writeMaybesWith
6058
59+ -- * Refolds
60+ , writer
61+ , writerWith
62+ , chunkWriter
63+ -- , chunkWriterWith
64+
65+ -- * Stream writes
6166 , putBytes
6267 , putBytesWith
63−
64− -- * Chunked Stream Write
65− , writeChunks
66− , writeChunksWith
67−
6868 , putChunksWith
6969 , putChunks
7070
@@ -86,7 +86,7 @@ module Streamly.Internal.FileSystem.Handle
8686
8787 -- , readChunksFrom
8888 -- , readChunksFromTo
89− , readChunksFromToWith
89+ , chunkReaderFromToWith
9090 -- , readChunksFromThenToWith
9191
9292 -- , writeIndex
@@ -101,6 +101,7 @@ module Streamly.Internal.FileSystem.Handle
101101 -- , writeChunksFromTo
102102 -- , writeChunksFromToWith
103103 -- , writeChunksFromThenToWith
104+
104105 -- * Deprecated
105106 , readChunksWithBufferOf
106107 , readWithBufferOf
@@ -145,9 +146,9 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K (mkStream)
145146-- >>> import qualified Streamly.Data.Array.Unboxed as Array
146147-- >>> import qualified Streamly.Data.Fold as Fold
147148-- >>> import qualified Streamly.Data.Unfold as Unfold
148−-- >>> import qualified Streamly.FileSystem.Handle as Handle
149149-- >>> import qualified Streamly.Prelude as Stream
150150--
151+-- >>> import qualified Streamly.Internal.Data.Array.Unboxed.Type as Array (writeNUnsafe)
151152-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
152153-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (first)
153154-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
@@ -220,16 +221,16 @@ _getChunksWith size h = S.fromStreamK go
220221 then stp
221222 else yld arr go
222223
223−-- | @getChunksWith size handle@ reads a stream of arrays from the file
224+-- | @readChunksWith size handle@ reads a stream of arrays from the file
224225-- handle @handle@. The maximum size of a single array is limited to @size@.
225226-- The actual size read may be less than or equal to @size@.
226227--
227−-- >>> getChunksWith size h = Stream.unfold Handle.readChunksWith (size, h)
228+-- >>> readChunksWith size h = Stream.unfold Handle.chunkReaderWith (size, h)
228229--
229230-- @since 0.9.0
230−{-# INLINE_NORMAL getChunksWith #-}
231−getChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
232−getChunksWith size h = S.fromStreamD (D.Stream step ())
231+{-# INLINE_NORMAL readChunksWith #-}
232+readChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
233+readChunksWith size h = S.fromStreamD (D.Stream step ())
233234 where
234235 {-# INLINE_LATE step #-}
235236 step _ _ = do
@@ -245,20 +246,19 @@ getChunksWith size h = S.fromStreamD (D.Stream step ())
245246-- or equal to @bufsize@.
246247--
247248-- @since 0.9.0
248−{-# INLINE_NORMAL readChunksWith #-}
249−readChunksWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
250−readChunksWith =
249+{-# INLINE_NORMAL chunkReaderWith #-}
250+chunkReaderWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
251+chunkReaderWith =
251252 UF.lmap (uncurry getChunk) UF.repeatM
252253 & UF.takeWhile ((/= 0) . byteLength)
253254
254−
255−-- | Same as 'readChunksWith'
255+-- | Same as 'chunkReaderWith'
256256--
257257-- @since 0.7.0
258−{-# DEPRECATED readChunksWithBufferOf "Please use readChunksWith instead." #-}
258+{-# DEPRECATED readChunksWithBufferOf "Please use chunkReaderWith instead." #-}
259259{-# INLINE_NORMAL readChunksWithBufferOf #-}
260260readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
261−readChunksWithBufferOf = readChunksWith
261+readChunksWithBufferOf = chunkReaderWith
262262
263263-- There are two ways to implement this.
264264--
@@ -267,15 +267,14 @@ readChunksWithBufferOf = readChunksWith
267267-- 2. Simply implement it from scratch like readChunksWith.
268268--
269269-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
270−--
270+
271271-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
272272-- reading from the offset `from` in the file and reads up to the offset `to`.
273273--
274−--
275−{-# INLINE_NORMAL readChunksFromToWith #-}
276−readChunksFromToWith :: MonadIO m =>
274+{-# INLINE_NORMAL chunkReaderFromToWith #-}
275+chunkReaderFromToWith :: MonadIO m =>
277276 Unfold m (Int, Int, Int, Handle) (Array Word8)
278−readChunksFromToWith = Unfold step inject
277+chunkReaderFromToWith = Unfold step inject
279278
280279 where
281280
@@ -297,20 +296,17 @@ readChunksFromToWith = Unfold step inject
297296 assert (len <= remaining)
298297 $ D.Yield arr (remaining - len, bufSize, h)
299298
300−
301−-- XXX read 'Array a' instead of Word8
302−--
303299-- | @getChunks handle@ reads a stream of arrays from the specified file
304300-- handle. The maximum size of a single array is limited to
305301-- @defaultChunkSize@. The actual size read may be less than or equal to
306302-- @defaultChunkSize@.
307303--
308−-- >>> getChunks = Handle.getChunksWith IO.defaultChunkSize
304+-- >>> readChunks = Handle.readChunksWith IO.defaultChunkSize
309305--
310−-- @since 0.9.0
311−{-# INLINE getChunks #-}
312−getChunks :: MonadIO m => Handle -> Stream m (Array Word8)
313−getChunks = getChunksWith defaultChunkSize
306+-- /Pre-release/
307+{-# INLINE readChunks #-}
308+readChunks :: MonadIO m => Handle -> Stream m (Array Word8)
309+readChunks = readChunksWith defaultChunkSize
314310
315311-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
316312-- device are performed using a buffer of size
@@ -318,12 +314,12 @@ getChunks = getChunksWith defaultChunkSize
318314-- size of arrays in the resulting stream are therefore less than or equal to
319315-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
320316--
321−-- >>> readChunks = Unfold.first IO.defaultChunkSize Handle.readChunksWith
317+-- >>> chunkReader = Unfold.first IO.defaultChunkSize Handle.chunkReaderWith
322318--
323−-- @since 0.7.0
324−{-# INLINE readChunks #-}
325−readChunks :: MonadIO m => Unfold m Handle (Array Word8)
326−readChunks = UF.first defaultChunkSize readChunksWith
319+-- @since 0.9.0
320+{-# INLINE chunkReader #-}
321+chunkReader :: MonadIO m => Unfold m Handle (Array Word8)
322+chunkReader = UF.first defaultChunkSize chunkReaderWith
327323
328324-------------------------------------------------------------------------------
329325-- Read File to Stream
@@ -336,54 +332,50 @@ readChunks = UF.first defaultChunkSize readChunksWith
336332-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
337333-- to the IO device are performed using buffers of @bufsize@.
338334--
339−-- >>> readWith = Unfold.many Array.read Handle.readChunksWith
335+-- >>> readerWith = Unfold.many Array.read Handle.chunkReaderWith
340336--
341337-- @since 0.9.0
342−{-# INLINE readWith #-}
343−readWith :: MonadIO m => Unfold m (Int, Handle) Word8
344−readWith = UF.many A.read readChunksWith
338+{-# INLINE readerWith #-}
339+readerWith :: MonadIO m => Unfold m (Int, Handle) Word8
340+readerWith = UF.many A.read chunkReaderWith
345341
346−-- | Same as 'readWith'
342+-- | Same as 'readerWith'
347343--
348344-- @since 0.7.0
349−{-# DEPRECATED readWithBufferOf "Please use readWith instead." #-}
345+{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead." #-}
350346{-# INLINE readWithBufferOf #-}
351347readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
352−readWithBufferOf = readWith
348+readWithBufferOf = readerWith
353349
354−-- | @getBytesWith bufsize handle@ reads a byte stream from a file
350+-- | @readWith bufsize handle@ reads a byte stream from a file
355351-- handle, reads are performed in chunks of up to @bufsize@.
356352--
357−-- >>> getBytesWith size h = Stream.unfoldMany Array.read $ Handle.getChunksWith size h
353+-- >>> readWith size h = Stream.unfoldMany Array.read $ Handle.readChunksWith size h
358354--
359355-- /Pre-release/
360−{-# INLINE getBytesWith #-}
361−getBytesWith :: MonadIO m => Int -> Handle -> Stream m Word8
362−getBytesWith size h = AS.concat $ getChunksWith size h
356+{-# INLINE readWith #-}
357+readWith :: MonadIO m => Int -> Handle -> Stream m Word8
358+readWith size h = AS.concat $ readChunksWith size h
363359
364−-- TODO
365−-- Generate a stream of elements of the given type from a file 'Handle'.
366−-- read :: (MonadIO m, Unboxed a) => Handle -> Stream m a
367−--
368360-- | Unfolds a file handle into a byte stream. IO requests to the device are
369361-- performed in sizes of
370362-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
371363--
372−-- >>> read = Unfold.many Handle.readChunks Array.read
364+-- >>> reader = Unfold.many Array.read chunkReader
373365--
374−-- @since 0.7.0
375−{-# INLINE read #-}
376−read :: MonadIO m => Unfold m Handle Word8
377−read = UF.many A.read readChunks
366+-- @since 0.9.0
367+{-# INLINE reader #-}
368+reader :: MonadIO m => Unfold m Handle Word8
369+reader = UF.many A.read chunkReader
378370
379371-- | Generate a byte stream from a file 'Handle'.
380372--
381−-- >>> getBytes h = Stream.unfoldMany Array.read $ Handle.getChunks h
373+-- >>> read h = Stream.unfoldMany Array.read $ Handle.readChunks h
382374--
383375-- /Pre-release/
384−{-# INLINE getBytes #-}
385−getBytes :: MonadIO m => Handle -> Stream m Word8
386−getBytes = AS.concat . getChunks
376+{-# INLINE read #-}
377+read :: MonadIO m => Handle -> Stream m Word8
378+read = AS.concat . readChunks
387379
388380-------------------------------------------------------------------------------
389381-- Writing
@@ -479,12 +471,12 @@ writeChunks h = FL.drainBy (putChunk h)
479471-- | Like writeChunks but uses the experimental 'Refold' API.
480472--
481473-- /Internal/
482−{-# INLINE consumeChunks #-}
483−consumeChunks :: MonadIO m => Refold m Handle (Array a) ()
484−consumeChunks = Refold.drainBy putChunk
474+{-# INLINE chunkWriter #-}
475+chunkWriter :: MonadIO m => Refold m Handle (Array a) ()
476+chunkWriter = Refold.drainBy putChunk
485477
486478-- XXX lpackArraysChunksOf should be written idiomatically
487−--
479+
488480-- | @writeChunksWith bufsize handle@ writes a stream of arrays
489481-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
490482-- We never split an array, if a single array is bigger than the specified size
@@ -515,7 +507,7 @@ writeChunksWithBufferOf = writeChunksWith
515507-- writes as well.
516508
517509-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
518−--
510+
519511-- | @writeWith reqSize handle@ writes the input stream to @handle@.
520512-- Bytes in the input stream are collected into a buffer until we have a chunk
521513-- of @reqSize@ and then written to the IO device.
@@ -548,13 +540,13 @@ writeMaybesWith n h =
548540 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
549541 in FL.many writeOnNothing (writeChunks h)
550542
551−-- | Like 'writeWith' but uses the experimental 'Refold' API.
543+-- | Like 'writeWith' but uses the experimental 'Refold' API.
552544--
553545-- /Internal/
554−{-# INLINE consumerWith #-}
555−consumerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
556−consumerWith n =
557− FL.refoldMany (FL.take n $ writeNUnsafe n) consumeChunks
546+{-# INLINE writerWith #-}
547+writerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
548+writerWith n =
549+ FL.refoldMany (FL.take n $ writeNUnsafe n) chunkWriter
558550
559551-- | Write a byte stream to a file handle. Accumulates the input in chunks of
560552-- up to 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize' before writing
@@ -570,15 +562,9 @@ write = writeWith defaultChunkSize
570562-- | Like 'write' but uses the experimental 'Refold' API.
571563--
572564-- /Internal/
573−{-# INLINE consumer #-}
574−consumer :: MonadIO m => Refold m Handle Word8 ()
575−consumer = consumerWith defaultChunkSize
576−
577−{-
578−{-# INLINE write #-}
579−write :: (MonadIO m, Unboxed a) => Handle -> Stream m a -> m ()
580−write = toHandleWith A.defaultChunkSize
581−-}
565+{-# INLINE writer #-}
566+writer :: MonadIO m => Refold m Handle Word8 ()
567+writer = writerWith defaultChunkSize
582568
583569-- XXX mmap a file into an array. This could be useful for in-place operations
584570-- on a file. For example, we can quicksort the contents of a file by mmapping