diff options
Diffstat (limited to 'ByteStringUtil.hs')
-rw-r--r-- | ByteStringUtil.hs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/ByteStringUtil.hs b/ByteStringUtil.hs new file mode 100644 index 0000000..9711e0b --- /dev/null +++ b/ByteStringUtil.hs | |||
@@ -0,0 +1,29 @@ | |||
1 | module ByteStringUtil where | ||
2 | |||
3 | import Data.ByteString.Lazy.Internal | ||
4 | import Data.ByteString.Lazy as L | ||
5 | import qualified Data.ByteString as S | ||
6 | import System.IO | ||
7 | import System.IO.Unsafe | ||
8 | |||
9 | oneMeg :: Int | ||
10 | oneMeg = 1048576 | ||
11 | |||
12 | hGetContentsN :: Int -> Handle -> IO ByteString | ||
13 | hGetContentsN kk h = lazyRead | ||
14 | where | ||
15 | k = kk - chunkOverhead | ||
16 | lazyRead = unsafeInterleaveIO loop | ||
17 | |||
18 | loop = do | ||
19 | c <- S.hGetSome h k -- only blocks if there is no data available | ||
20 | if S.null c | ||
21 | then hClose h >> return Empty | ||
22 | else do cs <- lazyRead | ||
23 | return (Chunk c cs) | ||
24 | |||
25 | readBigFile :: FilePath -> IO ByteString | ||
26 | readBigFile fname = do | ||
27 | h <- openFile fname ReadMode | ||
28 | hGetContentsN oneMeg h | ||
29 | |||