module Network.HaskellNet.BSStream
( BSStream(..)
, handleToStream
)
where
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS
import System.IO
data BSStream =
BSStream { bsGetLine :: IO ByteString
, bsGet :: Int -> IO ByteString
, bsPut :: ByteString -> IO ()
, bsFlush :: IO ()
, bsClose :: IO ()
, bsIsOpen :: IO Bool
, bsWaitForInput :: Int -> IO Bool
}
handleToStream :: Handle -> BSStream
handleToStream h =
BSStream { bsGetLine = BS.hGetLine h
, bsGet = BS.hGet h
, bsPut = \s -> BS.hPut h s >> hFlush h
, bsFlush = hFlush h
, bsClose = do
op <- hIsOpen h
if op then (hClose h) else return ()
, bsIsOpen = hIsOpen h
, bsWaitForInput = hWaitForInput h
}