aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Daniel Kortschak <dan.kortschak@adelaide.edu.au>2014-04-16 22:43:44 -0400
committerRuss Cox <rsc@golang.org>2014-04-16 22:43:44 -0400
commit6277cc3df578455c22a0da4deb4213d8db4c9ffe (patch)
tree4e7daadb2ce0069b2363a29b99c6ed04f21ade9b
parent6f25f1d4c901417af1da65e41992d71c30f64f8f (diff)
downloadgo-6277cc3df578455c22a0da4deb4213d8db4c9ffe.tar.gz
go-6277cc3df578455c22a0da4deb4213d8db4c9ffe.zip
compress/gzip: add Reset method to Reader
Fixes #6364. LGTM=rsc R=golang-codereviews, bradfitz, rsc, gobot CC=golang-codereviews https://golang.org/cl/13512052
-rw-r--r--src/pkg/compress/gzip/gunzip.go11
-rw-r--r--src/pkg/compress/gzip/gunzip_test.go20
2 files changed, 31 insertions, 0 deletions
diff --git a/src/pkg/compress/gzip/gunzip.go b/src/pkg/compress/gzip/gunzip.go
index 1fb9b0964c..d18269cfd1 100644
--- a/src/pkg/compress/gzip/gunzip.go
+++ b/src/pkg/compress/gzip/gunzip.go
@@ -89,6 +89,17 @@ func NewReader(r io.Reader) (*Reader, error) {
return z, nil
}
+// Reset discards the Reader z's state and makes it equivalent to the
+// result of its original state from NewReader, but reading from r instead.
+// This permits reusing a Reader rather than allocating a new one.
+func (z *Reader) Reset(r io.Reader) error {
+ z.r = makeReader(r)
+ z.digest.Reset()
+ z.size = 0
+ z.err = nil
+ return z.readHeader(true)
+}
+
// GZIP (RFC 1952) is little-endian, unlike ZLIB (RFC 1950).
func get4(p []byte) uint32 {
return uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24
diff --git a/src/pkg/compress/gzip/gunzip_test.go b/src/pkg/compress/gzip/gunzip_test.go
index 5615373737..1d5d8d9cc5 100644
--- a/src/pkg/compress/gzip/gunzip_test.go
+++ b/src/pkg/compress/gzip/gunzip_test.go
@@ -303,6 +303,26 @@ func TestDecompressor(t *testing.T) {
if s != tt.raw {
t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.name, n, s, len(tt.raw), tt.raw)
}
+
+ // Test Reader Reset.
+ in = bytes.NewReader(tt.gzip)
+ err = gzip.Reset(in)
+ if err != nil {
+ t.Errorf("%s: Reset: %s", tt.name, err)
+ continue
+ }
+ if tt.name != gzip.Name {
+ t.Errorf("%s: got name %s", tt.name, gzip.Name)
+ }
+ b.Reset()
+ n, err = io.Copy(b, gzip)
+ if err != tt.err {
+ t.Errorf("%s: io.Copy: %v want %v", tt.name, err, tt.err)
+ }
+ s = b.String()
+ if s != tt.raw {
+ t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.name, n, s, len(tt.raw), tt.raw)
+ }
}
}