Documentation
¶
Overview ¶
Package ioutil provides I/O hardened operations.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrReaderTimedOut = errors.New("reader timed out")
ErrReaderTimedOut is raised when the reader doesn't received data for a predeterminined time.
View Source
var ErrTruncatedCopy = errors.New("truncated copy due to too large input")
ErrTruncatedCopy is raised when the copy is larger than expected.
Functions ¶
func LimitCopy ¶
LimitCopy uses a buffered CopyN and a hardlimit to stop read from the reader when the maxSize amount of data has been written to the given writer and raise an error.
Example ¶
root := os.DirFS("./testdata")
// Open 1Gb gzip bomb
bomb, err := root.Open("1g.gz")
if err != nil {
panic(err)
}
// Pass through the GZIP decompression reader
gzr, err := gzip.NewReader(bomb)
if err != nil {
panic(err)
}
// Copy decompressed data with hard limit to 1Mb.
//
// Why not using an io.LimitReader? Because the LimitReader truncate the
// data without raising an error.
_, err = LimitCopy(io.Discard, gzr, 1024)
Output: truncated copy due to too large input
func LimitWriter ¶
LimitWriter create a new Writer that accepts at most 'limit' bytes.
Example ¶
out := bytes.Buffer{}
lw := LimitWriter(&out, 1024)
// Copy data from the reader
_, err := io.CopyN(lw, rand.Reader, 2048)
if err != nil {
panic(err)
}
Output: 1024
func TimeoutReader ¶
TimeoutReader create a timed-out limited reader instance.
Example ¶
// Can be any reader (os.Stdin, Sockets, etc.)
tr := TimeoutReader(&slowReader{
// The reader will block for 1s.
timeout: time.Second,
err: io.EOF,
}, time.Millisecond)
// Copy data from the reader
_, err := io.Copy(io.Discard, tr)
Output: reader timed out
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.