uhexdump-0.1: FileBytes.hs
module FileBytes where
import Data.Word (Word8)
import Foreign.Marshal.Array (allocaArray, peekArray, pokeArray)
import System.IO (hFileSize, Handle, hGetBuf, hPutBuf, openBinaryFile, hClose,
IOMode(ReadMode, WriteMode))
-- ----------------------------------------------------------------------
-- supporting utf-8 stuff
-- ----------------------------------------------------------------------
readFileBytes :: FilePath -> IO [Word8]
readFileBytes f =
do h <- openBinaryFile f ReadMode
hsize <- fromIntegral `fmap` hFileSize h
hGetBytes h hsize
writeFileBytes :: FilePath -> [Word8] -> IO ()
writeFileBytes f ws =
do h <- openBinaryFile f WriteMode
hPutBytes h (length ws) ws
hClose h
hGetBytes :: Handle -> Int -> IO [Word8]
hGetBytes h c = allocaArray c $ \p ->
do c' <- hGetBuf h p c
peekArray c' p
hPutBytes :: Handle -> Int -> [Word8] -> IO ()
hPutBytes h c ws = allocaArray c $ \p ->
do pokeArray p ws
hPutBuf h p c