module ByteStringUtil where import Data.ByteString.Lazy.Internal import Data.ByteString.Lazy as L 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) readBigFile :: FilePath -> IO ByteString readBigFile fname = do h <- openFile fname ReadMode hGetContentsN oneMeg h