From fb2cd7b48510531b238b46c4659195f5bf202225 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 16 Feb 2015 00:24:43 +0100 Subject: [PATCH] HashingWriter: Track size --- backend/writer.go | 13 ++++++++++--- backend/writer_test.go | 4 ++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/backend/writer.go b/backend/writer.go index 55ffb3279..17e7d0653 100644 --- a/backend/writer.go +++ b/backend/writer.go @@ -43,8 +43,9 @@ func (h *HashAppendWriter) Write(p []byte) (n int, err error) { } type HashingWriter struct { - w io.Writer - h hash.Hash + w io.Writer + h hash.Hash + size int } func NewHashingWriter(w io.Writer, h hash.Hash) *HashingWriter { @@ -55,9 +56,15 @@ func NewHashingWriter(w io.Writer, h hash.Hash) *HashingWriter { } func (h *HashingWriter) Write(p []byte) (int, error) { - return h.w.Write(p) + n, err := h.w.Write(p) + h.size += n + return n, err } func (h *HashingWriter) Sum(d []byte) []byte { return h.h.Sum(d) } + +func (h *HashingWriter) Size() int { + return h.size +} diff --git a/backend/writer_test.go b/backend/writer_test.go index bf4655c25..85d55f8f2 100644 --- a/backend/writer_test.go +++ b/backend/writer_test.go @@ -68,6 +68,10 @@ func TestHashingWriter(t *testing.T) { "HashAppendWriter: invalid number of bytes written: got %d, expected %d", n, size) + assert(t, wr.Size() == size, + "HashAppendWriter: invalid number of bytes returned: got %d, expected %d", + wr.Size, size) + resultingHash := wr.Sum(nil) assert(t, bytes.Equal(expectedHash[:], resultingHash), "HashAppendWriter: hashes do not match: expected %02x, got %02x",