@@ -1,6 +1,7 @@
1{-# OPTIONS_HADDOCK hide #-}
1{-# OPTIONS_HADDOCK hide #-}
2{-# LANGUAGE CPP #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE BangPatterns #-}
4+{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE MagicHash #-}
5{-# LANGUAGE MagicHash #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE UnboxedTuples #-}
7{-# LANGUAGE UnboxedTuples #-}
@@ -27,6 +28,7 @@ module Streamly.Internal.FileSystem.Handle
29 , toBytesWithBufferOf
30 , toBytesWithBufferOf
31 -- -- * Array Read
33 -- -- * Array Read
32 -- , readArrayUpto
34 -- , readArrayUpto
@@ -44,6 +46,7 @@ module Streamly.Internal.FileSystem.Handle
44 -- , writeUtf8
46 -- , writeUtf8
45 -- , writeUtf8ByLines
47 -- , writeUtf8ByLines
46 -- , writeByFrames
48 -- , writeByFrames
47 , writeWithBufferOf
50 , writeWithBufferOf
49 -- Byte stream write (Streams)
52 -- Byte stream write (Streams)
@@ -60,6 +63,7 @@ module Streamly.Internal.FileSystem.Handle
60 , fromChunks
63 , fromChunks
61 , putChunks
64 , putChunks
62 , putStrings
65 , putStrings
64 -- -- * Random Access (Seek)
68 -- -- * Random Access (Seek)
65 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
69 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
@@ -111,6 +115,7 @@ import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
111import System.IO (Handle, hGetBufSome, hPutBuf, stdin, stdout)
115import System.IO (Handle, hGetBufSome, hPutBuf, stdin, stdout)
112import Prelude hiding (read)
116import Prelude hiding (read)
118+import Streamly (MonadAsync)
114import Streamly.Data.Fold (Fold)
119import Streamly.Data.Fold (Fold)
115import Streamly.Internal.Data.Unfold.Types (Unfold(..))
120import Streamly.Internal.Data.Unfold.Types (Unfold(..))
116import Streamly.Internal.Memory.Array.Types
121import Streamly.Internal.Memory.Array.Types
@@ -124,6 +129,7 @@ import qualified Streamly.Data.Fold as FL
124import qualified Streamly.Internal.Data.Fold.Types as FL
129import qualified Streamly.Internal.Data.Fold.Types as FL
125import qualified Streamly.Internal.Data.Unicode.Stream as U
130import qualified Streamly.Internal.Data.Unicode.Stream as U
126import qualified Streamly.Internal.Data.Unfold as UF
131import qualified Streamly.Internal.Data.Unfold as UF
132+import qualified Streamly.Internal.Memory.Array as IA
127import qualified Streamly.Internal.Memory.ArrayStream as AS
133import qualified Streamly.Internal.Memory.ArrayStream as AS
128import qualified Streamly.Internal.Prelude as S
134import qualified Streamly.Internal.Prelude as S
129import qualified Streamly.Memory.Array as A
135import qualified Streamly.Memory.Array as A
@@ -245,6 +251,16 @@ toChunks = toChunksWithBufferOf defaultChunkSize
245getChunks :: (IsStream t, MonadIO m) => t m (Array Word8)
251getChunks :: (IsStream t, MonadIO m) => t m (Array Word8)
246getChunks = toChunks stdin
252getChunks = toChunks stdin
254+-- | Read a stream of bytes from standard input.
256+-- > getBytes = toBytes stdin
260+{-# INLINE getBytes #-}
261+getBytes :: (IsStream t, MonadIO m) => t m Word8
262+getBytes = toBytes stdin
248-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
264-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
249-- device are performed using a buffer of size
265-- device are performed using a buffer of size
250-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
266-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
@@ -344,16 +360,13 @@ fromChunks h m = S.mapM_ (liftIO . writeArray h) m
344putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
360putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
345putChunks = fromChunks stdout
361putChunks = fromChunks stdout
347−-- XXX this is currently buffered with 32K buffer. We need to use a timeout
348−-- based scheme instead.
350−-- | Write a stream of strings to standard output.
363+-- | Write a stream of strings to standard output using Latin1 encoding.
352-- /Internal/
365-- /Internal/
354{-# INLINE putStrings #-}
367{-# INLINE putStrings #-}
355−putStrings :: MonadIO m => SerialT m String -> m ()
368+putStrings :: MonadAsync m => SerialT m String -> m ()
356−putStrings = fromBytes stdout . U.encodeUtf8 . S.concatUnfold UF.fromList
369+putStrings = putChunks . S.mapM (IA.fromStream . U.encodeLatin1 . S.fromList)
358-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
371-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
359-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
372-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.