@@ -44,19 +44,18 @@ module Streamly.Internal.FileSystem.File
4444 withFile
4545
4646 -- ** Read From File
47+ , readWithBufferOf
4748 , read
4849 -- , readShared
49− -- , readTailForever
50−
5150 -- , readUtf8
5251 -- , readLines
5352 -- , readFrames
54− -- , readChunks
5553
5654 , toBytes
5755
5856 -- -- * Array Read
59− -- , readArrayOf
57+ , readChunksWithBufferOf
58+ , readChunks
6059
6160 , toChunksWithBufferOf
6261 , toChunks
@@ -158,6 +157,19 @@ usingFile =
158157 UF.bracket (\file -> liftIO $ openFile file ReadMode)
159158 (liftIO . hClose)
160159
160+{-# INLINABLE usingFile2 #-}
161+usingFile2 :: (MonadCatch m, MonadIO m)
162+ => Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
163+usingFile2 = UF.bracket before after
164+
165+ where
166+
167+ before (x, file) = do
168+ h <- liftIO $ openFile file ReadMode
169+ return (x, h)
170+
171+ after (_, h) = liftIO $ hClose h
172+
161173-------------------------------------------------------------------------------
162174-- Array IO (Input)
163175-------------------------------------------------------------------------------
@@ -217,15 +229,36 @@ toChunks = toChunksWithBufferOf defaultChunkSize
217229-- read requests at the same time. For serial case we can use async IO. We can
218230-- also control the read throughput in mbps or IOPS.
219231
220−{-
232+-- | Unfold the tuple @(bufsize, filepath)@ into a stream of 'Word8' arrays.
233+-- Read requests to the IO device are performed using a buffer of size
234+-- @bufsize@. The size of an array in the resulting stream is always less than
235+-- or equal to @bufsize@.
236+--
237+-- /Internal/
238+--
239+{-# INLINE readChunksWithBufferOf #-}
240+readChunksWithBufferOf :: (MonadCatch m, MonadIO m)
241+ => Unfold m (Int, FilePath) (Array Word8)
242+readChunksWithBufferOf = usingFile2 FH.readChunksWithBufferOf
243+
244+-- | Unfolds a 'FilePath' into a stream of 'Word8' arrays. Requests to the IO
245+-- device are performed using a buffer of size
246+-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
247+-- size of arrays in the resulting stream are therefore less than or equal to
248+-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'.
249+--
250+-- /Internal/
251+{-# INLINE readChunks #-}
252+readChunks :: (MonadCatch m, MonadIO m) => Unfold m FilePath (Array Word8)
253+readChunks = usingFile FH.readChunks
254+
221255-- | Unfolds the tuple @(bufsize, filepath)@ into a byte stream, read requests
222256-- to the IO device are performed using buffers of @bufsize@.
223257--
224−-- @since 0.7.0
258+-- /Internal/
225259{-# INLINE readWithBufferOf #-}
226−readWithBufferOf :: MonadIO m => Unfold m (Int, FilePath) Word8
227−readWithBufferOf = UF.concat (usingFilexxx FH.readChunksWithBufferOf) A.read
228−-}
260+readWithBufferOf :: (MonadCatch m, MonadIO m) => Unfold m (Int, FilePath) Word8
261+readWithBufferOf = usingFile2 FH.readWithBufferOf
229262
230263-- | Unfolds a file path into a byte stream. IO requests to the device are
231264-- performed in sizes of
@@ -236,20 +269,6 @@ readWithBufferOf = UF.concat (usingFilexxx FH.readChunksWithBufferOf) A.read
236269read :: (MonadCatch m, MonadIO m) => Unfold m FilePath Word8
237270read = UF.concat (usingFile FH.readChunks) A.read
238271
239−{-
240−-- | @readInChunksOf chunkSize handle@ reads a byte stream from a file
241−-- handle, reads are performed in chunks of up to @chunkSize@. The stream ends
242−-- as soon as EOF is encountered.
243−--
244−{-# INLINE readInChunksOf #-}
245−readInChunksOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
246−readInChunksOf chunkSize h = A.flattenArrays $ toChunksWithBufferOf chunkSize h
247−-}
248−
249−-- TODO
250−-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
251−--
252−-- > read = 'readByChunks' defaultChunkSize
253272-- | Generate a stream of bytes from a file specified by path. The stream ends
254273-- when EOF is encountered. File is locked using multiple reader and single
255274-- writer locking mode.
@@ -269,15 +288,6 @@ toBytes file = AS.concat $ withFile file ReadMode FH.toChunks
269288{-# INLINE readShared #-}
270289readShared :: (IsStream t, MonadIO m) => Handle -> t m Word8
271290readShared = undefined
272−
273−-- | Read a stream from a given file path. When end of file (EOF) is reached
274−-- this API waits for more data to be written to the file and keeps reading it
275−-- as it is written.
276−--
277−-- @since 0.7.0
278−{-# INLINE readTailForever #-}
279−readTailForever :: (IsStream t, MonadIO m) => Handle -> t m Word8
280−readTailForever = undefined
281291-}
282292
283293-------------------------------------------------------------------------------