1
0
mirror of https://github.com/golang/go synced 2024-11-25 09:37:56 -07:00

ioutil: add Discard, update tree.

This also removes an unnecessary allocation in
http/transfer.go

R=r, rsc1, r2, adg
CC=golang-dev
https://golang.org/cl/4426066
This commit is contained in:
Brad Fitzpatrick 2011-04-27 15:47:04 -07:00
parent ec3fe2a5b6
commit 9d12307a12
6 changed files with 18 additions and 33 deletions

View 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) { func benchmarkDecoder(b *testing.B, n int) {
b.StopTimer() b.StopTimer()
b.SetBytes(int64(n)) b.SetBytes(int64(n))
@ -134,7 +128,7 @@ func benchmarkDecoder(b *testing.B, n int) {
runtime.GC() runtime.GC()
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { 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))
} }
} }

View File

@ -113,7 +113,7 @@ func benchmarkEncoder(b *testing.B, n int) {
runtime.GC() runtime.GC()
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
w := NewWriter(devNull{}, LSB, 8) w := NewWriter(ioutil.Discard, LSB, 8)
w.Write(buf1) w.Write(buf1)
w.Close() w.Close()
} }

View File

@ -15,12 +15,6 @@ import (
"testing" "testing"
) )
type devNull struct{}
func (devNull) Write(p []byte) (int, os.Error) {
return len(p), nil
}
func pipeErr(err os.Error) io.Reader { func pipeErr(err os.Error) io.Reader {
pr, pw := io.Pipe() pr, pw := io.Pipe()
pw.CloseWithError(err) pw.CloseWithError(err)
@ -141,7 +135,7 @@ func TestParser(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
// Skip the #error section. // Skip the #error section.
if _, err := io.Copy(devNull{}, <-rc); err != nil { if _, err := io.Copy(ioutil.Discard, <-rc); err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Compare the parsed tree to the #document section. // Compare the parsed tree to the #document section.

View File

@ -7,6 +7,7 @@ package http
import ( import (
"bufio" "bufio"
"io" "io"
"io/ioutil"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -447,17 +448,10 @@ func (b *body) Close() os.Error {
return nil return nil
} }
trashBuf := make([]byte, 1024) // local for thread safety if _, err := io.Copy(ioutil.Discard, b); err != nil {
for {
_, err := b.Read(trashBuf)
if err == nil {
continue
}
if err == os.EOF {
break
}
return err return err
} }
if b.hdr == nil { // not reading trailer if b.hdr == nil { // not reading trailer
return nil return nil
} }

View File

@ -101,3 +101,13 @@ func (nopCloser) Close() os.Error { return nil }
func NopCloser(r io.Reader) io.ReadCloser { func NopCloser(r io.Reader) io.ReadCloser {
return nopCloser{r} 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)

View File

@ -16,6 +16,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"io" "io"
"io/ioutil"
"mime" "mime"
"net/textproto" "net/textproto"
"os" "os"
@ -76,14 +77,6 @@ func NewReader(reader io.Reader, boundary string) Reader {
// Implementation .... // 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) { func newPart(mr *multiReader) (bp *Part, err os.Error) {
bp = new(Part) bp = new(Part)
bp.Header = make(map[string][]string) 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 { func (bp *Part) Close() os.Error {
io.Copy(&devNull, bp) io.Copy(ioutil.Discard, bp)
return nil return nil
} }