]> Cypherpunks repositories - gostls13.git/commitdiff
compress/gzip: add Writer.Flush to call flate.Writer's Flush
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 2 Apr 2013 16:07:43 +0000 (09:07 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 2 Apr 2013 16:07:43 +0000 (09:07 -0700)
From a discussion on golang-nuts.

R=golang-dev, dsymonds, nigeltao, coocood, adg
CC=golang-dev
https://golang.org/cl/8251043

doc/go1.1.html
src/pkg/compress/gzip/gzip.go
src/pkg/compress/gzip/gzip_test.go

index dcbd5e7872ed7cce2c747af9e5b309b3414d5b52..75eb02d45c8bb7e1b31107a5ce0d9b2a9e039eb3 100644 (file)
@@ -631,6 +631,14 @@ so it implements the
 <a href="/pkg/io/#WriterTo"><code>io.WriterTo</code></a> interface.
 </li>
 
+<li>
+The <a href="/pkg/compress/gzip/"><code>compress/gzip</code></a> package has
+a new <a href="/pkg/compress/gzip/#Writer.Flush"><code>Flush</code></a>
+method for its
+<a href="/pkg/compress/gzip/#Writer"><code>Writer</code></a>
+type that flushes its underlying <code>flate.Writer</code>.
+</li>
+
 <li>
 The <a href="/pkg/crypto/hmac/"><code>crypto/hmac</code></a> package has a new function,
 <a href="/pkg/crypto/hmac/#Equal"><code>Equal</code></a>, to compare two MACs.
index 3035dfffccf31ded5a87c7856b3e7177bb7275c0..45558b74289409559751a927ecf47eaf8d417eba 100644 (file)
@@ -28,7 +28,7 @@ type Writer struct {
        Header
        w          io.Writer
        level      int
-       compressor io.WriteCloser
+       compressor *flate.Writer
        digest     hash.Hash32
        size       uint32
        closed     bool
@@ -191,6 +191,28 @@ func (z *Writer) Write(p []byte) (int, error) {
        return n, z.err
 }
 
+// Flush flushes any pending compressed data to the underlying writer.
+//
+// It is useful mainly in compressed network protocols, to ensure that
+// a remote reader has enough data to reconstruct a packet. Flush does
+// not return until the data has been written. If the underlying
+// writer returns an error, Flush returns that error.
+//
+// In the terminology of the zlib library, Flush is equivalent to Z_SYNC_FLUSH.
+func (z *Writer) Flush() error {
+       if z.err != nil {
+               return z.err
+       }
+       if z.closed {
+               return nil
+       }
+       if z.compressor == nil {
+               z.Write(nil)
+       }
+       z.err = z.compressor.Flush()
+       return z.err
+}
+
 // Close closes the Writer. It does not close the underlying io.Writer.
 func (z *Writer) Close() error {
        if z.err != nil {
index 6f7b593644975ae3cc3159c9625c3c61104c2ee8..4d1af94381cab54ff234ef9e9d73e740c7179200 100644 (file)
@@ -157,3 +157,43 @@ func TestLatin1RoundTrip(t *testing.T) {
                }
        }
 }
+
+func TestWriterFlush(t *testing.T) {
+       buf := new(bytes.Buffer)
+
+       w := NewWriter(buf)
+       w.Comment = "comment"
+       w.Extra = []byte("extra")
+       w.ModTime = time.Unix(1e8, 0)
+       w.Name = "name"
+
+       n0 := buf.Len()
+       if n0 != 0 {
+               t.Fatalf("buffer size = %d before writes; want 0", n0)
+       }
+
+       if err := w.Flush(); err != nil {
+               t.Fatal(err)
+       }
+
+       n1 := buf.Len()
+       if n1 == 0 {
+               t.Fatal("no data after first flush")
+       }
+
+       w.Write([]byte("x"))
+
+       n2 := buf.Len()
+       if n1 != n2 {
+               t.Fatalf("after writing a single byte, size changed from %d to %d; want no change", n1, n2)
+       }
+
+       if err := w.Flush(); err != nil {
+               t.Fatal(err)
+       }
+
+       n3 := buf.Len()
+       if n2 == n3 {
+               t.Fatal("Flush didn't flush any data")
+       }
+}