]> Cypherpunks repositories - gostls13.git/commitdiff
io: consolidate multi_reader and multi_writer into a single file, multi.go
authorRob Pike <r@golang.org>
Mon, 2 Aug 2010 22:04:33 +0000 (08:04 +1000)
committerRob Pike <r@golang.org>
Mon, 2 Aug 2010 22:04:33 +0000 (08:04 +1000)
R=rsc
CC=golang-dev
https://golang.org/cl/1860046

src/pkg/io/Makefile
src/pkg/io/multi.go [moved from src/pkg/io/multi_reader.go with 65% similarity]
src/pkg/io/multi_test.go [moved from src/pkg/io/multi_reader_test.go with 61% similarity]
src/pkg/io/multi_writer.go [deleted file]
src/pkg/io/multi_writer_test.go [deleted file]

index ad2e4cec44849425e6e3fedb2c770eae7a2ab72d..837888d5c1906a3a746108f7468e896d6fc7320a 100644 (file)
@@ -7,8 +7,7 @@ include ../../Make.$(GOARCH)
 TARG=io
 GOFILES=\
        io.go\
-       multi_reader.go\
-       multi_writer.go\
+       multi.go\
        pipe.go\
 
 include ../../Make.pkg
similarity index 65%
rename from src/pkg/io/multi_reader.go
rename to src/pkg/io/multi.go
index b6fa5dd058f27543c35947bf0afc9d2cd10bf355..88e4f1b769886620b2a0eb3c3920fb784fd4b210 100644 (file)
@@ -34,3 +34,27 @@ func (mr *multiReader) Read(p []byte) (n int, err os.Error) {
 func MultiReader(readers ...Reader) Reader {
        return &multiReader{readers}
 }
+
+type multiWriter struct {
+       writers []Writer
+}
+
+func (t *multiWriter) Write(p []byte) (n int, err os.Error) {
+       for _, w := range t.writers {
+               n, err = w.Write(p)
+               if err != nil {
+                       return
+               }
+               if n != len(p) {
+                       err = ErrShortWrite
+                       return
+               }
+       }
+       return len(p), nil
+}
+
+// MultiWriter creates a writer that duplicates its writes to all the
+// provided writers, similar to the Unix tee(1) command.
+func MultiWriter(writers ...Writer) Writer {
+       return &multiWriter{writers}
+}
similarity index 61%
rename from src/pkg/io/multi_reader_test.go
rename to src/pkg/io/multi_test.go
index 7d4639628bd913ae6479aa9338c32925bee11913..f5d2fadfbb3ba6c547499de3390c8b2b00cd0ccf 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2010 The Go Authors. All rights reserved.
+// Copyright 2010 The Go Authors.  All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
@@ -6,6 +6,9 @@ package io_test
 
 import (
        . "io"
+       "bytes"
+       "crypto/sha1"
+       "fmt"
        "os"
        "strings"
        "testing"
@@ -56,3 +59,30 @@ func TestMultiReader(t *testing.T) {
                expectRead(5, "foo ", nil)
        })
 }
+
+func TestMultiWriter(t *testing.T) {
+       sha1 := sha1.New()
+       sink := new(bytes.Buffer)
+       mw := MultiWriter(sha1, sink)
+
+       sourceString := "My input text."
+       source := strings.NewReader(sourceString)
+       written, err := Copy(mw, source)
+
+       if written != int64(len(sourceString)) {
+               t.Errorf("short write of %d, not %d", written, len(sourceString))
+       }
+
+       if err != nil {
+               t.Errorf("unexpected error: %v", err)
+       }
+
+       sha1hex := fmt.Sprintf("%x", sha1.Sum())
+       if sha1hex != "01cb303fa8c30a64123067c5aa6284ba7ec2d31b" {
+               t.Error("incorrect sha1 value")
+       }
+
+       if sink.String() != sourceString {
+               t.Error("expected %q; got %q", sourceString, sink.String())
+       }
+}
diff --git a/src/pkg/io/multi_writer.go b/src/pkg/io/multi_writer.go
deleted file mode 100644 (file)
index 5825288..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright 2010 The Go Authors.  All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package io
-
-import "os"
-
-type multiWriter struct {
-       writers []Writer
-}
-
-func (t *multiWriter) Write(p []byte) (n int, err os.Error) {
-       for _, w := range t.writers {
-               n, err = w.Write(p)
-               if err != nil {
-                       return
-               }
-               if n != len(p) {
-                       err = ErrShortWrite
-                       return
-               }
-       }
-       return len(p), nil
-}
-
-// MultiWriter creates a writer that duplicates its writes to all the
-// provided writers, similar to the Unix tee(1) command.
-func MultiWriter(writers ...Writer) Writer {
-       return &multiWriter{writers}
-}
diff --git a/src/pkg/io/multi_writer_test.go b/src/pkg/io/multi_writer_test.go
deleted file mode 100644 (file)
index 251a477..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-// Copyright 2010 The Go Authors.  All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package io_test
-
-import (
-       . "io"
-       "bytes"
-       "crypto/sha1"
-       "fmt"
-       "strings"
-       "testing"
-)
-
-func TestMultiWriter(t *testing.T) {
-       sha1 := sha1.New()
-       sink := new(bytes.Buffer)
-       mw := MultiWriter(sha1, sink)
-
-       sourceString := "My input text."
-       source := strings.NewReader(sourceString)
-       written, err := Copy(mw, source)
-
-       if written != int64(len(sourceString)) {
-               t.Errorf("short write of %d, not %d", written, len(sourceString))
-       }
-
-       if err != nil {
-               t.Errorf("unexpected error: %v", err)
-       }
-
-       sha1hex := fmt.Sprintf("%x", sha1.Sum())
-       if sha1hex != "01cb303fa8c30a64123067c5aa6284ba7ec2d31b" {
-               t.Error("incorrect sha1 value")
-       }
-
-       if sink.String() != sourceString {
-               t.Error("expected %q; got %q", sourceString, sink.String())
-       }
-}