@@ -20,7 +20,6 @@ module Streamly.Internal.FileSystem.Handle
22 , toBytesWithBufferOf
22 , toBytesWithBufferOf
25 -- -- * Array Read
24 -- -- * Array Read
26 -- , readArrayUpto
25 -- , readArrayUpto
@@ -30,7 +29,6 @@ module Streamly.Internal.FileSystem.Handle
31 , toChunksWithBufferOf
30 , toChunksWithBufferOf
32 , toChunks
31 , toChunks
35 -- ** Write to Handle
33 -- ** Write to Handle
36 -- Byte stream write (Folds)
34 -- Byte stream write (Folds)
@@ -43,8 +41,8 @@ module Streamly.Internal.FileSystem.Handle
43 , writeWithBufferOf
41 , writeWithBufferOf
45 -- Byte stream write (Streams)
43 -- Byte stream write (Streams)
46− , fromBytes
44+ , putBytes
47− , fromBytesWithBufferOf
45+ , putBytesWithBufferOf
49 -- -- * Array Write
47 -- -- * Array Write
50 , writeArray
48 , writeArray
@@ -52,12 +50,8 @@ module Streamly.Internal.FileSystem.Handle
52 , writeChunksWithBufferOf
50 , writeChunksWithBufferOf
54 -- -- * Array stream Write
52 -- -- * Array stream Write
55− , fromChunksWithBufferOf
53+ , putChunksWithBufferOf
57 , putChunks
54 , putChunks
62 -- -- * Random Access (Seek)
56 -- -- * Random Access (Seek)
63 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
57 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
@@ -106,10 +100,9 @@ import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
106import Foreign.Ptr (minusPtr, plusPtr)
100import Foreign.Ptr (minusPtr, plusPtr)
107import Foreign.Storable (Storable(..))
101import Foreign.Storable (Storable(..))
108import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
102import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
109−import System.IO (Handle, hGetBufSome, hPutBuf, stdin, stdout)
103+import System.IO (Handle, hGetBufSome, hPutBuf)
110import Prelude hiding (read)
104import Prelude hiding (read)
112−import Streamly.Prelude (MonadAsync)
113import Streamly.Data.Fold (Fold)
106import Streamly.Data.Fold (Fold)
114import Streamly.Internal.Data.Fold.Type (Fold2(..))
107import Streamly.Internal.Data.Fold.Type (Fold2(..))
115import Streamly.Internal.Data.Unfold.Type (Unfold(..))
108import Streamly.Internal.Data.Unfold.Type (Unfold(..))
@@ -124,7 +117,6 @@ import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
124import qualified Streamly.Data.Fold as FL
117import qualified Streamly.Data.Fold as FL
125import qualified Streamly.Internal.Data.Fold.Type as FL
118import qualified Streamly.Internal.Data.Fold.Type as FL
126import qualified Streamly.Internal.Data.Unfold as UF
119import qualified Streamly.Internal.Data.Unfold as UF
127−import qualified Streamly.Internal.Data.Array.Foreign as IA
128import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
120import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
129import qualified Streamly.Internal.Data.Stream.IsStream as S
121import qualified Streamly.Internal.Data.Stream.IsStream as S
130import qualified Streamly.Data.Array.Foreign as A
122import qualified Streamly.Data.Array.Foreign as A
@@ -231,28 +223,6 @@ readChunksWithBufferOf = Unfold step return
231toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
223toChunks :: (IsStream t, MonadIO m) => Handle -> t m (Array Word8)
232toChunks = toChunksWithBufferOf defaultChunkSize
224toChunks = toChunksWithBufferOf defaultChunkSize
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@.
238−-- > getChunks = toChunks stdin
242−{-# INLINE getChunks #-}
243−getChunks :: (IsStream t, MonadIO m) => t m (Array Word8)
244−getChunks = toChunks stdin
246−-- | Read a stream of bytes from standard input.
248−-- > getBytes = toBytes stdin
252−{-# INLINE getBytes #-}
253−getBytes :: (IsStream t, MonadIO m) => t m Word8
254−getBytes = toBytes stdin
256-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
226-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
257-- device are performed using a buffer of size
227-- device are performed using a buffer of size
258-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'. The
228-- 'Streamly.Internal.Data.Array.Foreign.Type.defaultChunkSize'. The
@@ -340,73 +310,31 @@ writeArray h Array{..} = withForeignPtr aStart $ \p -> hPutBuf h p aLen
340-- | Write a stream of arrays to a handle.
310-- | Write a stream of arrays to a handle.
342-- @since 0.7.0
312-- @since 0.7.0
343−{-# INLINE fromChunks #-}
344−fromChunks :: (MonadIO m, Storable a)
345− => Handle -> SerialT m (Array a) -> m ()
346−fromChunks h = S.mapM_ (liftIO . writeArray h)
348−-- | Write a stream of chunks to standard output.
352{-# INLINE putChunks #-}
313{-# INLINE putChunks #-}
353−putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
314+putChunks :: (MonadIO m, Storable a)
354−putChunks = fromChunks stdout
315+ => Handle -> SerialT m (Array a) -> m ()
355−
316+putChunks h = S.mapM_ (liftIO . writeArray h)
356−-- XXX use an unfold so that we can put any type of strings.
357−-- | Write a stream of strings to standard output using the supplied encoding.
358−-- Output is flushed to the device for each string.
362−{-# INLINE putStrings #-}
363−putStrings :: MonadAsync m
364− => (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
365−putStrings encode = putChunks . S.mapM (IA.fromStream . encode . S.fromList)
367−-- XXX use an unfold so that we can put lines from any object
368−-- | Write a stream of strings as separate lines to standard output using the
369−-- supplied encoding. Output is line buffered i.e. the output is written to the
370−-- device as soon as a newline is encountered.
374−{-# INLINE putLines #-}
375−putLines :: MonadAsync m
376− => (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
377−putLines encode = putChunks . S.mapM
378− (\xs -> IA.fromStream $ encode (S.fromList (xs ++ "\n")))
380−-- | Write a stream of bytes from standard output.
382−-- > putBytes = fromBytes stdout
386−{-# INLINE putBytes #-}
387−putBytes :: MonadIO m => SerialT m Word8 -> m ()
388−putBytes = fromBytes stdout
390−-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
318+-- | @putChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
391-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
319-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
392-- The chunk size is only a maximum and the actual writes could be smaller as
320-- The chunk size is only a maximum and the actual writes could be smaller as
393-- we do not split the arrays to fit exactly to the specified size.
321-- we do not split the arrays to fit exactly to the specified size.
395-- @since 0.7.0
323-- @since 0.7.0
396−{-# INLINE fromChunksWithBufferOf #-}
324+{-# INLINE putChunksWithBufferOf #-}
397−fromChunksWithBufferOf :: (MonadIO m, Storable a)
325+putChunksWithBufferOf :: (MonadIO m, Storable a)
398 => Int -> Handle -> SerialT m (Array a) -> m ()
326 => Int -> Handle -> SerialT m (Array a) -> m ()
399−fromChunksWithBufferOf n h xs = fromChunks h $ AS.compact n xs
327+putChunksWithBufferOf n h xs = putChunks h $ AS.compact n xs
401−-- | @fromBytesWithBufferOf bufsize handle stream@ writes @stream@ to @handle@
329+-- | @putBytesWithBufferOf bufsize handle stream@ writes @stream@ to @handle@
402-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
330-- in chunks of @bufsize@. A write is performed to the IO device as soon as we
403-- collect the required input size.
331-- collect the required input size.
405-- @since 0.7.0
333-- @since 0.7.0
406−{-# INLINE fromBytesWithBufferOf #-}
334+{-# INLINE putBytesWithBufferOf #-}
407−fromBytesWithBufferOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
335+putBytesWithBufferOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
408−fromBytesWithBufferOf n h m = fromChunks h $ S.arraysOf n m
336+putBytesWithBufferOf n h m = putChunks h $ S.arraysOf n m
409−-- fromBytesWithBufferOf n h m = fromChunks h $ AS.arraysOf n m
337+-- putBytesWithBufferOf n h m = putChunks h $ AS.arraysOf n m
411-- > write = 'writeWithBufferOf' A.defaultChunkSize
339-- > write = 'writeWithBufferOf' A.defaultChunkSize
@@ -417,9 +345,9 @@ fromBytesWithBufferOf n h m = fromChunks h $ S.arraysOf n m
417-- need some extra perf boost.
345-- need some extra perf boost.
419-- @since 0.7.0
347-- @since 0.7.0
420−{-# INLINE fromBytes #-}
348+{-# INLINE putBytes #-}
421−fromBytes :: MonadIO m => Handle -> SerialT m Word8 -> m ()
349+putBytes :: MonadIO m => Handle -> SerialT m Word8 -> m ()
422−fromBytes = fromBytesWithBufferOf defaultChunkSize
350+putBytes = putBytesWithBufferOf defaultChunkSize
424-- | Write a stream of arrays to a handle. Each array in the stream is written
352-- | Write a stream of arrays to a handle. Each array in the stream is written
425-- to the device as a separate IO request.
353-- to the device as a separate IO request.