module Text.XML.HXT.Arrow.Binary
( readBinaryValue
, writeBinaryValue
)
where
import Control.Arrow ()
import Control.Arrow.ArrowExc
import Control.Arrow.ArrowList
import Control.Arrow.ArrowIO
import Data.Binary
import qualified Data.ByteString.Lazy as B
import System.IO ( openBinaryFile
, hClose
, IOMode(..)
)
import Text.XML.HXT.Arrow.XmlState.ErrorHandling
import Text.XML.HXT.Arrow.XmlState.TypeDefs
readBinaryValue :: (Binary a) => String -> IOStateArrow s b a
readBinaryValue file
= (uncurry $ decodeBinaryValue file)
$< getSysVar ( theStrictDeserialize
.&&&.
theBinaryDeCompression
)
decodeBinaryValue :: (Binary a) => String -> Bool -> DeCompressionFct -> IOStateArrow s b a
decodeBinaryValue file strict decompress
= arrIO0 dec
`catchA`
issueExc "readBinaryValue"
where
dec = ( if strict
then readItAll
else B.readFile file
) >>= return . decode . decompress
readItAll = do
h <- openBinaryFile file ReadMode
c <- B.hGetContents h
B.length c `seq`
do
hClose h
return c
writeBinaryValue :: (Binary a) => String -> IOStateArrow s a ()
writeBinaryValue file = flip encodeBinaryValue file $< getSysVar theBinaryCompression
encodeBinaryValue :: (Binary a) => CompressionFct -> String -> IOStateArrow s a ()
encodeBinaryValue compress file
= arrIO enc
`catchA`
issueExc "writeBinaryXmlTree"
where
enc = B.writeFile file . compress . encode