-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteer.go
More file actions
41 lines (34 loc) · 931 Bytes
/
teer.go
File metadata and controls
41 lines (34 loc) · 931 Bytes
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
34
35
36
37
38
39
40
41
// teer.go - wrapper around io.ReadWrite(Close)r that copies data to itself.
//
// To the extent possible under law, Ivan Markin waived all copyright
// and related or neighboring rights to teer, using the creative
// commons "cc0" public domain dedication. See LICENSE or
// <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
package teer
import (
"errors"
"io"
)
type TeeReadWriter struct {
rw io.ReadWriter
tr io.Reader
}
func New(rw io.ReadWriter) *TeeReadWriter {
return &TeeReadWriter{
rw: rw,
tr: io.TeeReader(rw, rw),
}
}
func (t *TeeReadWriter) Read(b []byte) (int, error) {
return t.tr.Read(b)
}
func (t *TeeReadWriter) Write(b []byte) (int, error) {
return t.rw.Write(b)
}
func (t *TeeReadWriter) Close() error {
switch t.rw.(type) {
case io.ReadWriteCloser:
return t.rw.(io.ReadWriteCloser).Close()
}
return errors.New("unable to close: parent does not implement Close()")
}