-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tee.go
33 lines (28 loc) · 850 Bytes
/
tee.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2020 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
package xmlstream
import (
"encoding/xml"
)
// TeeReader returns a Reader that writes to w what it reads from r.
// All reads from r performed through it are matched with
// corresponding writes to w. There is no internal buffering -
// the write must complete before the read completes.
// Any error encountered while writing is reported as a read error.
func TeeReader(r xml.TokenReader, w TokenWriter) xml.TokenReader {
return teeReader{r, w}
}
type teeReader struct {
r xml.TokenReader
w TokenWriter
}
func (t teeReader) Token() (xml.Token, error) {
tok, err := t.r.Token()
if tok != nil {
if err := t.w.EncodeToken(tok); err != nil {
return tok, err
}
}
return tok, err
}