hashing: Remove io.WriterTo implementation
This functionality has gone unused since4b3dc415ef
changed hashing.Reader's only client to use ioutil.ReadAll on a bufio.Reader wrapping the hashing Reader. Revertsbcb852a8d0
.
This commit is contained in:
parent
ab49c14621
commit
54b8337813
2 changed files with 25 additions and 87 deletions
|
@ -5,47 +5,25 @@ import (
|
|||
"io"
|
||||
)
|
||||
|
||||
// ReadSumer hashes all data read from the underlying reader.
|
||||
type ReadSumer interface {
|
||||
io.Reader
|
||||
// Sum returns the hash of the data read so far.
|
||||
Sum(d []byte) []byte
|
||||
}
|
||||
|
||||
type reader struct {
|
||||
io.Reader
|
||||
// Reader hashes all data read from the underlying reader.
|
||||
type Reader struct {
|
||||
r io.Reader
|
||||
h hash.Hash
|
||||
}
|
||||
|
||||
type readWriterTo struct {
|
||||
reader
|
||||
writerTo io.WriterTo
|
||||
// NewReader returns a new Reader that uses the hash h. If the underlying
|
||||
// reader supports WriteTo then the returned reader will do so too.
|
||||
func NewReader(r io.Reader, h hash.Hash) *Reader {
|
||||
return &Reader{r: r, h: h}
|
||||
}
|
||||
|
||||
// NewReader returns a new ReadSummer that uses the hash h. If the underlying
|
||||
// reader supports WriteTo then the returned reader will do so too.
|
||||
func NewReader(r io.Reader, h hash.Hash) ReadSumer {
|
||||
rs := reader{
|
||||
Reader: io.TeeReader(r, h),
|
||||
h: h,
|
||||
}
|
||||
|
||||
if _, ok := r.(io.WriterTo); ok {
|
||||
return &readWriterTo{
|
||||
reader: rs,
|
||||
writerTo: r.(io.WriterTo),
|
||||
}
|
||||
}
|
||||
|
||||
return &rs
|
||||
func (h *Reader) Read(p []byte) (int, error) {
|
||||
n, err := h.r.Read(p)
|
||||
_, _ = h.h.Write(p[:n]) // Never returns an error.
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Sum returns the hash of the data read so far.
|
||||
func (h *reader) Sum(d []byte) []byte {
|
||||
func (h *Reader) Sum(d []byte) []byte {
|
||||
return h.h.Sum(d)
|
||||
}
|
||||
|
||||
// WriteTo reads all data into the passed writer
|
||||
func (h *readWriterTo) WriteTo(w io.Writer) (int64, error) {
|
||||
return h.writerTo.WriteTo(NewWriter(w, h.h))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue