summaryrefslogtreecommitdiff
path: root/lib/ByteStringUtil.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-07-02 05:30:36 -0400
committerJoe Crayne <joe@jerkface.net>2019-07-02 05:30:36 -0400
commit4e87e15398728286efeacdb54d3feba6070ed1b1 (patch)
tree71988340ccee7a4bf2b0600a87bc1249f4ca4f6f /lib/ByteStringUtil.hs
parent75aeff50b20f0bf08cfcda371c6323b73fe73a23 (diff)
Read packet file with larger chunk size.
Diffstat (limited to 'lib/ByteStringUtil.hs')
-rw-r--r--lib/ByteStringUtil.hs23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/ByteStringUtil.hs b/lib/ByteStringUtil.hs
new file mode 100644
index 0000000..c6b509d
--- /dev/null
+++ b/lib/ByteStringUtil.hs
@@ -0,0 +1,23 @@
1module ByteStringUtil where
2
3import Data.ByteString.Lazy.Internal
4import Data.ByteString.Lazy
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)