mirror of
https://github.com/golang/go
synced 2024-11-21 21:14:47 -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:
parent
ec3fe2a5b6
commit
9d12307a12
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user