Commits

Jared Hance  committed 6c2e9a5

Add hGetChunk

  • Participants
  • Parent commits a3aa2c4

Comments (0)

Files changed (1)

File Data/Text/IO.hs

     , appendFile
     -- * Operations on handles
     , hGetContents
+    , hGetChunk
     , hGetLine
     , hPutStr
     , hPutStrLn
 appendFile :: FilePath -> Text -> IO ()
 appendFile p = withFile p AppendMode . flip hPutStr
 
+-- | Read a single chunk of strict text from a 'Handle'.
+hGetChunk :: Handle -> IO Text
+hGetChunk h = wantReadableHandle "hGetChunk" h readSingleChunk
+ where
+  readSingleChunk hh@Handle__{..} = do
+    let catchError e
+          | isEOFError e = do
+              buf <- readIORef haCharBuffer
+              return $ if isEmptyBuffer buf
+                       then T.empty
+                       else T.singleton '\r'
+          | otherwise = throwIO (augmentIOError e "hGetChunk" h)
+    buf <- readIORef haCharBuffer
+    t <- readChunk hh buf `catch` catchError
+    return (hh, t)
+
 -- | Read the remaining contents of a 'Handle' as a string.  The
 -- 'Handle' is closed once the contents have been read, or if an
 -- exception is thrown.