on master help sign in
implement getBytes and writeStrings
commit8504b9363b826701405f5a71da22b8bd99b3341e authorHarendra Kumar <harendra.kumar@gmail.com> dateFri 15 Nov 2019, 00:31 (7 years ago) parent3315e1a
2 files changed+31−6
src/Streamly/Internal/FileSystem/Handle.hs+19−6 src/Streamly/Internal/Network/Socket.hs+12
src/Streamly/Internal/FileSystem/Handle.hs+19−6
@@ -1,6 +1,7 @@
11{-# OPTIONS_HADDOCK hide #-}
22{-# LANGUAGE CPP #-}
33{-# LANGUAGE BangPatterns #-}
4+{-# LANGUAGE FlexibleContexts #-}
45{-# LANGUAGE MagicHash #-}
56{-# LANGUAGE RecordWildCards #-}
67{-# LANGUAGE UnboxedTuples #-}
@@ -27,6 +28,7 @@ module Streamly.Internal.FileSystem.Handle
2728
2829 , toBytes
2930 , toBytesWithBufferOf
31+ , getBytes
3032
3133 -- -- * Array Read
3234 -- , readArrayUpto
@@ -44,6 +46,7 @@ module Streamly.Internal.FileSystem.Handle
4446 -- , writeUtf8
4547 -- , writeUtf8ByLines
4648 -- , writeByFrames
49+ -- , writeLines
4750 , writeWithBufferOf
4851
4952 -- Byte stream write (Streams)
@@ -60,6 +63,7 @@ module Streamly.Internal.FileSystem.Handle
6063 , fromChunks
6164 , putChunks
6265 , putStrings
66+ -- , putLines
6367
6468 -- -- * Random Access (Seek)
6569 -- -- | Unlike the streaming APIs listed above, these APIs apply to devices or
@@ -111,6 +115,7 @@ import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
111115import System.IO (Handle, hGetBufSome, hPutBuf, stdin, stdout)
112116import Prelude hiding (read)
113117
118+import Streamly (MonadAsync)
114119import Streamly.Data.Fold (Fold)
115120import Streamly.Internal.Data.Unfold.Types (Unfold(..))
116121import Streamly.Internal.Memory.Array.Types
@@ -124,6 +129,7 @@ import qualified Streamly.Data.Fold as FL
124129import qualified Streamly.Internal.Data.Fold.Types as FL
125130import qualified Streamly.Internal.Data.Unicode.Stream as U
126131import qualified Streamly.Internal.Data.Unfold as UF
132+import qualified Streamly.Internal.Memory.Array as IA
127133import qualified Streamly.Internal.Memory.ArrayStream as AS
128134import qualified Streamly.Internal.Prelude as S
129135import qualified Streamly.Memory.Array as A
@@ -245,6 +251,16 @@ toChunks = toChunksWithBufferOf defaultChunkSize
245251getChunks :: (IsStream t, MonadIO m) => t m (Array Word8)
246252getChunks = toChunks stdin
247253
254+-- | Read a stream of bytes from standard input.
255+--
256+-- > getBytes = toBytes stdin
257+--
258+-- /Internal/
259+--
260+{-# INLINE getBytes #-}
261+getBytes :: (IsStream t, MonadIO m) => t m Word8
262+getBytes = toBytes stdin
263+
248264-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
249265-- device are performed using a buffer of size
250266-- 'Streamly.Internal.Memory.Array.Types.defaultChunkSize'. The
@@ -344,16 +360,13 @@ fromChunks h m = S.mapM_ (liftIO . writeArray h) m
344360putChunks :: (MonadIO m, Storable a) => SerialT m (Array a) -> m ()
345361putChunks = fromChunks stdout
346362
347−-- XXX this is currently buffered with 32K buffer. We need to use a timeout
348−-- based scheme instead.
349−--
350−-- | Write a stream of strings to standard output.
363+-- | Write a stream of strings to standard output using Latin1 encoding.
351364--
352365-- /Internal/
353366--
354367{-# INLINE putStrings #-}
355−putStrings :: MonadIO m => SerialT m String -> m ()
356−putStrings = fromBytes stdout . U.encodeUtf8 . S.concatUnfold UF.fromList
368+putStrings :: MonadAsync m => SerialT m String -> m ()
369+putStrings = putChunks . S.mapM (IA.fromStream . U.encodeLatin1 . S.fromList)
357370
358371-- | @fromChunksWithBufferOf bufsize handle stream@ writes a stream of arrays
359372-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
src/Streamly/Internal/Network/Socket.hs+12
@@ -62,6 +62,7 @@ module Streamly.Internal.Network.Socket
6262 -- -- * Array Write
6363 , writeArray
6464 , writeChunks
65+ , writeStrings
6566
6667 -- reading/writing datagrams
6768 )
@@ -100,8 +101,10 @@ import Streamly.Data.Fold (Fold)
100101-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
101102
102103import qualified Streamly.Data.Fold as FL
104+import qualified Streamly.Data.Unicode.Stream as U
103105import qualified Streamly.Internal.Data.Fold.Types as FL
104106import qualified Streamly.Internal.Data.Unfold as UF
107+import qualified Streamly.Internal.Memory.Array as IA
105108import qualified Streamly.Memory.Array as A
106109import qualified Streamly.Internal.Memory.ArrayStream as AS
107110import qualified Streamly.Internal.Memory.Array.Types as A
@@ -415,6 +418,15 @@ fromChunks h m = S.mapM_ (liftIO . writeArray h) m
415418writeChunks :: (MonadIO m, Storable a) => Socket -> Fold m (Array a) ()
416419writeChunks h = FL.drainBy (liftIO . writeArray h)
417420
421+-- | Write a stream of strings to a socket in Latin1 encoding.
422+--
423+-- /Internal/
424+--
425+{-# INLINE writeStrings #-}
426+writeStrings :: MonadIO m => Socket -> Fold m String ()
427+writeStrings h =
428+ FL.lmapM (IA.fromStream . U.encodeLatin1 . S.fromList) (writeChunks h)
429+
418430-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
419431--
420432-- XXX test this