2023-01-11 11:13:13 +00:00
|
|
|
package iotools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2024-07-12 09:39:47 +00:00
|
|
|
// ReadCloserType implements io.ReadCloser
|
|
|
|
// by combining the two underlying interfaces,
|
|
|
|
// while providing an exported type to still
|
|
|
|
// access the underlying original io.Reader or
|
|
|
|
// io.Closer separately (e.g. without wrapping).
|
|
|
|
type ReadCloserType struct {
|
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
2023-01-11 11:13:13 +00:00
|
|
|
// ReaderFunc is a function signature which allows
|
|
|
|
// a function to implement the io.Reader type.
|
|
|
|
type ReaderFunc func([]byte) (int, error)
|
|
|
|
|
|
|
|
func (r ReaderFunc) Read(b []byte) (int, error) {
|
|
|
|
return r(b)
|
|
|
|
}
|
|
|
|
|
2023-10-31 11:12:22 +00:00
|
|
|
// ReaderFromFunc is a function signature which allows
|
|
|
|
// a function to implement the io.ReaderFrom type.
|
|
|
|
type ReaderFromFunc func(io.Reader) (int64, error)
|
|
|
|
|
|
|
|
func (rf ReaderFromFunc) ReadFrom(r io.Reader) (int64, error) {
|
|
|
|
return rf(r)
|
|
|
|
}
|
|
|
|
|
2023-01-11 11:13:13 +00:00
|
|
|
// ReadCloser wraps an io.Reader and io.Closer in order to implement io.ReadCloser.
|
|
|
|
func ReadCloser(r io.Reader, c io.Closer) io.ReadCloser {
|
2024-07-12 09:39:47 +00:00
|
|
|
return &ReadCloserType{r, c}
|
2023-01-11 11:13:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 09:39:47 +00:00
|
|
|
// NopReadCloser wraps io.Reader with NopCloser{} in ReadCloserType.
|
2023-01-11 11:13:13 +00:00
|
|
|
func NopReadCloser(r io.Reader) io.ReadCloser {
|
2024-07-12 09:39:47 +00:00
|
|
|
return &ReadCloserType{r, NopCloser{}}
|
2023-01-11 11:13:13 +00:00
|
|
|
}
|