package zio
import "zgo.at/zstd/zio"
Package zio implements some I/O utility functions.
Index ¶
- func ChangedFrom(file, base string) bool
- func CopyReader(r io.ReadCloser, limit int64) (full, cp io.ReadCloser)
- func Count(ctx context.Context, fp io.ReadSeeker, find []byte) (int, error)
- func DumpReader(b io.ReadCloser) (io.ReadCloser, io.ReadCloser, error)
- func ErrorReader(r io.Reader, err error) io.Reader
- func Exists(path string) bool
- func HashReader(r io.Reader, h hash.Hash) *hashReader
- func HashWriter(w io.Writer, h hash.Hash) *hashWriter
- func LimitReader(r io.Reader, n int64) io.ReadCloser
- func PeekReader(r io.Reader, peeked []byte) io.ReadCloser
- func ReadNopCloser(r io.Reader) io.ReadCloser
- func SlowReader(r io.Reader, chunkSize int, wait time.Duration) io.Reader
- func TeeReader(r io.Reader, w ...io.Writer) io.Reader
- func WriteNopCloser(w io.Writer) io.WriteCloser
- type LimitedReader
- type NopWriter
Functions ¶
func ChangedFrom ¶
Newer reports if the file's mtime is more recent than base.
func CopyReader ¶
func CopyReader(r io.ReadCloser, limit int64) (full, cp io.ReadCloser)
CopyReader copies up to limit bytes to a new reader. The "full" reader always reads the full data.
The reader may be nil or http.NoBody, in which case both return values are set to the same value.
The copy is set to "«read error: %s»" and never returns any data if any error occurs. The full reader will always return any data read before the error (if any) and then returns the exact same error.
This is intended to allow copying readers for debuggig or recording purposes, but not using a large amount of memory and not affecting the behaviour of the reader.
func Count ¶
Count the number of occurrences of find in the stream.
It will try to seek back to the original position after counting, so that something like this will just work:
fp, _ := os.Open("..")
lines, _ := zio.Count(context.Background(), fp, []byte{'\n'})
scan := bufio.NewScanner(fp)
var i int
for scan.Scan() {
fmt.Printf("line %d / %d\n", i+1, lines)
i++
}
func DumpReader ¶
func DumpReader(b io.ReadCloser) (io.ReadCloser, io.ReadCloser, error)
DumpReader reads all of b to memory and then returns two equivalent ReadClosers which will yield the same bytes.
This is useful if you want to read data from an io.Reader more than once.
It returns an error if the initial reading of all bytes fails. It does not attempt to make the returned ReadClosers have identical error-matching behavior.
See zio.DumpBody for an example usage.
func ErrorReader ¶
func ErrorReader(r io.Reader, err error) io.Reader
ErrorReader read all data from r and returns err on io.EOF.
func Exists ¶
Exists reports if a path exists.
func HashReader ¶
func HashReader(r io.Reader, h hash.Hash) *hashReader
HashReader writes to the hash for all data it reads.
func HashWriter ¶
func HashWriter(w io.Writer, h hash.Hash) *hashWriter
HashWriter writes to both the writer and hash.
func LimitReader ¶
func LimitReader(r io.Reader, n int64) io.ReadCloser
LimitReader returns a Reader that reads from r but stops with EOF after n bytes. The underlying implementation is a *LimitedReader.
This is identical to io.LimitReader, except that it calls Close on the reader if it has a Close method.
func PeekReader ¶
func PeekReader(r io.Reader, peeked []byte) io.ReadCloser
PeekReader returns a reader that first returns data from peeked, before reading from the reader r.
This is useful in cases where you want to "peek" a bit data from a reader that doesn't support seeking to determine if the compression or file format.
func ReadNopCloser ¶
func ReadNopCloser(r io.Reader) io.ReadCloser
ReadNopCloser is like io.NopCloser, but reads return fs.ErrClosed after the first close.
This is especially useful for tests, as readers may be closed which has no effect with io.NopCloser(), and may behave different from production code in some error cases.
func SlowReader ¶
func SlowReader(r io.Reader, chunkSize int, wait time.Duration) io.Reader
SlowReader returns a reader that waits after reading chunkSize bytes.
This is mainly intended for testing timeouts and such, where you want an intentionally slow reader.
func TeeReader ¶
func TeeReader(r io.Reader, w ...io.Writer) io.Reader
TeeReader returns a [Reader] that writes to w what it reads from r.
All reads from r performed through it are matched with corresponding writes to w. There is no internal buffering - the write must complete before the read completes. Any error encountered while writing is reported as a read error.
This is simular to io.TeeReader, except that it supports multiple writers.
func WriteNopCloser ¶
func WriteNopCloser(w io.Writer) io.WriteCloser
WriteNopCloser is the io.Writer variant of ReadNopCloser.
Types ¶
type LimitedReader ¶
type LimitedReader struct {
R io.Reader // underlying reader
N int64 // max bytes remaining
}
A LimitedReader reads from R but limits the amount of data returned to just N bytes. Each call to Read updates N to reflect the new amount remaining.
Read returns EOF when N <= 0 or when the underlying R returns EOF.
This is identical to io.LimiterReader, except that it calls Close on the reader if it has a Close method.
func (*LimitedReader) Close ¶
func (l *LimitedReader) Close() error
Close the underlying reader if it implements a Close method.
func (*LimitedReader) Read ¶
func (l *LimitedReader) Read(p []byte) (n int, err error)
type NopWriter ¶
type NopWriter struct{}
NopWriter is an io.Writer that does nothing.
func (*NopWriter) Write ¶
Write is a stub.
Source Files ¶
zio.go
- Version
- v0.0.0-20261028170124-7561b8637ff8 (latest)
- Published
- Oct 28, 2026
- Platform
- linux/amd64
- Imports
- 11 packages
- Last checked
- 2 minutes ago –
Tools for package owners.