2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-25 04:05:58 +00:00

HashingWriter: Add documentation

This commit is contained in:
Alexander Neumann 2015-11-29 14:27:52 +01:00
parent 8209bb309b
commit 4fd7676e92

View File

@ -46,12 +46,14 @@ func (h *HashAppendWriter) Write(p []byte) (n int, err error) {
return 0, errors.New("Write() called on closed HashAppendWriter") 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 { type HashingWriter struct {
w io.Writer w io.Writer
h hash.Hash h hash.Hash
size int 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 { func NewHashingWriter(w io.Writer, h hash.Hash) *HashingWriter {
return &HashingWriter{ return &HashingWriter{
h: h, 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) { func (h *HashingWriter) Write(p []byte) (int, error) {
n, err := h.w.Write(p) n, err := h.w.Write(p)
h.size += n h.size += n
return n, err return n, err
} }
// Sum returns the hash of all data written so far.
func (h *HashingWriter) Sum(d []byte) []byte { func (h *HashingWriter) Sum(d []byte) []byte {
return h.h.Sum(d) return h.h.Sum(d)
} }
// Size returns the number of bytes written to the underlying writer.
func (h *HashingWriter) Size() int { func (h *HashingWriter) Size() int {
return h.size return h.size
} }