]> Cypherpunks repositories - gostls13.git/commitdiff
ioutil: add Discard, update tree.
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 27 Apr 2011 22:47:04 +0000 (15:47 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 27 Apr 2011 22:47:04 +0000 (15:47 -0700)
This also removes an unnecessary allocation in
http/transfer.go

R=r, rsc1, r2, adg
CC=golang-dev
https://golang.org/cl/4426066

src/pkg/compress/lzw/reader_test.go
src/pkg/compress/lzw/writer_test.go
src/pkg/html/parse_test.go
src/pkg/http/transfer.go
src/pkg/io/ioutil/ioutil.go
src/pkg/mime/multipart/multipart.go

index 4b5dfaadea2023c51568f4109615decb22a2c683..72121a6b5696261fbd988c583fb316f9fb3fcf13 100644 (file)
@@ -112,12 +112,6 @@ func TestReader(t *testing.T) {
        }
 }
 
-type devNull struct{}
-
-func (devNull) Write(p []byte) (int, os.Error) {
-       return len(p), nil
-}
-
 func benchmarkDecoder(b *testing.B, n int) {
        b.StopTimer()
        b.SetBytes(int64(n))
@@ -134,7 +128,7 @@ func benchmarkDecoder(b *testing.B, n int) {
        runtime.GC()
        b.StartTimer()
        for i := 0; i < b.N; i++ {
-               io.Copy(devNull{}, NewReader(bytes.NewBuffer(buf1), LSB, 8))
+               io.Copy(ioutil.Discard, NewReader(bytes.NewBuffer(buf1), LSB, 8))
        }
 }
 
index e5815a03d5d628353ac33dca133217f3c24d0308..82464ecd1b0811b23eadc5b00cdeca7c9b07ccdb 100644 (file)
@@ -113,7 +113,7 @@ func benchmarkEncoder(b *testing.B, n int) {
        runtime.GC()
        b.StartTimer()
        for i := 0; i < b.N; i++ {
-               w := NewWriter(devNull{}, LSB, 8)
+               w := NewWriter(ioutil.Discard, LSB, 8)
                w.Write(buf1)
                w.Close()
        }
index fe955436c8a4bd609ed3443f4f0b558e73798eeb..3fa35d5dbe4c5eb3fd0cc5a59884cfb92c3be162 100644 (file)
@@ -15,12 +15,6 @@ import (
        "testing"
 )
 
-type devNull struct{}
-
-func (devNull) Write(p []byte) (int, os.Error) {
-       return len(p), nil
-}
-
 func pipeErr(err os.Error) io.Reader {
        pr, pw := io.Pipe()
        pw.CloseWithError(err)
@@ -141,7 +135,7 @@ func TestParser(t *testing.T) {
                                t.Fatal(err)
                        }
                        // Skip the #error section.
-                       if _, err := io.Copy(devNull{}, <-rc); err != nil {
+                       if _, err := io.Copy(ioutil.Discard, <-rc); err != nil {
                                t.Fatal(err)
                        }
                        // Compare the parsed tree to the #document section.
index 41614f144fe3ac999ba8338a2fdea81e97be0a4b..98c32bab64c1653d8682af0d6efe97072de3800f 100644 (file)
@@ -7,6 +7,7 @@ package http
 import (
        "bufio"
        "io"
+       "io/ioutil"
        "os"
        "strconv"
        "strings"
@@ -447,17 +448,10 @@ func (b *body) Close() os.Error {
                return nil
        }
 
-       trashBuf := make([]byte, 1024) // local for thread safety
-       for {
-               _, err := b.Read(trashBuf)
-               if err == nil {
-                       continue
-               }
-               if err == os.EOF {
-                       break
-               }
+       if _, err := io.Copy(ioutil.Discard, b); err != nil {
                return err
        }
+
        if b.hdr == nil { // not reading trailer
                return nil
        }
index ac481928b44934221f549f807f29063cec20525f..5f1eecaabedd7127be53a9decfb28035fda71731 100644 (file)
@@ -101,3 +101,13 @@ func (nopCloser) Close() os.Error { return nil }
 func NopCloser(r io.Reader) io.ReadCloser {
        return nopCloser{r}
 }
+
+type devNull int
+
+func (devNull) Write(p []byte) (int, os.Error) {
+       return len(p), nil
+}
+
+// Discard is an io.Writer on which all Write calls succeed
+// without doing anything.
+var Discard io.Writer = devNull(0)
index 22576cff462f08de0f54a2c3c03312cf73343e91..f857db1a08b661f036a64c62d701786e0ac17d37 100644 (file)
@@ -16,6 +16,7 @@ import (
        "bufio"
        "bytes"
        "io"
+       "io/ioutil"
        "mime"
        "net/textproto"
        "os"
@@ -76,14 +77,6 @@ func NewReader(reader io.Reader, boundary string) Reader {
 
 // Implementation ....
 
-type devNullWriter bool
-
-func (*devNullWriter) Write(p []byte) (n int, err os.Error) {
-       return len(p), nil
-}
-
-var devNull = devNullWriter(false)
-
 func newPart(mr *multiReader) (bp *Part, err os.Error) {
        bp = new(Part)
        bp.Header = make(map[string][]string)
@@ -158,7 +151,7 @@ func (bp *Part) Read(p []byte) (n int, err os.Error) {
 }
 
 func (bp *Part) Close() os.Error {
-       io.Copy(&devNull, bp)
+       io.Copy(ioutil.Discard, bp)
        return nil
 }