@@ -43,6 +43,7 @@ module Streamly.Internal.FileSystem.Handle
4343 -- ** Write to Handle
4444 -- Byte stream write (Folds)
4545 , write
46+ , write2
4647 -- , writeUtf8
4748 -- , writeUtf8ByLines
4849 -- , writeByFrames
@@ -117,6 +118,7 @@ import Prelude hiding (read)
117118
118119import Streamly (MonadAsync)
119120import Streamly.Data.Fold (Fold)
121+import Streamly.Internal.Data.Fold.Types (Fold2(..))
120122import Streamly.Internal.Data.Unfold.Types (Unfold(..))
121123import Streamly.Internal.Memory.Array.Types
122124 (Array(..), writeNUnsafe, defaultChunkSize, shrinkToFit,
@@ -410,6 +412,10 @@ fromBytes = fromBytesWithBufferOf defaultChunkSize
410412writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
411413writeChunks h = FL.drainBy (liftIO . writeArray h)
412414
415+{-# INLINE writeChunks2 #-}
416+writeChunks2 :: (MonadIO m, Storable a) => Fold2 m Handle (Array a) ()
417+writeChunks2 = Fold2 (\h arr -> liftIO $ writeArray h arr >> return h) return (\_ -> return ())
418+
413419-- | @writeChunksWithBufferOf bufsize handle@ writes a stream of arrays
414420-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
415421-- We never split an array, if a single array is bigger than the specified size
@@ -439,6 +445,10 @@ writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
439445writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
440446writeWithBufferOf n h = FL.lchunksOf n (writeNUnsafe n) (writeChunks h)
441447
448+{-# INLINE writeWithBufferOf2 #-}
449+writeWithBufferOf2 :: MonadIO m => Int -> Fold2 m Handle Word8 ()
450+writeWithBufferOf2 n = FL.lchunksOf2 n (writeNUnsafe n) writeChunks2
451+
442452-- > write = 'writeWithBufferOf' A.defaultChunkSize
443453--
444454-- | Write a byte stream to a file handle. Accumulates the input in chunks of
@@ -450,6 +460,10 @@ writeWithBufferOf n h = FL.lchunksOf n (writeNUnsafe n) (writeChunks h)
450460write :: MonadIO m => Handle -> Fold m Word8 ()
451461write = writeWithBufferOf defaultChunkSize
452462
463+{-# INLINE write2 #-}
464+write2 :: MonadIO m => Fold2 m Handle Word8 ()
465+write2 = writeWithBufferOf2 defaultChunkSize
466+
453467{-
454468{-# INLINE write #-}
455469write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()