Debian Codegraph @

nio.go

raw symbols SCIP
1 // Package nio provides a few buffered io primitives.
2 package nio
3
4 import "io"
5
6 // Buffer is used to store bytes.
7 type Buffer interface {
8 // Len returns how many bytes are buffered
9 Len() int64
10
11 // Cap returns how many bytes can in the buffer at a time
12 Cap() int64
13
14 // ReadWriter writes are stored in the buffer, reads return the stored data
15 io.ReadWriter
16 }
17
18 // Pipe creates a buffered pipe.
19 // It can be used to connect code expecting an io.Reader with code expecting an io.Writer.
20 // Reads on one end read from the supplied Buffer. Writes write to the supplied Buffer.
21 // It is safe to call Read and Write in parallel with each other or with Close.
22 // Close will complete once pending I/O is done, and may cancel blocking Read/Writes.
23 // Buffered data will still be available to Read after the Writer has been closed.
24 // Parallel calls to Read, and parallel calls to Write are also safe :
25 // the individual calls will be gated sequentially.
26 func Pipe(buf Buffer) (r *PipeReader, w *PipeWriter) {
27 p := newBufferedPipe(buf)
28 r = &PipeReader{bufpipe: p}
29 w = &PipeWriter{bufpipe: p}
30 return r, w
31 }
32
33 // Copy copies from src to buf, and from buf to dst in parallel until
34 // either EOF is reached on src or an error occurs. It returns the number of bytes
35 // copied to dst and the first error encountered while copying, if any.
36 // EOF is not considered to be an error. If src implements WriterTo, it is used to
37 // write to the supplied Buffer. If dst implements ReaderFrom, it is used to read from
38 // the supplied Buffer.
39 func Copy(dst io.Writer, src io.Reader, buf Buffer) (n int64, err error) {
40 return io.Copy(dst, NewReader(src, buf))
41 }
42
43 // NewReader reads from the buffer which is concurrently filled with data from the passed src.
44 func NewReader(src io.Reader, buf Buffer) io.ReadCloser {
45 r, w := Pipe(buf)
46
47 go func() {
48 _, err := io.Copy(w, src)
49 w.CloseWithError(err)
50 }()
51
52 return r
53 }