on master help sign in
implement getChunks and putChunks
89ddf14Harendra Kumar
src/Streamly/Internal/FileSystem/File.hs+9−9
@@ -77,7 +77,7 @@ module Streamly.Internal.FileSystem.File
77
77
78 -- -- * Array Write
78 -- -- * Array Write
79 , writeArray
79 , writeArray
80− , writeChunks
80+ , fromChunks
81
81
82 -- ** Append To File
82 -- ** Append To File
83 , append
83 , append
@@ -242,19 +242,19 @@ readTailForever = undefined
242-- Writing
242-- Writing
243-------------------------------------------------------------------------------
243-------------------------------------------------------------------------------
244
244
245−{-# INLINE writeChunksMode #-}
245+{-# INLINE fromChunksMode #-}
246−writeChunksMode :: (MonadAsync m, MonadCatch m, Storable a)
246+fromChunksMode :: (MonadAsync m, MonadCatch m, Storable a)
247 => IOMode -> FilePath -> SerialT m (Array a) -> m ()
247 => IOMode -> FilePath -> SerialT m (Array a) -> m ()
248−writeChunksMode mode file xs = S.drain $
248+fromChunksMode mode file xs = S.drain $
249 withFile file mode (\h -> S.mapM (liftIO . FH.writeArray h) xs)
249 withFile file mode (\h -> S.mapM (liftIO . FH.writeArray h) xs)
250
250
251-- | Write a stream of arrays to a file. Overwrites the file if it exists.
251-- | Write a stream of arrays to a file. Overwrites the file if it exists.
252--
252--
253-- @since 0.7.0
253-- @since 0.7.0
254−{-# INLINE writeChunks #-}
254+{-# INLINE fromChunks #-}
255−writeChunks :: (MonadAsync m, MonadCatch m, Storable a)
255+fromChunks :: (MonadAsync m, MonadCatch m, Storable a)
256 => FilePath -> SerialT m (Array a) -> m ()
256 => FilePath -> SerialT m (Array a) -> m ()
257−writeChunks = writeChunksMode WriteMode
257+fromChunks = fromChunksMode WriteMode
258
258
259-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
259-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
260--
260--
@@ -272,7 +272,7 @@ writeChunks = writeChunksMode WriteMode
272{-# INLINE writeWithBufferOf #-}
272{-# INLINE writeWithBufferOf #-}
273writeWithBufferOf :: (MonadAsync m, MonadCatch m)
273writeWithBufferOf :: (MonadAsync m, MonadCatch m)
274 => Int -> FilePath -> SerialT m Word8 -> m ()
274 => Int -> FilePath -> SerialT m Word8 -> m ()
275−writeWithBufferOf n file xs = writeChunks file $ AS.arraysOf n xs
275+writeWithBufferOf n file xs = fromChunks file $ AS.arraysOf n xs
276
276
277-- > write = 'writeWithBufferOf' defaultChunkSize
277-- > write = 'writeWithBufferOf' defaultChunkSize
278--
278--
@@ -298,7 +298,7 @@ write = toHandleWith A.defaultChunkSize
298{-# INLINE appendChunks #-}
298{-# INLINE appendChunks #-}
299appendChunks :: (MonadAsync m, MonadCatch m, Storable a)
299appendChunks :: (MonadAsync m, MonadCatch m, Storable a)
300 => FilePath -> SerialT m (Array a) -> m ()
300 => FilePath -> SerialT m (Array a) -> m ()
301−appendChunks = writeChunksMode AppendMode
301+appendChunks = fromChunksMode AppendMode
302
302
303-- | Like 'append' but provides control over the write buffer. Output will
303-- | Like 'append' but provides control over the write buffer. Output will
304-- be written to the IO device as soon as we collect the specified number of
304-- be written to the IO device as soon as we collect the specified number of
src/Streamly/Internal/FileSystem/Handle.hs+24−2
@@ -36,6 +36,7 @@ module Streamly.Internal.FileSystem.Handle
36
36
37 , toChunksWithBufferOf
37 , toChunksWithBufferOf
38 , toChunks
38 , toChunks
39+ , getChunks
39
40
40 -- ** Write to Handle
41 -- ** Write to Handle
41 -- Byte stream write (Folds)
42 -- Byte stream write (Folds)
@@ -55,8 +56,9 @@ module Streamly.Internal.FileSystem.Handle
55 , writeChunksWithBufferOf
56 , writeChunksWithBufferOf
56
57
57 -- -- * Array stream Write
58 -- -- * Array stream Write
58− , fromChunks
59 , fromChunksWithBufferOf
59 , fromChunksWithBufferOf
60+ , fromChunks
61+ , putChunks
60
62
61 -- -- * Random Access (Seek)
63 -- -- * Random Access (Seek)
62 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
64 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
@@ -105,7 +107,7 @@ import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
105import Foreign.Ptr (minusPtr, plusPtr)
107import Foreign.Ptr (minusPtr, plusPtr)
106import Foreign.Storable (Storable(..))
108import Foreign.Storable (Storable(..))
107import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
109import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
108−import System.IO (Handle, hGetBufSome, hPutBuf)
110+import System.IO (Handle, hGetBufSome, hPutBuf, stdin, stdout)
109import Prelude hiding (read)
111import Prelude hiding (read)
110
112
111import Streamly.Data.Fold (Fold)
113import Streamly.Data.Fold (Fold)
@@ -229,6 +231,18 @@ readChunksWithBufferOf = Unfold step return
229toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
231toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
230toChunks = toChunksWithBufferOf defaultChunkSize
232toChunks = toChunksWithBufferOf defaultChunkSize
231
233
234+-- | Read a stream of chunks from standard input. The maximum size of a single
235+-- chunk is limited to @defaultChunkSize@. The actual size read may be less
236+-- than @defaultChunkSize@.
237+--
238+-- > getChunks = toChunks stdin
239+--
240+-- /Internal/
241+--
242+{-# INLINE getChunks #-}
243+getChunks :: (IsStream t, MonadIO m) => t m (Array Word8)
244+getChunks = toChunks stdin
245+
232-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
246-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
233-- device are performed using a buffer of size
247-- device are performed using a buffer of size
234-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
248-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
@@ -320,6 +334,14 @@ fromChunks :: (MonadIO m, Storable a)
320 => Handle -> SerialT m (Array a) -> m ()
334 => Handle -> SerialT m (Array a) -> m ()
321fromChunks h m = S.mapM_ (liftIO . writeArray h) m
335fromChunks h m = S.mapM_ (liftIO . writeArray h) m
322
336
337+-- | Write a stream of chunks to standard output.
338+--
339+-- /Internal/
340+--
341+{-# INLINE putChunks #-}
342+putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
343+putChunks = fromChunks stdout
344+
323-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
345-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
324-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
346-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
325-- The chunk size is only a maximum and the actual writes could be smaller as
347-- The chunk size is only a maximum and the actual writes could be smaller as