| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer |
| 6 |
// object, creating another object (Reader or Writer) that also implements |
| 7 |
// the interface but provides buffering and some help for textual I/O. |
| 8 |
package bufio |
| 9 |
|
| 10 |
import ( |
| 11 |
"bytes" |
| 12 |
"errors" |
| 13 |
"io" |
| 14 |
"strings" |
| 15 |
"unicode/utf8" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
defaultBufSize = 4096 |
| 20 |
) |
| 21 |
|
| 22 |
var ( |
| 23 |
ErrInvalidUnreadByte = errors.New("bufio: invalid use of UnreadByte") |
| 24 |
ErrInvalidUnreadRune = errors.New("bufio: invalid use of UnreadRune") |
| 25 |
ErrBufferFull = errors.New("bufio: buffer full") |
| 26 |
ErrNegativeCount = errors.New("bufio: negative count") |
| 27 |
) |
| 28 |
|
| 29 |
// Buffered input. |
| 30 |
|
| 31 |
// Reader implements buffering for an io.Reader object. |
| 32 |
// A new Reader is created by calling [NewReader] or [NewReaderSize]; |
| 33 |
// alternatively the zero value of a Reader may be used after calling [Reset] |
| 34 |
// on it. |
| 35 |
type Reader struct { |
| 36 |
buf []byte |
| 37 |
rd io.Reader // reader provided by the client |
| 38 |
r, w int // buf read and write positions |
| 39 |
err error |
| 40 |
lastByte int // last byte read for UnreadByte; -1 means invalid |
| 41 |
lastRuneSize int // size of last rune read for UnreadRune; -1 means invalid |
| 42 |
} |
| 43 |
|
| 44 |
const minReadBufferSize = 16 |
| 45 |
const maxConsecutiveEmptyReads = 100 |
| 46 |
|
| 47 |
// NewReaderSize returns a new [Reader] whose buffer has at least the specified |
| 48 |
// size. If the argument io.Reader is already a [Reader] with large enough |
| 49 |
// size, it returns the underlying [Reader]. |
| 50 |
func NewReaderSize(rd io.Reader, size int) *Reader { |
| 51 |
// Is it already a Reader? |
| 52 |
b, ok := rd.(*Reader) |
| 53 |
if ok && len(b.buf) >= size { |
| 54 |
return b |
| 55 |
} |
| 56 |
r := new(Reader) |
| 57 |
r.reset(make([]byte, max(size, minReadBufferSize)), rd) |
| 58 |
return r |
| 59 |
} |
| 60 |
|
| 61 |
// NewReader returns a new [Reader] whose buffer has the default size. |
| 62 |
func NewReader(rd io.Reader) *Reader { |
| 63 |
return NewReaderSize(rd, defaultBufSize) |
| 64 |
} |
| 65 |
|
| 66 |
// Size returns the size of the underlying buffer in bytes. |
| 67 |
func (b *Reader) Size() int { return len(b.buf) } |
| 68 |
|
| 69 |
// Reset discards any buffered data, resets all state, and switches |
| 70 |
// the buffered reader to read from r. |
| 71 |
// Calling Reset on the zero value of [Reader] initializes the internal buffer |
| 72 |
// to the default size. |
| 73 |
// Calling b.Reset(b) (that is, resetting a [Reader] to itself) does nothing. |
| 74 |
func (b *Reader) Reset(r io.Reader) { |
| 75 |
// If a Reader r is passed to NewReader, NewReader will return r. |
| 76 |
// Different layers of code may do that, and then later pass r |
| 77 |
// to Reset. Avoid infinite recursion in that case. |
| 78 |
if b == r { |
| 79 |
return |
| 80 |
} |
| 81 |
if b.buf == nil { |
| 82 |
b.buf = make([]byte, defaultBufSize) |
| 83 |
} |
| 84 |
b.reset(b.buf, r) |
| 85 |
} |
| 86 |
|
| 87 |
func (b *Reader) reset(buf []byte, r io.Reader) { |
| 88 |
*b = Reader{ |
| 89 |
buf: buf, |
| 90 |
rd: r, |
| 91 |
lastByte: -1, |
| 92 |
lastRuneSize: -1, |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
var errNegativeRead = errors.New("bufio: reader returned negative count from Read") |
| 97 |
|
| 98 |
// fill reads a new chunk into the buffer. |
| 99 |
func (b *Reader) fill() { |
| 100 |
// Slide existing data to beginning. |
| 101 |
if b.r > 0 { |
| 102 |
copy(b.buf, b.buf[b.r:b.w]) |
| 103 |
b.w -= b.r |
| 104 |
b.r = 0 |
| 105 |
} |
| 106 |
|
| 107 |
if b.w >= len(b.buf) { |
| 108 |
panic("bufio: tried to fill full buffer") |
| 109 |
} |
| 110 |
|
| 111 |
// Read new data: try a limited number of times. |
| 112 |
for i := maxConsecutiveEmptyReads; i > 0; i-- { |
| 113 |
n, err := b.rd.Read(b.buf[b.w:]) |
| 114 |
if n < 0 { |
| 115 |
panic(errNegativeRead) |
| 116 |
} |
| 117 |
b.w += n |
| 118 |
if err != nil { |
| 119 |
b.err = err |
| 120 |
return |
| 121 |
} |
| 122 |
if n > 0 { |
| 123 |
return |
| 124 |
} |
| 125 |
} |
| 126 |
b.err = io.ErrNoProgress |
| 127 |
} |
| 128 |
|
| 129 |
func (b *Reader) readErr() error { |
| 130 |
err := b.err |
| 131 |
b.err = nil |
| 132 |
return err |
| 133 |
} |
| 134 |
|
| 135 |
// Peek returns the next n bytes without advancing the reader. The bytes stop |
| 136 |
// being valid at the next read call. If necessary, Peek will read more bytes |
| 137 |
// into the buffer in order to make n bytes available. If Peek returns fewer |
| 138 |
// than n bytes, it also returns an error explaining why the read is short. |
| 139 |
// The error is [ErrBufferFull] if n is larger than b's buffer size. |
| 140 |
// |
| 141 |
// Calling Peek prevents a [Reader.UnreadByte] or [Reader.UnreadRune] call from succeeding |
| 142 |
// until the next read operation. |
| 143 |
func (b *Reader) Peek(n int) ([]byte, error) { |
| 144 |
if n < 0 { |
| 145 |
return nil, ErrNegativeCount |
| 146 |
} |
| 147 |
|
| 148 |
b.lastByte = -1 |
| 149 |
b.lastRuneSize = -1 |
| 150 |
|
| 151 |
for b.w-b.r < n && b.w-b.r < len(b.buf) && b.err == nil { |
| 152 |
b.fill() // b.w-b.r < len(b.buf) => buffer is not full |
| 153 |
} |
| 154 |
|
| 155 |
if n > len(b.buf) { |
| 156 |
return b.buf[b.r:b.w], ErrBufferFull |
| 157 |
} |
| 158 |
|
| 159 |
// 0 <= n <= len(b.buf) |
| 160 |
var err error |
| 161 |
if avail := b.w - b.r; avail < n { |
| 162 |
// not enough data in buffer |
| 163 |
n = avail |
| 164 |
err = b.readErr() |
| 165 |
if err == nil { |
| 166 |
err = ErrBufferFull |
| 167 |
} |
| 168 |
} |
| 169 |
return b.buf[b.r : b.r+n], err |
| 170 |
} |
| 171 |
|
| 172 |
// Discard skips the next n bytes, returning the number of bytes discarded. |
| 173 |
// |
| 174 |
// If Discard skips fewer than n bytes, it also returns an error. |
| 175 |
// If 0 <= n <= b.Buffered(), Discard is guaranteed to succeed without |
| 176 |
// reading from the underlying io.Reader. |
| 177 |
func (b *Reader) Discard(n int) (discarded int, err error) { |
| 178 |
if n < 0 { |
| 179 |
return 0, ErrNegativeCount |
| 180 |
} |
| 181 |
if n == 0 { |
| 182 |
return |
| 183 |
} |
| 184 |
|
| 185 |
b.lastByte = -1 |
| 186 |
b.lastRuneSize = -1 |
| 187 |
|
| 188 |
remain := n |
| 189 |
for { |
| 190 |
skip := b.Buffered() |
| 191 |
if skip == 0 { |
| 192 |
b.fill() |
| 193 |
skip = b.Buffered() |
| 194 |
} |
| 195 |
if skip > remain { |
| 196 |
skip = remain |
| 197 |
} |
| 198 |
b.r += skip |
| 199 |
remain -= skip |
| 200 |
if remain == 0 { |
| 201 |
return n, nil |
| 202 |
} |
| 203 |
if b.err != nil { |
| 204 |
return n - remain, b.readErr() |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
// Read reads data into p. |
| 210 |
// It returns the number of bytes read into p. |
| 211 |
// The bytes are taken from at most one Read on the underlying [Reader], |
| 212 |
// hence n may be less than len(p). |
| 213 |
// To read exactly len(p) bytes, use io.ReadFull(b, p). |
| 214 |
// If the underlying [Reader] can return a non-zero count with io.EOF, |
| 215 |
// then this Read method can do so as well; see the [io.Reader] docs. |
| 216 |
func (b *Reader) Read(p []byte) (n int, err error) { |
| 217 |
n = len(p) |
| 218 |
if n == 0 { |
| 219 |
if b.Buffered() > 0 { |
| 220 |
return 0, nil |
| 221 |
} |
| 222 |
return 0, b.readErr() |
| 223 |
} |
| 224 |
if b.r == b.w { |
| 225 |
if b.err != nil { |
| 226 |
return 0, b.readErr() |
| 227 |
} |
| 228 |
if len(p) >= len(b.buf) { |
| 229 |
// Large read, empty buffer. |
| 230 |
// Read directly into p to avoid copy. |
| 231 |
n, b.err = b.rd.Read(p) |
| 232 |
if n < 0 { |
| 233 |
panic(errNegativeRead) |
| 234 |
} |
| 235 |
if n > 0 { |
| 236 |
b.lastByte = int(p[n-1]) |
| 237 |
b.lastRuneSize = -1 |
| 238 |
} |
| 239 |
return n, b.readErr() |
| 240 |
} |
| 241 |
// One read. |
| 242 |
// Do not use b.fill, which will loop. |
| 243 |
b.r = 0 |
| 244 |
b.w = 0 |
| 245 |
n, b.err = b.rd.Read(b.buf) |
| 246 |
if n < 0 { |
| 247 |
panic(errNegativeRead) |
| 248 |
} |
| 249 |
if n == 0 { |
| 250 |
return 0, b.readErr() |
| 251 |
} |
| 252 |
b.w += n |
| 253 |
} |
| 254 |
|
| 255 |
// copy as much as we can |
| 256 |
// Note: if the slice panics here, it is probably because |
| 257 |
// the underlying reader returned a bad count. See issue 49795. |
| 258 |
n = copy(p, b.buf[b.r:b.w]) |
| 259 |
b.r += n |
| 260 |
b.lastByte = int(b.buf[b.r-1]) |
| 261 |
b.lastRuneSize = -1 |
| 262 |
return n, nil |
| 263 |
} |
| 264 |
|
| 265 |
// ReadByte reads and returns a single byte. |
| 266 |
// If no byte is available, returns an error. |
| 267 |
func (b *Reader) ReadByte() (byte, error) { |
| 268 |
b.lastRuneSize = -1 |
| 269 |
for b.r == b.w { |
| 270 |
if b.err != nil { |
| 271 |
return 0, b.readErr() |
| 272 |
} |
| 273 |
b.fill() // buffer is empty |
| 274 |
} |
| 275 |
c := b.buf[b.r] |
| 276 |
b.r++ |
| 277 |
b.lastByte = int(c) |
| 278 |
return c, nil |
| 279 |
} |
| 280 |
|
| 281 |
// UnreadByte unreads the last byte. Only the most recently read byte can be unread. |
| 282 |
// |
| 283 |
// UnreadByte returns an error if the most recent method called on the |
| 284 |
// [Reader] was not a read operation. Notably, [Reader.Peek], [Reader.Discard], and [Reader.WriteTo] are not |
| 285 |
// considered read operations. |
| 286 |
func (b *Reader) UnreadByte() error { |
| 287 |
if b.lastByte < 0 || b.r == 0 && b.w > 0 { |
| 288 |
return ErrInvalidUnreadByte |
| 289 |
} |
| 290 |
// b.r > 0 || b.w == 0 |
| 291 |
if b.r > 0 { |
| 292 |
b.r-- |
| 293 |
} else { |
| 294 |
// b.r == 0 && b.w == 0 |
| 295 |
b.w = 1 |
| 296 |
} |
| 297 |
b.buf[b.r] = byte(b.lastByte) |
| 298 |
b.lastByte = -1 |
| 299 |
b.lastRuneSize = -1 |
| 300 |
return nil |
| 301 |
} |
| 302 |
|
| 303 |
// ReadRune reads a single UTF-8 encoded Unicode character and returns the |
| 304 |
// rune and its size in bytes. If the encoded rune is invalid, it consumes one byte |
| 305 |
// and returns unicode.ReplacementChar (U+FFFD) with a size of 1. |
| 306 |
func (b *Reader) ReadRune() (r rune, size int, err error) { |
| 307 |
for b.r+utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) && b.err == nil && b.w-b.r < len(b.buf) { |
| 308 |
b.fill() // b.w-b.r < len(buf) => buffer is not full |
| 309 |
} |
| 310 |
b.lastRuneSize = -1 |
| 311 |
if b.r == b.w { |
| 312 |
return 0, 0, b.readErr() |
| 313 |
} |
| 314 |
r, size = rune(b.buf[b.r]), 1 |
| 315 |
if r >= utf8.RuneSelf { |
| 316 |
r, size = utf8.DecodeRune(b.buf[b.r:b.w]) |
| 317 |
} |
| 318 |
b.r += size |
| 319 |
b.lastByte = int(b.buf[b.r-1]) |
| 320 |
b.lastRuneSize = size |
| 321 |
return r, size, nil |
| 322 |
} |
| 323 |
|
| 324 |
// UnreadRune unreads the last rune. If the most recent method called on |
| 325 |
// the [Reader] was not a [Reader.ReadRune], [Reader.UnreadRune] returns an error. (In this |
| 326 |
// regard it is stricter than [Reader.UnreadByte], which will unread the last byte |
| 327 |
// from any read operation.) |
| 328 |
func (b *Reader) UnreadRune() error { |
| 329 |
if b.lastRuneSize < 0 || b.r < b.lastRuneSize { |
| 330 |
return ErrInvalidUnreadRune |
| 331 |
} |
| 332 |
b.r -= b.lastRuneSize |
| 333 |
b.lastByte = -1 |
| 334 |
b.lastRuneSize = -1 |
| 335 |
return nil |
| 336 |
} |
| 337 |
|
| 338 |
// Buffered returns the number of bytes that can be read from the current buffer. |
| 339 |
func (b *Reader) Buffered() int { return b.w - b.r } |
| 340 |
|
| 341 |
// ReadSlice reads until the first occurrence of delim in the input, |
| 342 |
// returning a slice pointing at the bytes in the buffer. |
| 343 |
// The bytes stop being valid at the next read. |
| 344 |
// If ReadSlice encounters an error before finding a delimiter, |
| 345 |
// it returns all the data in the buffer and the error itself (often io.EOF). |
| 346 |
// ReadSlice fails with error [ErrBufferFull] if the buffer fills without a delim. |
| 347 |
// Because the data returned from ReadSlice will be overwritten |
| 348 |
// by the next I/O operation, most clients should use |
| 349 |
// [Reader.ReadBytes] or ReadString instead. |
| 350 |
// ReadSlice returns err != nil if and only if line does not end in delim. |
| 351 |
func (b *Reader) ReadSlice(delim byte) (line []byte, err error) { |
| 352 |
s := 0 // search start index |
| 353 |
for { |
| 354 |
// Search buffer. |
| 355 |
if i := bytes.IndexByte(b.buf[b.r+s:b.w], delim); i >= 0 { |
| 356 |
i += s |
| 357 |
line = b.buf[b.r : b.r+i+1] |
| 358 |
b.r += i + 1 |
| 359 |
break |
| 360 |
} |
| 361 |
|
| 362 |
// Pending error? |
| 363 |
if b.err != nil { |
| 364 |
line = b.buf[b.r:b.w] |
| 365 |
b.r = b.w |
| 366 |
err = b.readErr() |
| 367 |
break |
| 368 |
} |
| 369 |
|
| 370 |
// Buffer full? |
| 371 |
if b.Buffered() >= len(b.buf) { |
| 372 |
b.r = b.w |
| 373 |
line = b.buf |
| 374 |
err = ErrBufferFull |
| 375 |
break |
| 376 |
} |
| 377 |
|
| 378 |
s = b.w - b.r // do not rescan area we scanned before |
| 379 |
|
| 380 |
b.fill() // buffer is not full |
| 381 |
} |
| 382 |
|
| 383 |
// Handle last byte, if any. |
| 384 |
if i := len(line) - 1; i >= 0 { |
| 385 |
b.lastByte = int(line[i]) |
| 386 |
b.lastRuneSize = -1 |
| 387 |
} |
| 388 |
|
| 389 |
return |
| 390 |
} |
| 391 |
|
| 392 |
// ReadLine is a low-level line-reading primitive. Most callers should use |
| 393 |
// [Reader.ReadBytes]('\n') or [Reader.ReadString]('\n') instead or use a [Scanner]. |
| 394 |
// |
| 395 |
// ReadLine tries to return a single line, not including the end-of-line bytes. |
| 396 |
// If the line was too long for the buffer then isPrefix is set and the |
| 397 |
// beginning of the line is returned. The rest of the line will be returned |
| 398 |
// from future calls. isPrefix will be false when returning the last fragment |
| 399 |
// of the line. The returned buffer is only valid until the next call to |
| 400 |
// ReadLine. ReadLine either returns a non-nil line or it returns an error, |
| 401 |
// never both. |
| 402 |
// |
| 403 |
// The text returned from ReadLine does not include the line end ("\r\n" or "\n"). |
| 404 |
// No indication or error is given if the input ends without a final line end. |
| 405 |
// Calling [Reader.UnreadByte] after ReadLine will always unread the last byte read |
| 406 |
// (possibly a character belonging to the line end) even if that byte is not |
| 407 |
// part of the line returned by ReadLine. |
| 408 |
func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) { |
| 409 |
line, err = b.ReadSlice('\n') |
| 410 |
if err == ErrBufferFull { |
| 411 |
// Handle the case where "\r\n" straddles the buffer. |
| 412 |
if len(line) > 0 && line[len(line)-1] == '\r' { |
| 413 |
// Put the '\r' back on buf and drop it from line. |
| 414 |
// Let the next call to ReadLine check for "\r\n". |
| 415 |
if b.r == 0 { |
| 416 |
// should be unreachable |
| 417 |
panic("bufio: tried to rewind past start of buffer") |
| 418 |
} |
| 419 |
b.r-- |
| 420 |
line = line[:len(line)-1] |
| 421 |
} |
| 422 |
return line, true, nil |
| 423 |
} |
| 424 |
|
| 425 |
if len(line) == 0 { |
| 426 |
if err != nil { |
| 427 |
line = nil |
| 428 |
} |
| 429 |
return |
| 430 |
} |
| 431 |
err = nil |
| 432 |
|
| 433 |
if line[len(line)-1] == '\n' { |
| 434 |
drop := 1 |
| 435 |
if len(line) > 1 && line[len(line)-2] == '\r' { |
| 436 |
drop = 2 |
| 437 |
} |
| 438 |
line = line[:len(line)-drop] |
| 439 |
} |
| 440 |
return |
| 441 |
} |
| 442 |
|
| 443 |
// collectFragments reads until the first occurrence of delim in the input. It |
| 444 |
// returns (slice of full buffers, remaining bytes before delim, total number |
| 445 |
// of bytes in the combined first two elements, error). |
| 446 |
// The complete result is equal to |
| 447 |
// `bytes.Join(append(fullBuffers, finalFragment), nil)`, which has a |
| 448 |
// length of `totalLen`. The result is structured in this way to allow callers |
| 449 |
// to minimize allocations and copies. |
| 450 |
func (b *Reader) collectFragments(delim byte) (fullBuffers [][]byte, finalFragment []byte, totalLen int, err error) { |
| 451 |
var frag []byte |
| 452 |
// Use ReadSlice to look for delim, accumulating full buffers. |
| 453 |
for { |
| 454 |
var e error |
| 455 |
frag, e = b.ReadSlice(delim) |
| 456 |
if e == nil { // got final fragment |
| 457 |
break |
| 458 |
} |
| 459 |
if e != ErrBufferFull { // unexpected error |
| 460 |
err = e |
| 461 |
break |
| 462 |
} |
| 463 |
|
| 464 |
// Make a copy of the buffer. |
| 465 |
buf := bytes.Clone(frag) |
| 466 |
fullBuffers = append(fullBuffers, buf) |
| 467 |
totalLen += len(buf) |
| 468 |
} |
| 469 |
|
| 470 |
totalLen += len(frag) |
| 471 |
return fullBuffers, frag, totalLen, err |
| 472 |
} |
| 473 |
|
| 474 |
// ReadBytes reads until the first occurrence of delim in the input, |
| 475 |
// returning a slice containing the data up to and including the delimiter. |
| 476 |
// If ReadBytes encounters an error before finding a delimiter, |
| 477 |
// it returns the data read before the error and the error itself (often io.EOF). |
| 478 |
// ReadBytes returns err != nil if and only if the returned data does not end in |
| 479 |
// delim. |
| 480 |
// For simple uses, a Scanner may be more convenient. |
| 481 |
func (b *Reader) ReadBytes(delim byte) ([]byte, error) { |
| 482 |
full, frag, n, err := b.collectFragments(delim) |
| 483 |
// Allocate new buffer to hold the full pieces and the fragment. |
| 484 |
buf := make([]byte, n) |
| 485 |
n = 0 |
| 486 |
// Copy full pieces and fragment in. |
| 487 |
for i := range full { |
| 488 |
n += copy(buf[n:], full[i]) |
| 489 |
} |
| 490 |
copy(buf[n:], frag) |
| 491 |
return buf, err |
| 492 |
} |
| 493 |
|
| 494 |
// ReadString reads until the first occurrence of delim in the input, |
| 495 |
// returning a string containing the data up to and including the delimiter. |
| 496 |
// If ReadString encounters an error before finding a delimiter, |
| 497 |
// it returns the data read before the error and the error itself (often io.EOF). |
| 498 |
// ReadString returns err != nil if and only if the returned data does not end in |
| 499 |
// delim. |
| 500 |
// For simple uses, a Scanner may be more convenient. |
| 501 |
func (b *Reader) ReadString(delim byte) (string, error) { |
| 502 |
full, frag, n, err := b.collectFragments(delim) |
| 503 |
// Allocate new buffer to hold the full pieces and the fragment. |
| 504 |
var buf strings.Builder |
| 505 |
buf.Grow(n) |
| 506 |
// Copy full pieces and fragment in. |
| 507 |
for _, fb := range full { |
| 508 |
buf.Write(fb) |
| 509 |
} |
| 510 |
buf.Write(frag) |
| 511 |
return buf.String(), err |
| 512 |
} |
| 513 |
|
| 514 |
// WriteTo implements io.WriterTo. |
| 515 |
// This may make multiple calls to the [Reader.Read] method of the underlying [Reader]. |
| 516 |
// If the underlying reader supports the [Reader.WriteTo] method, |
| 517 |
// this calls the underlying [Reader.WriteTo] without buffering. |
| 518 |
func (b *Reader) WriteTo(w io.Writer) (n int64, err error) { |
| 519 |
b.lastByte = -1 |
| 520 |
b.lastRuneSize = -1 |
| 521 |
|
| 522 |
if b.r < b.w { |
| 523 |
n, err = b.writeBuf(w) |
| 524 |
if err != nil { |
| 525 |
return |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
if r, ok := b.rd.(io.WriterTo); ok { |
| 530 |
m, err := r.WriteTo(w) |
| 531 |
n += m |
| 532 |
return n, err |
| 533 |
} |
| 534 |
|
| 535 |
if w, ok := w.(io.ReaderFrom); ok { |
| 536 |
m, err := w.ReadFrom(b.rd) |
| 537 |
n += m |
| 538 |
return n, err |
| 539 |
} |
| 540 |
|
| 541 |
if b.w-b.r < len(b.buf) { |
| 542 |
b.fill() // buffer not full |
| 543 |
} |
| 544 |
|
| 545 |
for b.r < b.w { |
| 546 |
// b.r < b.w => buffer is not empty |
| 547 |
m, err := b.writeBuf(w) |
| 548 |
n += m |
| 549 |
if err != nil { |
| 550 |
return n, err |
| 551 |
} |
| 552 |
b.fill() // buffer is empty |
| 553 |
} |
| 554 |
|
| 555 |
if b.err == io.EOF { |
| 556 |
b.err = nil |
| 557 |
} |
| 558 |
|
| 559 |
return n, b.readErr() |
| 560 |
} |
| 561 |
|
| 562 |
var errNegativeWrite = errors.New("bufio: writer returned negative count from Write") |
| 563 |
|
| 564 |
// writeBuf writes the [Reader]'s buffer to the writer. |
| 565 |
func (b *Reader) writeBuf(w io.Writer) (int64, error) { |
| 566 |
n, err := w.Write(b.buf[b.r:b.w]) |
| 567 |
if n < 0 { |
| 568 |
panic(errNegativeWrite) |
| 569 |
} |
| 570 |
b.r += n |
| 571 |
return int64(n), err |
| 572 |
} |
| 573 |
|
| 574 |
// buffered output |
| 575 |
|
| 576 |
// Writer implements buffering for an [io.Writer] object. |
| 577 |
// If an error occurs writing to a [Writer], no more data will be |
| 578 |
// accepted and all subsequent writes, and [Writer.Flush], will return the error. |
| 579 |
// After all data has been written, the client should call the |
| 580 |
// [Writer.Flush] method to guarantee all data has been forwarded to |
| 581 |
// the underlying [io.Writer]. |
| 582 |
type Writer struct { |
| 583 |
err error |
| 584 |
buf []byte |
| 585 |
n int |
| 586 |
wr io.Writer |
| 587 |
} |
| 588 |
|
| 589 |
// NewWriterSize returns a new [Writer] whose buffer has at least the specified |
| 590 |
// size. If the argument io.Writer is already a [Writer] with large enough |
| 591 |
// size, it returns the underlying [Writer]. |
| 592 |
func NewWriterSize(w io.Writer, size int) *Writer { |
| 593 |
// Is it already a Writer? |
| 594 |
b, ok := w.(*Writer) |
| 595 |
if ok && len(b.buf) >= size { |
| 596 |
return b |
| 597 |
} |
| 598 |
if size <= 0 { |
| 599 |
size = defaultBufSize |
| 600 |
} |
| 601 |
return &Writer{ |
| 602 |
buf: make([]byte, size), |
| 603 |
wr: w, |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
// NewWriter returns a new [Writer] whose buffer has the default size. |
| 608 |
// If the argument io.Writer is already a [Writer] with large enough buffer size, |
| 609 |
// it returns the underlying [Writer]. |
| 610 |
func NewWriter(w io.Writer) *Writer { |
| 611 |
return NewWriterSize(w, defaultBufSize) |
| 612 |
} |
| 613 |
|
| 614 |
// Size returns the size of the underlying buffer in bytes. |
| 615 |
func (b *Writer) Size() int { return len(b.buf) } |
| 616 |
|
| 617 |
// Reset discards any unflushed buffered data, clears any error, and |
| 618 |
// resets b to write its output to w. |
| 619 |
// Calling Reset on the zero value of [Writer] initializes the internal buffer |
| 620 |
// to the default size. |
| 621 |
// Calling w.Reset(w) (that is, resetting a [Writer] to itself) does nothing. |
| 622 |
func (b *Writer) Reset(w io.Writer) { |
| 623 |
// If a Writer w is passed to NewWriter, NewWriter will return w. |
| 624 |
// Different layers of code may do that, and then later pass w |
| 625 |
// to Reset. Avoid infinite recursion in that case. |
| 626 |
if b == w { |
| 627 |
return |
| 628 |
} |
| 629 |
if b.buf == nil { |
| 630 |
b.buf = make([]byte, defaultBufSize) |
| 631 |
} |
| 632 |
b.err = nil |
| 633 |
b.n = 0 |
| 634 |
b.wr = w |
| 635 |
} |
| 636 |
|
| 637 |
// Flush writes any buffered data to the underlying [io.Writer]. |
| 638 |
func (b *Writer) Flush() error { |
| 639 |
if b.err != nil { |
| 640 |
return b.err |
| 641 |
} |
| 642 |
if b.n == 0 { |
| 643 |
return nil |
| 644 |
} |
| 645 |
n, err := b.wr.Write(b.buf[0:b.n]) |
| 646 |
if n < b.n && err == nil { |
| 647 |
err = io.ErrShortWrite |
| 648 |
} |
| 649 |
if err != nil { |
| 650 |
if n > 0 && n < b.n { |
| 651 |
copy(b.buf[0:b.n-n], b.buf[n:b.n]) |
| 652 |
} |
| 653 |
b.n -= n |
| 654 |
b.err = err |
| 655 |
return err |
| 656 |
} |
| 657 |
b.n = 0 |
| 658 |
return nil |
| 659 |
} |
| 660 |
|
| 661 |
// Available returns how many bytes are unused in the buffer. |
| 662 |
func (b *Writer) Available() int { return len(b.buf) - b.n } |
| 663 |
|
| 664 |
// AvailableBuffer returns an empty buffer with b.Available() capacity. |
| 665 |
// This buffer is intended to be appended to and |
| 666 |
// passed to an immediately succeeding [Writer.Write] call. |
| 667 |
// The buffer is only valid until the next write operation on b. |
| 668 |
func (b *Writer) AvailableBuffer() []byte { |
| 669 |
return b.buf[b.n:][:0] |
| 670 |
} |
| 671 |
|
| 672 |
// Buffered returns the number of bytes that have been written into the current buffer. |
| 673 |
func (b *Writer) Buffered() int { return b.n } |
| 674 |
|
| 675 |
// Write writes the contents of p into the buffer. |
| 676 |
// It returns the number of bytes written. |
| 677 |
// If nn < len(p), it also returns an error explaining |
| 678 |
// why the write is short. |
| 679 |
func (b *Writer) Write(p []byte) (nn int, err error) { |
| 680 |
for len(p) > b.Available() && b.err == nil { |
| 681 |
var n int |
| 682 |
if b.Buffered() == 0 { |
| 683 |
// Large write, empty buffer. |
| 684 |
// Write directly from p to avoid copy. |
| 685 |
n, b.err = b.wr.Write(p) |
| 686 |
} else { |
| 687 |
n = copy(b.buf[b.n:], p) |
| 688 |
b.n += n |
| 689 |
b.Flush() |
| 690 |
} |
| 691 |
nn += n |
| 692 |
p = p[n:] |
| 693 |
} |
| 694 |
if b.err != nil { |
| 695 |
return nn, b.err |
| 696 |
} |
| 697 |
n := copy(b.buf[b.n:], p) |
| 698 |
b.n += n |
| 699 |
nn += n |
| 700 |
return nn, nil |
| 701 |
} |
| 702 |
|
| 703 |
// WriteByte writes a single byte. |
| 704 |
func (b *Writer) WriteByte(c byte) error { |
| 705 |
if b.err != nil { |
| 706 |
return b.err |
| 707 |
} |
| 708 |
if b.Available() <= 0 && b.Flush() != nil { |
| 709 |
return b.err |
| 710 |
} |
| 711 |
b.buf[b.n] = c |
| 712 |
b.n++ |
| 713 |
return nil |
| 714 |
} |
| 715 |
|
| 716 |
// WriteRune writes a single Unicode code point, returning |
| 717 |
// the number of bytes written and any error. |
| 718 |
func (b *Writer) WriteRune(r rune) (size int, err error) { |
| 719 |
// Compare as uint32 to correctly handle negative runes. |
| 720 |
if uint32(r) < utf8.RuneSelf { |
| 721 |
err = b.WriteByte(byte(r)) |
| 722 |
if err != nil { |
| 723 |
return 0, err |
| 724 |
} |
| 725 |
return 1, nil |
| 726 |
} |
| 727 |
if b.err != nil { |
| 728 |
return 0, b.err |
| 729 |
} |
| 730 |
n := b.Available() |
| 731 |
if n < utf8.UTFMax { |
| 732 |
if b.Flush(); b.err != nil { |
| 733 |
return 0, b.err |
| 734 |
} |
| 735 |
n = b.Available() |
| 736 |
if n < utf8.UTFMax { |
| 737 |
// Can only happen if buffer is silly small. |
| 738 |
return b.WriteString(string(r)) |
| 739 |
} |
| 740 |
} |
| 741 |
size = utf8.EncodeRune(b.buf[b.n:], r) |
| 742 |
b.n += size |
| 743 |
return size, nil |
| 744 |
} |
| 745 |
|
| 746 |
// WriteString writes a string. |
| 747 |
// It returns the number of bytes written. |
| 748 |
// If the count is less than len(s), it also returns an error explaining |
| 749 |
// why the write is short. |
| 750 |
func (b *Writer) WriteString(s string) (int, error) { |
| 751 |
var sw io.StringWriter |
| 752 |
tryStringWriter := true |
| 753 |
|
| 754 |
nn := 0 |
| 755 |
for len(s) > b.Available() && b.err == nil { |
| 756 |
var n int |
| 757 |
if b.Buffered() == 0 && sw == nil && tryStringWriter { |
| 758 |
// Check at most once whether b.wr is a StringWriter. |
| 759 |
sw, tryStringWriter = b.wr.(io.StringWriter) |
| 760 |
} |
| 761 |
if b.Buffered() == 0 && tryStringWriter { |
| 762 |
// Large write, empty buffer, and the underlying writer supports |
| 763 |
// WriteString: forward the write to the underlying StringWriter. |
| 764 |
// This avoids an extra copy. |
| 765 |
n, b.err = sw.WriteString(s) |
| 766 |
} else { |
| 767 |
n = copy(b.buf[b.n:], s) |
| 768 |
b.n += n |
| 769 |
b.Flush() |
| 770 |
} |
| 771 |
nn += n |
| 772 |
s = s[n:] |
| 773 |
} |
| 774 |
if b.err != nil { |
| 775 |
return nn, b.err |
| 776 |
} |
| 777 |
n := copy(b.buf[b.n:], s) |
| 778 |
b.n += n |
| 779 |
nn += n |
| 780 |
return nn, nil |
| 781 |
} |
| 782 |
|
| 783 |
// ReadFrom implements [io.ReaderFrom]. If the underlying writer |
| 784 |
// supports the ReadFrom method, this calls the underlying ReadFrom. |
| 785 |
// If there is buffered data and an underlying ReadFrom, this fills |
| 786 |
// the buffer and writes it before calling ReadFrom. |
| 787 |
func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) { |
| 788 |
if b.err != nil { |
| 789 |
return 0, b.err |
| 790 |
} |
| 791 |
readerFrom, readerFromOK := b.wr.(io.ReaderFrom) |
| 792 |
var m int |
| 793 |
for { |
| 794 |
if b.Available() == 0 { |
| 795 |
if err1 := b.Flush(); err1 != nil { |
| 796 |
return n, err1 |
| 797 |
} |
| 798 |
} |
| 799 |
if readerFromOK && b.Buffered() == 0 { |
| 800 |
nn, err := readerFrom.ReadFrom(r) |
| 801 |
b.err = err |
| 802 |
n += nn |
| 803 |
return n, err |
| 804 |
} |
| 805 |
nr := 0 |
| 806 |
for nr < maxConsecutiveEmptyReads { |
| 807 |
m, err = r.Read(b.buf[b.n:]) |
| 808 |
if m != 0 || err != nil { |
| 809 |
break |
| 810 |
} |
| 811 |
nr++ |
| 812 |
} |
| 813 |
if nr == maxConsecutiveEmptyReads { |
| 814 |
return n, io.ErrNoProgress |
| 815 |
} |
| 816 |
b.n += m |
| 817 |
n += int64(m) |
| 818 |
if err != nil { |
| 819 |
break |
| 820 |
} |
| 821 |
} |
| 822 |
if err == io.EOF { |
| 823 |
// If we filled the buffer exactly, flush preemptively. |
| 824 |
if b.Available() == 0 { |
| 825 |
err = b.Flush() |
| 826 |
} else { |
| 827 |
err = nil |
| 828 |
} |
| 829 |
} |
| 830 |
return n, err |
| 831 |
} |
| 832 |
|
| 833 |
// buffered input and output |
| 834 |
|
| 835 |
// ReadWriter stores pointers to a [Reader] and a [Writer]. |
| 836 |
// It implements [io.ReadWriter]. |
| 837 |
type ReadWriter struct { |
| 838 |
*Reader |
| 839 |
*Writer |
| 840 |
} |
| 841 |
|
| 842 |
// NewReadWriter allocates a new [ReadWriter] that dispatches to r and w. |
| 843 |
func NewReadWriter(r *Reader, w *Writer) *ReadWriter { |
| 844 |
return &ReadWriter{r, w} |
| 845 |
} |