jx/internal/iofilter/filter.go

37 lines
646 B
Go

// 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 {
input io.ReadCloser
filter ReadFilter
}
func NewReader(reader io.ReadCloser, filter ReadFilter) *Reader {
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
}
func (r *Reader) Close() error {
return r.input.Close()
}