Z-IO-0.1.0.0: A simple and high performance IO toolkit for Haskell
Copyright(c) Dong Han 2017-2018
LicenseBSD
Maintainerwinterland1989@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Z.IO.Buffered

Description

This module provide buffered IO interface.

Synopsis

Input & Output device

class Input i where Source #

Input device

readInput should return 0 on EOF.

Methods

readInput :: HasCallStack => i -> Ptr Word8 -> Int -> IO Int Source #

Instances

Instances details
Input File Source # 
Instance details

Defined in Z.IO.FileSystem

Methods

readInput :: File -> Ptr Word8 -> Int -> IO Int Source #

Input UVStream Source # 
Instance details

Defined in Z.IO.UV.Manager

Methods

readInput :: UVStream -> Ptr Word8 -> Int -> IO Int Source #

Input StdStream Source # 
Instance details

Defined in Z.IO.StdStream

Input FileT Source # 
Instance details

Defined in Z.IO.FileSystem.Threaded

Methods

readInput :: FileT -> Ptr Word8 -> Int -> IO Int Source #

class Output o where Source #

Output device

writeOutput should not return until all data are written (may not necessarily flushed to hardware, that should be done in device specific way).

Methods

writeOutput :: HasCallStack => o -> Ptr Word8 -> Int -> IO () Source #

Instances

Instances details
Output File Source # 
Instance details

Defined in Z.IO.FileSystem

Methods

writeOutput :: File -> Ptr Word8 -> Int -> IO () Source #

Output UVStream Source # 
Instance details

Defined in Z.IO.UV.Manager

Methods

writeOutput :: UVStream -> Ptr Word8 -> Int -> IO () Source #

Output StdStream Source # 
Instance details

Defined in Z.IO.StdStream

Methods

writeOutput :: StdStream -> Ptr Word8 -> Int -> IO () Source #

Output FileT Source # 
Instance details

Defined in Z.IO.FileSystem.Threaded

Methods

writeOutput :: FileT -> Ptr Word8 -> Int -> IO () Source #

Buffered Input

data BufferedInput i Source #

Input device with buffer, NOT THREAD SAFE!

  • A BufferedInput should not be used in multiple threads, there's no locking mechanism to protect buffering state.
  • A Input device should only be used with a single BufferedInput, If multiple BufferedInput s are opened on a same Input device, the behaviour is undefined.

newBufferedInput Source #

Arguments

:: Int

Input buffer size

-> input 
-> IO (BufferedInput input) 

Open a new buffered input with given buffer size, e.g. defaultChunkSize.

readBuffer :: (HasCallStack, Input i) => BufferedInput i -> IO Bytes Source #

Request bytes from BufferedInput.

The buffering logic is quite simple:

If we have pushed back bytes, directly return it, otherwise we read using buffer size. If we read N bytes, and N is larger than half of the buffer size, then we freeze buffer and return, otherwise we copy buffer into result and reuse buffer afterward.

unReadBuffer :: (HasCallStack, Input i) => Bytes -> BufferedInput i -> IO () Source #

Push bytes back into buffer.

readParser :: (HasCallStack, Input i) => Parser a -> BufferedInput i -> IO (Bytes, Either ParseError a) Source #

Read buffer and parse with Parser.

This function will continuously draw data from input before parsing finish.

readExactly :: (HasCallStack, Input i) => Int -> BufferedInput i -> IO Bytes Source #

Read exactly N bytes

If EOF reached before N bytes read, a ShortReadException will be thrown

readToMagic :: (HasCallStack, Input i) => Word8 -> BufferedInput i -> IO Bytes Source #

Read until reach a magic bytes

If EOF is reached before meet a magic byte, partial bytes are returned.

readToMagic' :: (HasCallStack, Input i) => Word8 -> BufferedInput i -> IO Bytes Source #

Read until reach a magic bytes

If EOF is reached before meet a magic byte, a ShortReadException will be thrown.

readLine :: (HasCallStack, Input i) => BufferedInput i -> IO Bytes Source #

Read to a linefeed ('n' or 'rn'), return Bytes before it.

If EOF is reached before meet a magic byte, partial line is returned.

readLine' :: (HasCallStack, Input i) => BufferedInput i -> IO Bytes Source #

Read to a linefeed ('n' or 'rn'), return Bytes before it.

If EOF reached before meet a 'n', a ShortReadException will be thrown.

readAll :: (HasCallStack, Input i) => BufferedInput i -> IO [Bytes] Source #

Read all chunks from a BufferedInput.

readAll' :: (HasCallStack, Input i) => BufferedInput i -> IO Bytes Source #

Read all chunks from a BufferedInput, and concat chunks together.

Buffered Output

data BufferedOutput o Source #

Output device with buffer, NOT THREAD SAFE!

newBufferedOutput Source #

Arguments

:: Int

Output buffer size

-> output 
-> IO (BufferedOutput output) 

Open a new buffered output with given buffer size, e.g. defaultChunkSize.

writeBuffer :: Output o => BufferedOutput o -> Bytes -> IO () Source #

Write Bytes into buffered handle.

Copy Bytes to buffer if it can hold, otherwise write both buffer(if not empty) and Bytes.

writeBuilder :: Output o => BufferedOutput o -> Builder a -> IO () Source #

Write Bytes into buffered handle.

Copy Bytes to buffer if it can hold, otherwise write both buffer(if not empty) and Bytes.

flushBuffer :: Output f => BufferedOutput f -> IO () Source #

Flush the buffer into output device(if not empty).

Exceptions

common buffer size