ioutil

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

ioutil

Package ioutil provides I/O hardened operations.

Variables

ErrReaderTimedOut is raised when the reader doesn't received data for a predeterminined time.

var ErrReaderTimedOut = errors.New("reader timed out")

ErrTruncatedCopy is raised when the copy is larger than expected.

var ErrTruncatedCopy = errors.New("truncated copy due to too large input")

Functions

func LimitCopy

func LimitCopy(dst io.Writer, src io.Reader, maxSize uint64) (uint64, error)

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.

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

func LimitWriter(w io.Writer, limit int) io.Writer

LimitWriter create a new Writer that accepts at most 'limit' bytes.

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

func TimeoutReader(reader io.Reader, timeout time.Duration) io.Reader

TimeoutReader create a timed-out limited reader instance.

// 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

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

func LimitCopy(dst io.Writer, src io.Reader, maxSize uint64) (uint64, error)

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

func LimitWriter(w io.Writer, limit int) io.Writer

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

func TimeoutReader(reader io.Reader, timeout time.Duration) io.Reader

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL