jx/internal/iofilter/filter.go

37 lines
646 B
Go
Raw Normal View History

2024-04-17 23:49:41 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package iofilter
import (
"io"
"log/slog"
)
type ReadFilter func([]byte, int, error) (int, error)
type Reader struct {
2024-09-19 08:18:48 +00:00
input io.ReadCloser
2024-04-17 23:49:41 +00:00
filter ReadFilter
}
2024-09-19 08:18:48 +00:00
func NewReader(reader io.ReadCloser, filter ReadFilter) *Reader {
2024-04-17 23:49:41 +00:00
return &Reader{
input: reader,
filter: filter,
}
}
func (r *Reader) Read(p []byte) (n int, err error) {
n, err = r.input.Read(p)
if r.filter != nil {
return r.filter(p, n, err)
} else {
slog.Warn("iofilter.Reader.Read() - nil filter")
}
return n, err
}
2024-09-19 08:18:48 +00:00
func (r *Reader) Close() error {
return r.input.Close()
}