Browse code

Add better comments to utils/stdcopy.go.

Jérôme Petazzoni authored on 2013/10/17 02:58:53
Showing 1 changed files
... ...
@@ -37,8 +37,12 @@ func (w *StdWriter) Write(buf []byte) (n int, err error) {
37 37
 	return n - StdWriterPrefixLen, err
38 38
 }
39 39
 
40
-// NewStdWriter instanciate a new Writer based on the given type `t`.
41
-// the utils package contains the valid parametres for `t`:
40
+// NewStdWriter instanciates a new Writer.
41
+// Everything written to it will be encapsulated using a custom format,
42
+// and written to the underlying `w` stream.
43
+// This allows multiple write streams (e.g. stdout and stderr) to be muxed into a single connection.
44
+// `t` indicates the id of the stream to encapsulate.
45
+// It can be utils.Stdin, utils.Stdout, utils.Stderr.
42 46
 func NewStdWriter(w io.Writer, t StdType) *StdWriter {
43 47
 	if len(t) != StdWriterPrefixLen {
44 48
 		return nil
... ...
@@ -55,16 +59,14 @@ var ErrInvalidStdHeader = errors.New("Unrecognized input header")
55 55
 
56 56
 // StdCopy is a modified version of io.Copy.
57 57
 //
58
-// StdCopy copies from src to dstout or dsterr until either EOF is reached
59
-// on src or an error occurs.  It returns the number of bytes
60
-// copied and the first error encountered while copying, if any.
58
+// StdCopy will demultiplex `src`, assuming that it contains two streams,
59
+// previously multiplexed together using a StdWriter instance.
60
+// As it reads from `src`, StdCopy will write to `dstout` and `dsterr`.
61 61
 //
62
-// A successful Copy returns err == nil, not err == EOF.
63
-// Because Copy is defined to read from src until EOF, it does
64
-// not treat an EOF from Read as an error to be reported.
62
+// StdCopy will read until it hits EOF on `src`. It will then return a nil error.
63
+// In other words: if `err` is non nil, it indicates a real underlying error.
65 64
 //
66
-// The source needs to be writter via StdWriter, dstout or dsterr is selected
67
-// based on the prefix added by StdWriter
65
+// `written` will hold the total number of bytes written to `dstout` and `dsterr`.
68 66
 func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error) {
69 67
 	var (
70 68
 		buf       = make([]byte, 32*1024+StdWriterPrefixLen+1)