Read implements the standard Read interface: it reads data from the pipe, reading from the internal buffer, otherwise blocking until a writer arrives or the write end is closed. If the write end is closed with an error, that error is returned as err; otherwise err is io.EOF.
(data []byte)
| 130 | // or the write end is closed. If the write end is closed with an error, that |
| 131 | // error is returned as err; otherwise err is io.EOF. |
| 132 | func (r *pipeReader) Read(data []byte) (n int, err error) { |
| 133 | r.cond.L.Lock() |
| 134 | defer r.cond.L.Unlock() |
| 135 | |
| 136 | if r.buf == nil { |
| 137 | r.buf = r.bufPool.Get() |
| 138 | } |
| 139 | |
| 140 | for { |
| 141 | n, err = r.buf.Read(data) |
| 142 | // If not closed and no read, wait for writing. |
| 143 | if err == io.EOF && r.rerr == nil && n == 0 { |
| 144 | r.cond.Wait() // Wait for data to be written |
| 145 | continue |
| 146 | } |
| 147 | break |
| 148 | } |
| 149 | if err == io.EOF { |
| 150 | // Put buffer back to pool |
| 151 | r.bufPool.Put(r.buf) |
| 152 | r.buf = nil |
| 153 | return n, r.rerr |
| 154 | } |
| 155 | return n, err |
| 156 | } |
| 157 | |
| 158 | // Close closes the reader; subsequent writes from the write half of the pipe |
| 159 | // will return error ErrClosedPipe. |