From 4fd7676e92f160492e8781adc5c1ced94ab282f1 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 29 Nov 2015 14:27:52 +0100 Subject: [PATCH] HashingWriter: Add documentation --- backend/writer.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/backend/writer.go b/backend/writer.go index ae2c5bfd9..6bee58b49 100644 --- a/backend/writer.go +++ b/backend/writer.go @@ -46,12 +46,14 @@ func (h *HashAppendWriter) Write(p []byte) (n int, err error) { return 0, errors.New("Write() called on closed HashAppendWriter") } +// HashingWriter wraps an io.Writer to hashes all data that is written to it. type HashingWriter struct { w io.Writer h hash.Hash size int } +// NewHashAppendWriter wraps the writer w and feeds all data written to the hash h. func NewHashingWriter(w io.Writer, h hash.Hash) *HashingWriter { return &HashingWriter{ h: h, @@ -59,16 +61,19 @@ func NewHashingWriter(w io.Writer, h hash.Hash) *HashingWriter { } } +// Write wraps the write method of the underlying writer and also hashes all data. func (h *HashingWriter) Write(p []byte) (int, error) { n, err := h.w.Write(p) h.size += n return n, err } +// Sum returns the hash of all data written so far. func (h *HashingWriter) Sum(d []byte) []byte { return h.h.Sum(d) } +// Size returns the number of bytes written to the underlying writer. func (h *HashingWriter) Size() int { return h.size }