mirror of
https://github.com/golang/go
synced 2024-09-23 23:10:13 -06:00
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
This commit is contained in:
parent
4de6687554
commit
4432be3b28
@ -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.
|
||||
|
@ -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 {
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user