on master help sign in
Implement readChunksFromToWith
commitf08d73afa1df976765f9796e843fba2e943f1450 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateFri 10 Sep 2021, 12:35 (5 years ago) parentf6b88dc
1 file changed+27
src/Streamly/Internal/FileSystem/File.hs+27
src/Streamly/Internal/FileSystem/File.hs+27
@@ -55,6 +55,7 @@ module Streamly.Internal.FileSystem.File
5555
5656 -- -- * Array Read
5757 , readChunksWithBufferOf
58+ , readChunksFromToWith
5859 , readChunks
5960
6061 , toChunksWithBufferOf
@@ -170,6 +171,19 @@ usingFile2 = UF.bracket before after
170171
171172 after (_, h) = liftIO $ hClose h
172173
174+{-# INLINABLE usingFile3 #-}
175+usingFile3 :: (MonadCatch m, MonadAsync m)
176+ => Unfold m (x, y, z, Handle) a -> Unfold m (x, y, z, FilePath) a
177+usingFile3 = UF.bracket before after
178+
179+ where
180+
181+ before (x, y, z, file) = do
182+ h <- liftIO $ openFile file ReadMode
183+ return (x, y, z, h)
184+
185+ after (_, _, _, h) = liftIO $ hClose h
186+
173187-------------------------------------------------------------------------------
174188-- Array IO (Input)
175189-------------------------------------------------------------------------------
@@ -241,6 +255,19 @@ readChunksWithBufferOf :: (MonadCatch m, MonadAsync m)
241255 => Unfold m (Int, FilePath) (Array Word8)
242256readChunksWithBufferOf = usingFile2 FH.readChunksWithBufferOf
243257
258+-- | Unfold the tuple @(from, to, bufsize, filepath)@ into a stream
259+-- of 'Word8' arrays.
260+-- Read requests to the IO device are performed using a buffer of size
261+-- @bufsize@ starting from absolute offset of @from@ till the absolute
262+-- position of @to@. The size of an array in the resulting stream is always
263+-- less than or equal to @bufsize@.
264+--
265+-- /Pre-release/
266+{-# INLINE readChunksFromToWith #-}
267+readChunksFromToWith :: (MonadCatch m, MonadAsync m) =>
268+ Unfold m (Int, Int, Int, FilePath) (Array Word8)
269+readChunksFromToWith = usingFile3 FH.readChunksFromToWith
270+
244271-- | Unfolds a 'FilePath' into a stream of 'Word8' arrays. Requests to the IO
245272-- device are performed using a buffer of size
246273-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'. The