2012-03-27 23:13:14 +00:00
|
|
|
// 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
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
type eofReader struct{}
|
|
|
|
|
|
|
|
func (eofReader) Read([]byte) (int, error) {
|
|
|
|
return 0, EOF
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
type multiReader struct {
|
|
|
|
readers []Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mr *multiReader) Read(p []byte) (n int, err error) {
|
|
|
|
for len(mr.readers) > 0 {
|
2017-10-07 00:16:47 +00:00
|
|
|
// Optimization to flatten nested multiReaders (Issue 13558).
|
|
|
|
if len(mr.readers) == 1 {
|
|
|
|
if r, ok := mr.readers[0].(*multiReader); ok {
|
|
|
|
mr.readers = r.readers
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
n, err = mr.readers[0].Read(p)
|
2017-10-07 00:16:47 +00:00
|
|
|
if err == EOF {
|
|
|
|
// Use eofReader instead of nil to avoid nil panic
|
|
|
|
// after performing flatten (Issue 18232).
|
|
|
|
mr.readers[0] = eofReader{} // permit earlier GC
|
|
|
|
mr.readers = mr.readers[1:]
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
if n > 0 || err != EOF {
|
2017-10-07 00:16:47 +00:00
|
|
|
if err == EOF && len(mr.readers) > 0 {
|
|
|
|
// Don't return EOF yet. More readers remain.
|
2012-03-27 23:13:14 +00:00
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiReader returns a Reader that's the logical concatenation of
|
2017-10-07 00:16:47 +00:00
|
|
|
// the provided input readers. They're read sequentially. Once all
|
2015-08-28 15:33:40 +00:00
|
|
|
// inputs have returned EOF, Read will return EOF. If any of the readers
|
|
|
|
// return a non-nil, non-EOF error, Read will return that error.
|
2012-03-27 23:13:14 +00:00
|
|
|
func MultiReader(readers ...Reader) Reader {
|
2015-08-28 15:33:40 +00:00
|
|
|
r := make([]Reader, len(readers))
|
|
|
|
copy(r, readers)
|
|
|
|
return &multiReader{r}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type multiWriter struct {
|
|
|
|
writers []Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *multiWriter) Write(p []byte) (n int, err 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
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
var _ StringWriter = (*multiWriter)(nil)
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
func (t *multiWriter) WriteString(s string) (n int, err error) {
|
|
|
|
var p []byte // lazily initialized if/when needed
|
|
|
|
for _, w := range t.writers {
|
2019-06-02 15:48:37 +00:00
|
|
|
if sw, ok := w.(StringWriter); ok {
|
2017-04-10 11:32:00 +00:00
|
|
|
n, err = sw.WriteString(s)
|
|
|
|
} else {
|
|
|
|
if p == nil {
|
|
|
|
p = []byte(s)
|
|
|
|
}
|
|
|
|
n, err = w.Write(p)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != len(s) {
|
|
|
|
err = ErrShortWrite
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(s), nil
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
// MultiWriter creates a writer that duplicates its writes to all the
|
|
|
|
// provided writers, similar to the Unix tee(1) command.
|
2018-12-28 15:30:48 +00:00
|
|
|
//
|
|
|
|
// Each write is written to each listed writer, one at a time.
|
|
|
|
// If a listed writer returns an error, that overall write operation
|
|
|
|
// stops and returns the error; it does not continue down the list.
|
2012-03-27 23:13:14 +00:00
|
|
|
func MultiWriter(writers ...Writer) Writer {
|
2018-12-28 15:30:48 +00:00
|
|
|
allWriters := make([]Writer, 0, len(writers))
|
|
|
|
for _, w := range writers {
|
|
|
|
if mw, ok := w.(*multiWriter); ok {
|
|
|
|
allWriters = append(allWriters, mw.writers...)
|
|
|
|
} else {
|
|
|
|
allWriters = append(allWriters, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &multiWriter{allWriters}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|