Commit 4432be3b authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

compress/gzip: add Writer.Flush to call flate.Writer's Flush

From a discussion on golang-nuts.

R=golang-dev, dsymonds, nigeltao, coocood, adg
CC=golang-dev
https://golang.org/cl/8251043
parent 4de66875
...@@ -631,6 +631,14 @@ so it implements the ...@@ -631,6 +631,14 @@ so it implements the
<a href="/pkg/io/#WriterTo"><code>io.WriterTo</code></a> interface. <a href="/pkg/io/#WriterTo"><code>io.WriterTo</code></a> interface.
</li> </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> <li>
The <a href="/pkg/crypto/hmac/"><code>crypto/hmac</code></a> package has a new function, 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. <a href="/pkg/crypto/hmac/#Equal"><code>Equal</code></a>, to compare two MACs.
......
...@@ -28,7 +28,7 @@ type Writer struct { ...@@ -28,7 +28,7 @@ type Writer struct {
Header Header
w io.Writer w io.Writer
level int level int
compressor io.WriteCloser compressor *flate.Writer
digest hash.Hash32 digest hash.Hash32
size uint32 size uint32
closed bool closed bool
...@@ -191,6 +191,28 @@ func (z *Writer) Write(p []byte) (int, error) { ...@@ -191,6 +191,28 @@ func (z *Writer) Write(p []byte) (int, error) {
return n, z.err 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. // Close closes the Writer. It does not close the underlying io.Writer.
func (z *Writer) Close() error { func (z *Writer) Close() error {
if z.err != nil { if z.err != nil {
......
...@@ -157,3 +157,43 @@ func TestLatin1RoundTrip(t *testing.T) { ...@@ -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")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment