WriteTo writes data to w until the buffer is drained or an error occurs. The return value n is the number of bytes written; it always fits into an int, but it is int64 to match the io.WriterTo interface. Any error encountered during the write is also returned.
(w io.Writer)
| 236 | // int, but it is int64 to match the io.WriterTo interface. Any error |
| 237 | // encountered during the write is also returned. |
| 238 | func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) { |
| 239 | if b.off < len(b.buf) { |
| 240 | nBytes := b.Len() |
| 241 | m, e := w.Write(b.buf[b.off:]) |
| 242 | if m > nBytes { |
| 243 | panic("bytes.Buffer.WriteTo: invalid Write count") |
| 244 | } |
| 245 | b.off += m |
| 246 | n = int64(m) |
| 247 | if e != nil { |
| 248 | return n, e |
| 249 | } |
| 250 | // all bytes should have been written, by definition of |
| 251 | // Write method in io.Writer |
| 252 | if m != nBytes { |
| 253 | return n, io.ErrShortWrite |
| 254 | } |
| 255 | } |
| 256 | // Buffer is now empty; reset. |
| 257 | b.Truncate(0) |
| 258 | return |
| 259 | } |
| 260 | |
| 261 | // WriteByte appends the byte c to the buffer, growing the buffer as needed. |
| 262 | // The returned error is always nil, but is included to match bufio.Writer's |