@@ -86,7 +86,6 @@ module Streamly.FileSystem.Handle
86 -- -- * Array Write
86 -- -- * Array Write
87 , writeArray
87 , writeArray
88 , writeArrays
88 , writeArrays
89− -- , writeArraysPackedUpto
90 , writeArraysInChunksOf
89 , writeArraysInChunksOf
92 -- -- * Random Access (Seek)
91 -- -- * Random Access (Seek)
@@ -113,17 +112,17 @@ where
113import Control.Monad.IO.Class (MonadIO(..))
112import Control.Monad.IO.Class (MonadIO(..))
114import Data.Word (Word8)
113import Data.Word (Word8)
115import Foreign.ForeignPtr (withForeignPtr)
114import Foreign.ForeignPtr (withForeignPtr)
116−import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
115+import Foreign.Ptr (plusPtr)
117−import Foreign.Ptr (minusPtr, plusPtr)
118import Foreign.Storable (Storable(..))
116import Foreign.Storable (Storable(..))
119import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
117import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
120−import System.IO (Handle, hGetBufSome, hPutBuf)
118+import System.IO (Handle, hGetBufSome)
121import Prelude hiding (read)
119import Prelude hiding (read)
121+import Streamly.FileSystem.Handle.Internal (writeArray)
123import Streamly.Memory.Array.Types (Array(..))
122import Streamly.Memory.Array.Types (Array(..))
124−import Streamly.Streams.Serial (SerialT)
125import Streamly.Streams.StreamK.Type (IsStream, mkStream)
123import Streamly.Streams.StreamK.Type (IsStream, mkStream)
124+import Streamly.Memory.Array.Types
126−import Streamly.Memory.Array.Types (defaultChunkSize, shrinkToFit, lpackArraysChunksOf)
125+ (defaultChunkSize, shrinkToFit, lpackArraysChunksOf)
127import Streamly.Fold (Fold)
126import Streamly.Fold (Fold)
128-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
127-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
@@ -131,7 +130,6 @@ import qualified Streamly.Fold as FL
131import qualified Streamly.Fold.Types as FL
130import qualified Streamly.Fold.Types as FL
132import qualified Streamly.Memory.Array as A
131import qualified Streamly.Memory.Array as A
133import qualified Streamly.Memory.ArrayStream as AS
132import qualified Streamly.Memory.ArrayStream as AS
134−import qualified Streamly.Prelude as S
135import qualified Streamly.Streams.StreamD.Type as D
133import qualified Streamly.Streams.StreamD.Type as D
137-------------------------------------------------------------------------------
135-------------------------------------------------------------------------------
@@ -170,22 +168,6 @@ readArrayUpto size h = do
170 -- XXX shrink only if the diff is significant
168 -- XXX shrink only if the diff is significant
171 shrinkToFit v
169 shrinkToFit v
173−-------------------------------------------------------------------------------
175−-------------------------------------------------------------------------------
177−-- | Write an 'Array' to a file handle.
180−{-# INLINABLE writeArray #-}
181−writeArray :: Storable a => Handle -> Array a -> IO ()
182−writeArray _ arr | A.length arr == 0 = return ()
183−writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
186− let p = unsafeForeignPtrToPtr aStart
187− in aEnd `minusPtr` p
189-------------------------------------------------------------------------------
171-------------------------------------------------------------------------------
190-- Stream of Arrays IO
172-- Stream of Arrays IO
191-------------------------------------------------------------------------------
173-------------------------------------------------------------------------------
@@ -268,14 +250,6 @@ read = AS.flatten . readArrays
268-- Writing
250-- Writing
269-------------------------------------------------------------------------------
251-------------------------------------------------------------------------------
271−-- | Write a stream of arrays to a handle.
274−{-# INLINE writeArraysS #-}
275−writeArraysS :: (MonadIO m, Storable a)
276− => Handle -> SerialT m (Array a) -> m ()
277−writeArraysS h m = S.mapM_ (liftIO . writeArray h) m
279-- | Write a stream of arrays to a handle.
253-- | Write a stream of arrays to a handle.
281-- @since 0.7.0
255-- @since 0.7.0
@@ -283,17 +257,6 @@ writeArraysS h m = S.mapM_ (liftIO . writeArray h) m
283writeArrays :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
257writeArrays :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
284writeArrays h = FL.drainBy (liftIO . writeArray h)
258writeArrays h = FL.drainBy (liftIO . writeArray h)
286−-- | @writeArraysPackedUpto chunkSize handle stream@ writes a stream of arrays
287−-- to @handle@ after coalescing the adjacent arrays in chunks of @chunkSize@.
288−-- The chunk size is only a maximum and the actual writes could be smaller as
289−-- we do not split the arrays to fit exactly to the specified size.
292−{-# INLINE _writeArraysPackedUpto #-}
293−_writeArraysPackedUpto :: (MonadIO m, Storable a)
294− => Int -> Handle -> SerialT m (Array a) -> m ()
295−_writeArraysPackedUpto n h xs = writeArraysS h $ AS.compact n xs
297-- | @writeArraysInChunksOf chunkSize handle@ writes a stream of arrays
260-- | @writeArraysInChunksOf chunkSize handle@ writes a stream of arrays
298-- to @handle@ after coalescing the adjacent arrays in chunks of @chunkSize@.
261-- to @handle@ after coalescing the adjacent arrays in chunks of @chunkSize@.
299-- We never split an array, if a single array is bigger than the specified size
262-- We never split an array, if a single array is bigger than the specified size
@@ -314,15 +277,6 @@ writeArraysInChunksOf n h = lpackArraysChunksOf n (writeArrays h)
314-- do not want buffering to occur at GHC level as well. Same thing applies to
277-- do not want buffering to occur at GHC level as well. Same thing applies to
315-- writes as well.
278-- writes as well.
317−-- | @writeInChunksOf chunkSize handle stream@ writes @stream@ to @handle@ in
318−-- chunks of @chunkSize@. A write is performed to the IO device as soon as we
319−-- collect the required input size.
322−{-# INLINE writeInChunksOfS #-}
323−writeInChunksOfS :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
324−writeInChunksOfS n h m = writeArraysS h $ AS.arraysOf n m
326-- | @writeInChunksOf chunkSize handle@ writes the input stream to @handle@ in
280-- | @writeInChunksOf chunkSize handle@ writes the input stream to @handle@ in
327-- chunks of @chunkSize@. A write is performed to the IO device as soon as we
281-- chunks of @chunkSize@. A write is performed to the IO device as soon as we
328-- collect the required input size.
282-- collect the required input size.
@@ -332,16 +286,6 @@ writeInChunksOfS n h m = writeArraysS h $ AS.arraysOf n m
332writeInChunksOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
286writeInChunksOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
333writeInChunksOf n h = FL.lchunksOf n (A.writeN n) (writeArrays h)
287writeInChunksOf n h = FL.lchunksOf n (A.writeN n) (writeArrays h)
335−-- > write = 'writeInChunksOf' A.defaultChunkSize
337−-- | Write a byte stream to a file handle. Accumulates the input in chunks of
338−-- up to 'A.defaultChunkSize' before writing.
341−{-# INLINE _writeS #-}
342−_writeS :: MonadIO m => Handle -> SerialT m Word8 -> m ()
343−_writeS = writeInChunksOfS defaultChunkSize
345-- > write = 'writeInChunksOf' A.defaultChunkSize
289-- > write = 'writeInChunksOf' A.defaultChunkSize
347-- | Write a byte stream to a file handle. Accumulates the input in chunks of
291-- | Write a byte stream to a file handle. Accumulates the input in chunks of