summaryrefslogtreecommitdiff
path: root/ByteStringUtil.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-07-31 11:24:27 -0400
committerJoe Crayne <joe@jerkface.net>2020-01-13 23:59:35 -0500
commit7cad9f6b02e2f71803047235622b9157ff988e75 (patch)
tree36c31805b96ebdc7dd915fc4ce50cfa33f3e5118 /ByteStringUtil.hs
parent85fb0dd752a8911e8fa14287ab9f8673fd6ffda4 (diff)
faster file loading.
Diffstat (limited to 'ByteStringUtil.hs')
-rw-r--r--ByteStringUtil.hs29
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 @@
1module ByteStringUtil where
2
3import Data.ByteString.Lazy.Internal
4import Data.ByteString.Lazy as L
5import qualified Data.ByteString as S
6import System.IO
7import System.IO.Unsafe
8
9oneMeg :: Int
10oneMeg = 1048576
11
12hGetContentsN :: Int -> Handle -> IO ByteString
13hGetContentsN 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
25readBigFile :: FilePath -> IO ByteString
26readBigFile fname = do
27 h <- openFile fname ReadMode
28 hGetContentsN oneMeg h
29