module ByteStringUtil where import Data.ByteString.Lazy.Internal import Data.ByteString.Lazy import qualified Data.ByteString as S import System.IO import System.IO.Unsafe oneMeg :: Int oneMeg = 1048576 hGetContentsN :: Int -> Handle -> IO ByteString hGetContentsN kk h = lazyRead where k = kk - chunkOverhead lazyRead = unsafeInterleaveIO loop loop = do c <- S.hGetSome h k -- only blocks if there is no data available if S.null c then hClose h >> return Empty else do cs <- lazyRead return (Chunk c cs)