Skip to content

Commit

Permalink
Add hGetChunk
Browse files Browse the repository at this point in the history
  • Loading branch information
jhance committed Apr 8, 2012
1 parent c13c87a commit abf20a8
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Data/Text/IO.hs
Expand Up @@ -26,6 +26,7 @@ module Data.Text.IO
, appendFile
-- * Operations on handles
, hGetContents
, hGetChunk
, hGetLine
, hPutStr
, hPutStrLn
Expand Down Expand Up @@ -97,6 +98,22 @@ writeFile p = withFile p WriteMode . flip hPutStr
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.
Expand Down

0 comments on commit abf20a8

Please sign in to comment.