#include "inline.hs"
module Streamly.Internal.FileSystem.File
(
withFile
, read
, readChunksWith
, readChunks
, readerWith
, reader
, chunkReaderWith
, chunkReaderFromToWith
, chunkReader
, putChunk
, write
, writeWith
, writeChunks
, fromBytes
, fromBytesWith
, fromChunks
, writeAppend
, writeAppendWith
, writeAppendArray
, writeAppendChunks
, readWithBufferOf
, readChunksWithBufferOf
, readChunksFromToWith
, toBytes
, toChunks
, toChunksWithBufferOf
, writeWithBufferOf
, fromBytesWithBufferOf
)
where
import Control.Monad.Catch (MonadCatch)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Word (Word8)
import System.IO (Handle, openFile, IOMode(..), hClose)
import Prelude hiding (read)
import qualified Control.Monad.Catch as MC
import qualified System.IO as SIO
import Streamly.Data.Fold (groupsOf, drain)
import Streamly.Internal.Data.Array.Type (Array(..))
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Data.Stream (Stream)
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import Streamly.Internal.System.IO (defaultChunkSize)
import qualified Streamly.Internal.Data.Array as A
import qualified Streamly.Data.Stream as S
import qualified Streamly.Data.Unfold as UF
import qualified Streamly.Internal.Data.Array.Type as IA (pinnedChunksOf)
import qualified Streamly.Internal.Data.Unfold as UF (bracketIO)
import qualified Streamly.Internal.Data.Fold.Type as FL
(Step(..), snoc, reduce)
import qualified Streamly.Internal.FileSystem.Handle as FH
{-# INLINE withFile #-}
withFile :: (MonadIO m, MonadCatch m)
=> FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
withFile :: forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
withFile FilePath
file IOMode
mode = IO Handle
-> (Handle -> IO ()) -> (Handle -> Stream m a) -> Stream m a
forall (m :: * -> *) b c a.
(MonadIO m, MonadCatch m) =>
IO b -> (b -> IO c) -> (b -> Stream m a) -> Stream m a
S.bracketIO (FilePath -> IOMode -> IO Handle
openFile FilePath
file IOMode
mode) Handle -> IO ()
hClose
{-# INLINE usingFile #-}
usingFile :: (MonadIO m, MonadCatch m)
=> Unfold m Handle a -> Unfold m FilePath a
usingFile :: forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
Unfold m Handle a -> Unfold m FilePath a
usingFile = (FilePath -> IO Handle)
-> (Handle -> IO ()) -> Unfold m Handle a -> Unfold m FilePath a
forall (m :: * -> *) a c d b.
(MonadIO m, MonadCatch m) =>
(a -> IO c) -> (c -> IO d) -> Unfold m c b -> Unfold m a b
UF.bracketIO (FilePath -> IOMode -> IO Handle
`openFile` IOMode
ReadMode) Handle -> IO ()
hClose
{-# INLINE usingFile2 #-}
usingFile2 :: (MonadIO m, MonadCatch m)
=> Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
usingFile2 :: forall (m :: * -> *) x a.
(MonadIO m, MonadCatch m) =>
Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
usingFile2 = ((x, FilePath) -> IO (x, Handle))
-> ((x, Handle) -> IO ())
-> Unfold m (x, Handle) a
-> Unfold m (x, FilePath) a
forall (m :: * -> *) a c d b.
(MonadIO m, MonadCatch m) =>
(a -> IO c) -> (c -> IO d) -> Unfold m c b -> Unfold m a b
UF.bracketIO (x, FilePath) -> IO (x, Handle)
forall {a}. (a, FilePath) -> IO (a, Handle)
before (x, Handle) -> IO ()
forall {a}. (a, Handle) -> IO ()
after
where
before :: (a, FilePath) -> IO (a, Handle)
before (a
x, FilePath
file) = do
Handle
h <- FilePath -> IOMode -> IO Handle
openFile FilePath
file IOMode
ReadMode
(a, Handle) -> IO (a, Handle)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x, Handle
h)
after :: (a, Handle) -> IO ()
after (a
_, Handle
h) = Handle -> IO ()
hClose Handle
h
{-# INLINE usingFile3 #-}
usingFile3 :: (MonadIO m, MonadCatch m)
=> Unfold m (x, y, z, Handle) a -> Unfold m (x, y, z, FilePath) a
usingFile3 :: forall (m :: * -> *) x y z a.
(MonadIO m, MonadCatch m) =>
Unfold m (x, y, z, Handle) a -> Unfold m (x, y, z, FilePath) a
usingFile3 = ((x, y, z, FilePath) -> IO (x, y, z, Handle))
-> ((x, y, z, Handle) -> IO ())
-> Unfold m (x, y, z, Handle) a
-> Unfold m (x, y, z, FilePath) a
forall (m :: * -> *) a c d b.
(MonadIO m, MonadCatch m) =>
(a -> IO c) -> (c -> IO d) -> Unfold m c b -> Unfold m a b
UF.bracketIO (x, y, z, FilePath) -> IO (x, y, z, Handle)
forall {a} {b} {c}. (a, b, c, FilePath) -> IO (a, b, c, Handle)
before (x, y, z, Handle) -> IO ()
forall {a} {b} {c}. (a, b, c, Handle) -> IO ()
after
where
before :: (a, b, c, FilePath) -> IO (a, b, c, Handle)
before (a
x, b
y, c
z, FilePath
file) = do
Handle
h <- FilePath -> IOMode -> IO Handle
openFile FilePath
file IOMode
ReadMode
(a, b, c, Handle) -> IO (a, b, c, Handle)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
x, b
y, c
z, Handle
h)
after :: (a, b, c, Handle) -> IO ()
after (a
_, b
_, c
_, Handle
h) = Handle -> IO ()
hClose Handle
h
{-# INLINABLE putChunk #-}
putChunk :: FilePath -> Array a -> IO ()
putChunk :: forall a. FilePath -> Array a -> IO ()
putChunk FilePath
file Array a
arr = FilePath -> IOMode -> (Handle -> IO ()) -> IO ()
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
SIO.withFile FilePath
file IOMode
WriteMode (Handle -> Array a -> IO ()
forall (m :: * -> *) a. MonadIO m => Handle -> Array a -> m ()
`FH.putChunk` Array a
arr)
{-# INLINABLE writeAppendArray #-}
writeAppendArray :: FilePath -> Array a -> IO ()
writeAppendArray :: forall a. FilePath -> Array a -> IO ()
writeAppendArray FilePath
file Array a
arr = FilePath -> IOMode -> (Handle -> IO ()) -> IO ()
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
SIO.withFile FilePath
file IOMode
AppendMode (Handle -> Array a -> IO ()
forall (m :: * -> *) a. MonadIO m => Handle -> Array a -> m ()
`FH.putChunk` Array a
arr)
{-# INLINE readChunksWith #-}
readChunksWith :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m (Array Word8)
readChunksWith :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m (Array Word8)
readChunksWith Int
size FilePath
file =
FilePath
-> IOMode
-> (Handle -> Stream m (Array Word8))
-> Stream m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
withFile FilePath
file IOMode
ReadMode (Int -> Handle -> Stream m (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Int -> Handle -> Stream m (Array Word8)
FH.readChunksWith Int
size)
{-# DEPRECATED toChunksWithBufferOf "Please use 'readChunksWith' instead" #-}
{-# INLINE toChunksWithBufferOf #-}
toChunksWithBufferOf :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m (Array Word8)
toChunksWithBufferOf :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m (Array Word8)
toChunksWithBufferOf = Int -> FilePath -> Stream m (Array Word8)
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m (Array Word8)
readChunksWith
{-# INLINE readChunks #-}
readChunks :: (MonadIO m, MonadCatch m)
=> FilePath -> Stream m (Array Word8)
readChunks :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m (Array Word8)
readChunks = Int -> FilePath -> Stream m (Array Word8)
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m (Array Word8)
readChunksWith Int
defaultChunkSize
{-# DEPRECATED toChunks "Please use 'readChunks' instead" #-}
{-# INLINE toChunks #-}
toChunks :: (MonadIO m, MonadCatch m) => FilePath -> Stream m (Array Word8)
toChunks :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m (Array Word8)
toChunks = FilePath -> Stream m (Array Word8)
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m (Array Word8)
readChunks
{-# INLINE chunkReaderWith #-}
chunkReaderWith :: (MonadIO m, MonadCatch m)
=> Unfold m (Int, FilePath) (Array Word8)
chunkReaderWith :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, FilePath) (Array Word8)
chunkReaderWith = Unfold m (Int, Handle) (Array Word8)
-> Unfold m (Int, FilePath) (Array Word8)
forall (m :: * -> *) x a.
(MonadIO m, MonadCatch m) =>
Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
usingFile2 Unfold m (Int, Handle) (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Handle) (Array Word8)
FH.chunkReaderWith
{-# DEPRECATED readChunksWithBufferOf
"Please use 'chunkReaderWith' instead" #-}
{-# INLINE readChunksWithBufferOf #-}
readChunksWithBufferOf :: (MonadIO m, MonadCatch m)
=> Unfold m (Int, FilePath) (Array Word8)
readChunksWithBufferOf :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, FilePath) (Array Word8)
readChunksWithBufferOf = Unfold m (Int, FilePath) (Array Word8)
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, FilePath) (Array Word8)
chunkReaderWith
{-# INLINE chunkReaderFromToWith #-}
chunkReaderFromToWith :: (MonadIO m, MonadCatch m) =>
Unfold m (Int, Int, Int, FilePath) (Array Word8)
chunkReaderFromToWith :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, Int, Int, FilePath) (Array Word8)
chunkReaderFromToWith = Unfold m (Int, Int, Int, Handle) (Array Word8)
-> Unfold m (Int, Int, Int, FilePath) (Array Word8)
forall (m :: * -> *) x y z a.
(MonadIO m, MonadCatch m) =>
Unfold m (x, y, z, Handle) a -> Unfold m (x, y, z, FilePath) a
usingFile3 Unfold m (Int, Int, Int, Handle) (Array Word8)
forall (m :: * -> *).
MonadIO m =>
Unfold m (Int, Int, Int, Handle) (Array Word8)
FH.chunkReaderFromToWith
{-# DEPRECATED readChunksFromToWith
"Please use 'chunkReaderFromToWith' instead" #-}
{-# INLINE readChunksFromToWith #-}
readChunksFromToWith :: (MonadIO m, MonadCatch m) =>
Unfold m (Int, Int, Int, FilePath) (Array Word8)
readChunksFromToWith :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, Int, Int, FilePath) (Array Word8)
readChunksFromToWith = Unfold m (Int, Int, Int, FilePath) (Array Word8)
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, Int, Int, FilePath) (Array Word8)
chunkReaderFromToWith
{-# INLINE chunkReader #-}
chunkReader :: (MonadIO m, MonadCatch m) => Unfold m FilePath (Array Word8)
chunkReader :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m FilePath (Array Word8)
chunkReader = Unfold m Handle (Array Word8) -> Unfold m FilePath (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
Unfold m Handle a -> Unfold m FilePath a
usingFile Unfold m Handle (Array Word8)
forall (m :: * -> *). MonadIO m => Unfold m Handle (Array Word8)
FH.chunkReader
{-# INLINE readerWith #-}
readerWith :: (MonadIO m, MonadCatch m) => Unfold m (Int, FilePath) Word8
readerWith :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, FilePath) Word8
readerWith = Unfold m (Int, Handle) Word8 -> Unfold m (Int, FilePath) Word8
forall (m :: * -> *) x a.
(MonadIO m, MonadCatch m) =>
Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
usingFile2 Unfold m (Int, Handle) Word8
forall (m :: * -> *). MonadIO m => Unfold m (Int, Handle) Word8
FH.readerWith
{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead" #-}
{-# INLINE readWithBufferOf #-}
readWithBufferOf :: (MonadIO m, MonadCatch m) =>
Unfold m (Int, FilePath) Word8
readWithBufferOf :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, FilePath) Word8
readWithBufferOf = Unfold m (Int, FilePath) Word8
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m (Int, FilePath) Word8
readerWith
{-# INLINE reader #-}
reader :: (MonadIO m, MonadCatch m) => Unfold m FilePath Word8
reader :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Unfold m FilePath Word8
reader = Unfold m (Array Word8) Word8
-> Unfold m FilePath (Array Word8) -> Unfold m FilePath Word8
forall (m :: * -> *) b c a.
Monad m =>
Unfold m b c -> Unfold m a b -> Unfold m a c
UF.many Unfold m (Array Word8) Word8
forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.reader (Unfold m Handle (Array Word8) -> Unfold m FilePath (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
Unfold m Handle a -> Unfold m FilePath a
usingFile Unfold m Handle (Array Word8)
forall (m :: * -> *). MonadIO m => Unfold m Handle (Array Word8)
FH.chunkReader)
{-# INLINE read #-}
read :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8
read :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m Word8
read FilePath
file = Stream m (Array Word8) -> Stream m Word8
forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Stream m (Array a) -> Stream m a
A.concat (Stream m (Array Word8) -> Stream m Word8)
-> Stream m (Array Word8) -> Stream m Word8
forall a b. (a -> b) -> a -> b
$ FilePath
-> IOMode
-> (Handle -> Stream m (Array Word8))
-> Stream m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
withFile FilePath
file IOMode
ReadMode Handle -> Stream m (Array Word8)
forall (m :: * -> *). MonadIO m => Handle -> Stream m (Array Word8)
FH.readChunks
{-# DEPRECATED toBytes "Please use 'read' instead" #-}
{-# INLINE toBytes #-}
toBytes :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8
toBytes :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m Word8
toBytes = FilePath -> Stream m Word8
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m Word8
read
{-# INLINE fromChunksMode #-}
fromChunksMode :: (MonadIO m, MonadCatch m)
=> IOMode -> FilePath -> Stream m (Array a) -> m ()
fromChunksMode :: forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
IOMode -> FilePath -> Stream m (Array a) -> m ()
fromChunksMode IOMode
mode FilePath
file Stream m (Array a)
xs = Fold m () () -> Stream m () -> m ()
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m b
S.fold Fold m () ()
forall (m :: * -> *) a. Monad m => Fold m a ()
drain (Stream m () -> m ()) -> Stream m () -> m ()
forall a b. (a -> b) -> a -> b
$
FilePath -> IOMode -> (Handle -> Stream m ()) -> Stream m ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> IOMode -> (Handle -> Stream m a) -> Stream m a
withFile FilePath
file IOMode
mode (\Handle
h -> (Array a -> m ()) -> Stream m (Array a) -> Stream m ()
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
S.mapM (Handle -> Array a -> m ()
forall (m :: * -> *) a. MonadIO m => Handle -> Array a -> m ()
FH.putChunk Handle
h) Stream m (Array a)
xs)
{-# INLINE fromChunks #-}
fromChunks :: (MonadIO m, MonadCatch m)
=> FilePath -> Stream m (Array a) -> m ()
fromChunks :: forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m (Array a) -> m ()
fromChunks = IOMode -> FilePath -> Stream m (Array a) -> m ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
IOMode -> FilePath -> Stream m (Array a) -> m ()
fromChunksMode IOMode
WriteMode
{-# INLINE fromBytesWith #-}
fromBytesWith :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWith :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWith Int
n FilePath
file Stream m Word8
xs = FilePath -> Stream m (Array Word8) -> m ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m (Array a) -> m ()
fromChunks FilePath
file (Stream m (Array Word8) -> m ()) -> Stream m (Array Word8) -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Stream m Word8 -> Stream m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (Array a)
IA.pinnedChunksOf Int
n Stream m Word8
xs
{-# DEPRECATED fromBytesWithBufferOf "Please use 'fromBytesWith' instead" #-}
{-# INLINE fromBytesWithBufferOf #-}
fromBytesWithBufferOf :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWithBufferOf :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWithBufferOf = Int -> FilePath -> Stream m Word8 -> m ()
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWith
{-# INLINE fromBytes #-}
fromBytes :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8 -> m ()
fromBytes :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m Word8 -> m ()
fromBytes = Int -> FilePath -> Stream m Word8 -> m ()
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWith Int
defaultChunkSize
{-# INLINE writeChunks #-}
writeChunks :: (MonadIO m, MonadCatch m)
=> FilePath -> Fold m (Array a) ()
writeChunks :: forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> Fold m (Array a) ()
writeChunks FilePath
path = ((Fold m (Array a) (), Handle)
-> Array a -> m (Step (Fold m (Array a) (), Handle) ()))
-> m (Step (Fold m (Array a) (), Handle) ())
-> ((Fold m (Array a) (), Handle) -> m ())
-> ((Fold m (Array a) (), Handle) -> m ())
-> Fold m (Array a) ()
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold (Fold m (Array a) (), Handle)
-> Array a -> m (Step (Fold m (Array a) (), Handle) ())
forall {m :: * -> *} {a} {b} {b}.
(MonadCatch m, MonadIO m) =>
(Fold m a b, Handle) -> a -> m (Step (Fold m a b, Handle) b)
step m (Step (Fold m (Array a) (), Handle) ())
forall {a} {b}. m (Step (Fold m (Array a) (), Handle) b)
initial (Fold m (Array a) (), Handle) -> m ()
forall {m :: * -> *} {p}. Monad m => p -> m ()
extract (Fold m (Array a) (), Handle) -> m ()
forall {m :: * -> *} {a}.
MonadIO m =>
(Fold m a (), Handle) -> m ()
final
where
initial :: m (Step (Fold m (Array a) (), Handle) b)
initial = do
Handle
h <- IO Handle -> m Handle
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IOMode -> IO Handle
openFile FilePath
path IOMode
WriteMode)
Fold m (Array a) ()
fld <- Fold m (Array a) () -> m (Fold m (Array a) ())
forall (m :: * -> *) a b. Monad m => Fold m a b -> m (Fold m a b)
FL.reduce (Handle -> Fold m (Array a) ()
forall (m :: * -> *) a. MonadIO m => Handle -> Fold m (Array a) ()
FH.writeChunks Handle
h)
m (Fold m (Array a) ()) -> m () -> m (Fold m (Array a) ())
forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`MC.onException` IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> IO ()
hClose Handle
h)
Step (Fold m (Array a) (), Handle) b
-> m (Step (Fold m (Array a) (), Handle) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Fold m (Array a) (), Handle) b
-> m (Step (Fold m (Array a) (), Handle) b))
-> Step (Fold m (Array a) (), Handle) b
-> m (Step (Fold m (Array a) (), Handle) b)
forall a b. (a -> b) -> a -> b
$ (Fold m (Array a) (), Handle)
-> Step (Fold m (Array a) (), Handle) b
forall s b. s -> Step s b
FL.Partial (Fold m (Array a) ()
fld, Handle
h)
step :: (Fold m a b, Handle) -> a -> m (Step (Fold m a b, Handle) b)
step (Fold m a b
fld, Handle
h) a
x = do
Fold m a b
r <- Fold m a b -> a -> m (Fold m a b)
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> a -> m (Fold m a b)
FL.snoc Fold m a b
fld a
x m (Fold m a b) -> m () -> m (Fold m a b)
forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`MC.onException` IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> IO ()
hClose Handle
h)
Step (Fold m a b, Handle) b -> m (Step (Fold m a b, Handle) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Fold m a b, Handle) b -> m (Step (Fold m a b, Handle) b))
-> Step (Fold m a b, Handle) b -> m (Step (Fold m a b, Handle) b)
forall a b. (a -> b) -> a -> b
$ (Fold m a b, Handle) -> Step (Fold m a b, Handle) b
forall s b. s -> Step s b
FL.Partial (Fold m a b
r, Handle
h)
extract :: p -> m ()
extract p
_ = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
final :: (Fold m a (), Handle) -> m ()
final (Fold s -> a -> m (Step s ())
_ m (Step s ())
initial1 s -> m ()
_ s -> m ()
final1, Handle
h) = do
IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Handle -> IO ()
hClose Handle
h
Step s ()
res <- m (Step s ())
initial1
case Step s ()
res of
FL.Partial s
fs -> s -> m ()
final1 s
fs
FL.Done () -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINE writeWith #-}
writeWith :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Fold m Word8 ()
writeWith :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Fold m Word8 ()
writeWith Int
n FilePath
path =
Int
-> Fold m Word8 (Array Word8)
-> Fold m (Array Word8) ()
-> Fold m Word8 ()
forall (m :: * -> *) a b c.
Monad m =>
Int -> Fold m a b -> Fold m b c -> Fold m a c
groupsOf Int
n (Int -> Fold m Word8 (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (Array a)
A.unsafePinnedCreateOf Int
n) (FilePath -> Fold m (Array Word8) ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> Fold m (Array a) ()
writeChunks FilePath
path)
{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
{-# INLINE writeWithBufferOf #-}
writeWithBufferOf :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Fold m Word8 ()
writeWithBufferOf :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Fold m Word8 ()
writeWithBufferOf = Int -> FilePath -> Fold m Word8 ()
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Fold m Word8 ()
writeWith
{-# INLINE write #-}
write :: (MonadIO m, MonadCatch m) => FilePath -> Fold m Word8 ()
write :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Fold m Word8 ()
write = Int -> FilePath -> Fold m Word8 ()
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Fold m Word8 ()
writeWith Int
defaultChunkSize
{-# INLINE writeAppendChunks #-}
writeAppendChunks :: (MonadIO m, MonadCatch m)
=> FilePath -> Stream m (Array a) -> m ()
writeAppendChunks :: forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m (Array a) -> m ()
writeAppendChunks = IOMode -> FilePath -> Stream m (Array a) -> m ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
IOMode -> FilePath -> Stream m (Array a) -> m ()
fromChunksMode IOMode
AppendMode
{-# INLINE writeAppendWith #-}
writeAppendWith :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m Word8 -> m ()
writeAppendWith :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m Word8 -> m ()
writeAppendWith Int
n FilePath
file Stream m Word8
xs =
FilePath -> Stream m (Array Word8) -> m ()
forall (m :: * -> *) a.
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m (Array a) -> m ()
writeAppendChunks FilePath
file (Stream m (Array Word8) -> m ()) -> Stream m (Array Word8) -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Stream m Word8 -> Stream m (Array Word8)
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (Array a)
IA.pinnedChunksOf Int
n Stream m Word8
xs
{-# INLINE writeAppend #-}
writeAppend :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8 -> m ()
writeAppend :: forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
FilePath -> Stream m Word8 -> m ()
writeAppend = Int -> FilePath -> Stream m Word8 -> m ()
forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
Int -> FilePath -> Stream m Word8 -> m ()
writeAppendWith Int
defaultChunkSize