-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreader.go
34 lines (29 loc) · 818 Bytes
/
reader.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
34
package main
import (
"bytes"
"io"
"net"
)
// Read wrapper is a wrapper around an io.ReadWriter
// Writing to this, writes to the underlying ReadWriter ,
// while reading from it, reads from the buffer first, before reading the underlying io.ReadWriter
// once the buffer is empty it can't be read from again. See io.MultiReader
type ConnectionReader struct {
net.Conn
multiReader io.Reader
}
func NewConnectionReader(rw net.Conn, prepend ...[]byte) *ConnectionReader {
cw := ConnectionReader{
Conn: rw,
}
readers := make([]io.Reader, len(prepend)+1)
for i, item := range prepend {
readers[i] = bytes.NewBuffer(item)
}
readers[len(readers)-1] = rw
cw.multiReader = io.MultiReader(readers...)
return &cw
}
func (cr *ConnectionReader) Read(p []byte) (int, error) {
return cr.multiReader.Read(p)
}