Source file
src/io/multi.go
1
2
3
4
5 package io
6
7 type eofReader struct{}
8
9 func (eofReader) Read([]byte) (int, error) {
10 return 0, EOF
11 }
12
13 type multiReader struct {
14 readers []Reader
15 }
16
17 func (mr *multiReader) Read(p []byte) (n int, err error) {
18 for len(mr.readers) > 0 {
19
20 if len(mr.readers) == 1 {
21 if r, ok := mr.readers[0].(*multiReader); ok {
22 mr.readers = r.readers
23 continue
24 }
25 }
26 n, err = mr.readers[0].Read(p)
27 if err == EOF {
28
29
30 mr.readers[0] = eofReader{}
31 mr.readers = mr.readers[1:]
32 }
33 if n > 0 || err != EOF {
34 if err == EOF && len(mr.readers) > 0 {
35
36 err = nil
37 }
38 return
39 }
40 }
41 return 0, EOF
42 }
43
44
45
46
47
48 func MultiReader(readers ...Reader) Reader {
49 r := make([]Reader, len(readers))
50 copy(r, readers)
51 return &multiReader{r}
52 }
53
54 type multiWriter struct {
55 writers []Writer
56 }
57
58 func (t *multiWriter) Write(p []byte) (n int, err error) {
59 for _, w := range t.writers {
60 n, err = w.Write(p)
61 if err != nil {
62 return
63 }
64 if n != len(p) {
65 err = ErrShortWrite
66 return
67 }
68 }
69 return len(p), nil
70 }
71
72 var _ StringWriter = (*multiWriter)(nil)
73
74 func (t *multiWriter) WriteString(s string) (n int, err error) {
75 var p []byte
76 for _, w := range t.writers {
77 if sw, ok := w.(StringWriter); ok {
78 n, err = sw.WriteString(s)
79 } else {
80 if p == nil {
81 p = []byte(s)
82 }
83 n, err = w.Write(p)
84 }
85 if err != nil {
86 return
87 }
88 if n != len(s) {
89 err = ErrShortWrite
90 return
91 }
92 }
93 return len(s), nil
94 }
95
96
97
98
99
100
101
102 func MultiWriter(writers ...Writer) Writer {
103 allWriters := make([]Writer, 0, len(writers))
104 for _, w := range writers {
105 if mw, ok := w.(*multiWriter); ok {
106 allWriters = append(allWriters, mw.writers...)
107 } else {
108 allWriters = append(allWriters, w)
109 }
110 }
111 return &multiWriter{allWriters}
112 }
113
View as plain text