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 bytes 6 7 // Simple byte buffer for marshaling data. 8 9 import ( 10 "errors" 11 "io" 12 "unicode/utf8" 13 ) 14 15 // smallBufferSize is an initial allocation minimal capacity. 16 const smallBufferSize = 64 17 18 // A Buffer is a variable-sized buffer of bytes with Read and Write methods. 19 // The zero value for Buffer is an empty buffer ready to use. 20 type Buffer struct { 21 buf []byte // contents are the bytes buf[off : len(buf)] 22 off int // read at &buf[off], write at &buf[len(buf)] 23 lastRead readOp // last read operation, so that Unread* can work correctly. 24 } 25 26 // The readOp constants describe the last action performed on 27 // the buffer, so that UnreadRune and UnreadByte can check for 28 // invalid usage. opReadRuneX constants are chosen such that 29 // converted to int they correspond to the rune size that was read. 30 type readOp int8 31 32 // Don't use iota for these, as the values need to correspond with the 33 // names and comments, which is easier to see when being explicit. 34 const ( 35 opRead readOp = -1 // Any other read operation. 36 opInvalid readOp = 0 // Non-read operation. 37 opReadRune1 readOp = 1 // Read rune of size 1. 38 opReadRune2 readOp = 2 // Read rune of size 2. 39 opReadRune3 readOp = 3 // Read rune of size 3. 40 opReadRune4 readOp = 4 // Read rune of size 4. 41 ) 42 43 // ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer. 44 var ErrTooLarge = errors.New("bytes.Buffer: too large") 45 var errNegativeRead = errors.New("bytes.Buffer: reader returned negative count from Read") 46 47 const maxInt = int(^uint(0) >> 1) 48 49 // Bytes returns a slice of length b.Len() holding the unread portion of the buffer. 50 // The slice is valid for use only until the next buffer modification (that is, 51 // only until the next call to a method like Read, Write, Reset, or Truncate). 52 // The slice aliases the buffer content at least until the next buffer modification, 53 // so immediate changes to the slice will affect the result of future reads. 54 func (b *Buffer) Bytes() []byte { return b.buf[b.off:] } 55 56 // String returns the contents of the unread portion of the buffer 57 // as a string. If the Buffer is a nil pointer, it returns "<nil>". 58 // 59 // To build strings more efficiently, see the strings.Builder type. 60 func (b *Buffer) String() string { 61 if b == nil { 62 // Special case, useful in debugging. 63 return "<nil>" 64 } 65 return string(b.buf[b.off:]) 66 } 67 68 // empty reports whether the unread portion of the buffer is empty. 69 func (b *Buffer) empty() bool { return len(b.buf) <= b.off } 70 71 // Len returns the number of bytes of the unread portion of the buffer; 72 // b.Len() == len(b.Bytes()). 73 func (b *Buffer) Len() int { return len(b.buf) - b.off } 74 75 // Cap returns the capacity of the buffer's underlying byte slice, that is, the 76 // total space allocated for the buffer's data. 77 func (b *Buffer) Cap() int { return cap(b.buf) } 78 79 // Truncate discards all but the first n unread bytes from the buffer 80 // but continues to use the same allocated storage. 81 // It panics if n is negative or greater than the length of the buffer. 82 func (b *Buffer) Truncate(n int) { 83 if n == 0 { 84 b.Reset() 85 return 86 } 87 b.lastRead = opInvalid 88 if n < 0 || n > b.Len() { 89 panic("bytes.Buffer: truncation out of range") 90 } 91 b.buf = b.buf[:b.off+n] 92 } 93 94 // Reset resets the buffer to be empty, 95 // but it retains the underlying storage for use by future writes. 96 // Reset is the same as Truncate(0). 97 func (b *Buffer) Reset() { 98 b.buf = b.buf[:0] 99 b.off = 0 100 b.lastRead = opInvalid 101 } 102 103 // tryGrowByReslice is a inlineable version of grow for the fast-case where the 104 // internal buffer only needs to be resliced. 105 // It returns the index where bytes should be written and whether it succeeded. 106 func (b *Buffer) tryGrowByReslice(n int) (int, bool) { 107 if l := len(b.buf); n <= cap(b.buf)-l { 108 b.buf = b.buf[:l+n] 109 return l, true 110 } 111 return 0, false 112 } 113 114 // grow grows the buffer to guarantee space for n more bytes. 115 // It returns the index where bytes should be written. 116 // If the buffer can't grow it will panic with ErrTooLarge. 117 func (b *Buffer) grow(n int) int { 118 m := b.Len() 119 // If buffer is empty, reset to recover space. 120 if m == 0 && b.off != 0 { 121 b.Reset() 122 } 123 // Try to grow by means of a reslice. 124 if i, ok := b.tryGrowByReslice(n); ok { 125 return i 126 } 127 if b.buf == nil && n <= smallBufferSize { 128 b.buf = make([]byte, n, smallBufferSize) 129 return 0 130 } 131 c := cap(b.buf) 132 if n <= c/2-m { 133 // We can slide things down instead of allocating a new 134 // slice. We only need m+n <= c to slide, but 135 // we instead let capacity get twice as large so we 136 // don't spend all our time copying. 137 copy(b.buf, b.buf[b.off:]) 138 } else if c > maxInt-c-n { 139 panic(ErrTooLarge) 140 } else { 141 // Not enough space anywhere, we need to allocate. 142 buf := makeSlice(2*c + n) 143 copy(buf, b.buf[b.off:]) 144 b.buf = buf 145 } 146 // Restore b.off and len(b.buf). 147 b.off = 0 148 b.buf = b.buf[:m+n] 149 return m 150 } 151 152 // Grow grows the buffer's capacity, if necessary, to guarantee space for 153 // another n bytes. After Grow(n), at least n bytes can be written to the 154 // buffer without another allocation. 155 // If n is negative, Grow will panic. 156 // If the buffer can't grow it will panic with ErrTooLarge. 157 func (b *Buffer) Grow(n int) { 158 if n < 0 { 159 panic("bytes.Buffer.Grow: negative count") 160 } 161 m := b.grow(n) 162 b.buf = b.buf[:m] 163 } 164 165 // Write appends the contents of p to the buffer, growing the buffer as 166 // needed. The return value n is the length of p; err is always nil. If the 167 // buffer becomes too large, Write will panic with ErrTooLarge. 168 func (b *Buffer) Write(p []byte) (n int, err error) { 169 b.lastRead = opInvalid 170 m, ok := b.tryGrowByReslice(len(p)) 171 if !ok { 172 m = b.grow(len(p)) 173 } 174 return copy(b.buf[m:], p), nil 175 } 176 177 // WriteString appends the contents of s to the buffer, growing the buffer as 178 // needed. The return value n is the length of s; err is always nil. If the 179 // buffer becomes too large, WriteString will panic with ErrTooLarge. 180 func (b *Buffer) WriteString(s string) (n int, err error) { 181 b.lastRead = opInvalid 182 m, ok := b.tryGrowByReslice(len(s)) 183 if !ok { 184 m = b.grow(len(s)) 185 } 186 return copy(b.buf[m:], s), nil 187 } 188 189 // MinRead is the minimum slice size passed to a Read call by 190 // Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond 191 // what is required to hold the contents of r, ReadFrom will not grow the 192 // underlying buffer. 193 const MinRead = 512 194 195 // ReadFrom reads data from r until EOF and appends it to the buffer, growing 196 // the buffer as needed. The return value n is the number of bytes read. Any 197 // error except io.EOF encountered during the read is also returned. If the 198 // buffer becomes too large, ReadFrom will panic with ErrTooLarge. 199 func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) { 200 b.lastRead = opInvalid 201 for { 202 i := b.grow(MinRead) 203 b.buf = b.buf[:i] 204 m, e := r.Read(b.buf[i:cap(b.buf)]) 205 if m < 0 { 206 panic(errNegativeRead) 207 } 208 209 b.buf = b.buf[:i+m] 210 n += int64(m) 211 if e == io.EOF { 212 return n, nil // e is EOF, so return nil explicitly 213 } 214 if e != nil { 215 return n, e 216 } 217 } 218 } 219 220 // makeSlice allocates a slice of size n. If the allocation fails, it panics 221 // with ErrTooLarge. 222 func makeSlice(n int) []byte { 223 // If the make fails, give a known error. 224 defer func() { 225 if recover() != nil { 226 panic(ErrTooLarge) 227 } 228 }() 229 return make([]byte, n) 230 } 231 232 // WriteTo writes data to w until the buffer is drained or an error occurs. 233 // The return value n is the number of bytes written; it always fits into an 234 // int, but it is int64 to match the io.WriterTo interface. Any error 235 // encountered during the write is also returned. 236 func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) { 237 b.lastRead = opInvalid 238 if nBytes := b.Len(); nBytes > 0 { 239 m, e := w.Write(b.buf[b.off:]) 240 if m > nBytes { 241 panic("bytes.Buffer.WriteTo: invalid Write count") 242 } 243 b.off += m 244 n = int64(m) 245 if e != nil { 246 return n, e 247 } 248 // all bytes should have been written, by definition of 249 // Write method in io.Writer 250 if m != nBytes { 251 return n, io.ErrShortWrite 252 } 253 } 254 // Buffer is now empty; reset. 255 b.Reset() 256 return n, nil 257 } 258 259 // WriteByte appends the byte c to the buffer, growing the buffer as needed. 260 // The returned error is always nil, but is included to match bufio.Writer's 261 // WriteByte. If the buffer becomes too large, WriteByte will panic with 262 // ErrTooLarge. 263 func (b *Buffer) WriteByte(c byte) error { 264 b.lastRead = opInvalid 265 m, ok := b.tryGrowByReslice(1) 266 if !ok { 267 m = b.grow(1) 268 } 269 b.buf[m] = c 270 return nil 271 } 272 273 // WriteRune appends the UTF-8 encoding of Unicode code point r to the 274 // buffer, returning its length and an error, which is always nil but is 275 // included to match bufio.Writer's WriteRune. The buffer is grown as needed; 276 // if it becomes too large, WriteRune will panic with ErrTooLarge. 277 func (b *Buffer) WriteRune(r rune) (n int, err error) { 278 // Compare as uint32 to correctly handle negative runes. 279 if uint32(r) < utf8.RuneSelf { 280 b.WriteByte(byte(r)) 281 return 1, nil 282 } 283 b.lastRead = opInvalid 284 m, ok := b.tryGrowByReslice(utf8.UTFMax) 285 if !ok { 286 m = b.grow(utf8.UTFMax) 287 } 288 n = utf8.EncodeRune(b.buf[m:m+utf8.UTFMax], r) 289 b.buf = b.buf[:m+n] 290 return n, nil 291 } 292 293 // Read reads the next len(p) bytes from the buffer or until the buffer 294 // is drained. The return value n is the number of bytes read. If the 295 // buffer has no data to return, err is io.EOF (unless len(p) is zero); 296 // otherwise it is nil. 297 func (b *Buffer) Read(p []byte) (n int, err error) { 298 b.lastRead = opInvalid 299 if b.empty() { 300 // Buffer is empty, reset to recover space. 301 b.Reset() 302 if len(p) == 0 { 303 return 0, nil 304 } 305 return 0, io.EOF 306 } 307 n = copy(p, b.buf[b.off:]) 308 b.off += n 309 if n > 0 { 310 b.lastRead = opRead 311 } 312 return n, nil 313 } 314 315 // Next returns a slice containing the next n bytes from the buffer, 316 // advancing the buffer as if the bytes had been returned by Read. 317 // If there are fewer than n bytes in the buffer, Next returns the entire buffer. 318 // The slice is only valid until the next call to a read or write method. 319 func (b *Buffer) Next(n int) []byte { 320 b.lastRead = opInvalid 321 m := b.Len() 322 if n > m { 323 n = m 324 } 325 data := b.buf[b.off : b.off+n] 326 b.off += n 327 if n > 0 { 328 b.lastRead = opRead 329 } 330 return data 331 } 332 333 // ReadByte reads and returns the next byte from the buffer. 334 // If no byte is available, it returns error io.EOF. 335 func (b *Buffer) ReadByte() (byte, error) { 336 if b.empty() { 337 // Buffer is empty, reset to recover space. 338 b.Reset() 339 return 0, io.EOF 340 } 341 c := b.buf[b.off] 342 b.off++ 343 b.lastRead = opRead 344 return c, nil 345 } 346 347 // ReadRune reads and returns the next UTF-8-encoded 348 // Unicode code point from the buffer. 349 // If no bytes are available, the error returned is io.EOF. 350 // If the bytes are an erroneous UTF-8 encoding, it 351 // consumes one byte and returns U+FFFD, 1. 352 func (b *Buffer) ReadRune() (r rune, size int, err error) { 353 if b.empty() { 354 // Buffer is empty, reset to recover space. 355 b.Reset() 356 return 0, 0, io.EOF 357 } 358 c := b.buf[b.off] 359 if c < utf8.RuneSelf { 360 b.off++ 361 b.lastRead = opReadRune1 362 return rune(c), 1, nil 363 } 364 r, n := utf8.DecodeRune(b.buf[b.off:]) 365 b.off += n 366 b.lastRead = readOp(n) 367 return r, n, nil 368 } 369 370 // UnreadRune unreads the last rune returned by ReadRune. 371 // If the most recent read or write operation on the buffer was 372 // not a successful ReadRune, UnreadRune returns an error. (In this regard 373 // it is stricter than UnreadByte, which will unread the last byte 374 // from any read operation.) 375 func (b *Buffer) UnreadRune() error { 376 if b.lastRead <= opInvalid { 377 return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune") 378 } 379 if b.off >= int(b.lastRead) { 380 b.off -= int(b.lastRead) 381 } 382 b.lastRead = opInvalid 383 return nil 384 } 385 386 var errUnreadByte = errors.New("bytes.Buffer: UnreadByte: previous operation was not a successful read") 387 388 // UnreadByte unreads the last byte returned by the most recent successful 389 // read operation that read at least one byte. If a write has happened since 390 // the last read, if the last read returned an error, or if the read read zero 391 // bytes, UnreadByte returns an error. 392 func (b *Buffer) UnreadByte() error { 393 if b.lastRead == opInvalid { 394 return errUnreadByte 395 } 396 b.lastRead = opInvalid 397 if b.off > 0 { 398 b.off-- 399 } 400 return nil 401 } 402 403 // ReadBytes reads until the first occurrence of delim in the input, 404 // returning a slice containing the data up to and including the delimiter. 405 // If ReadBytes encounters an error before finding a delimiter, 406 // it returns the data read before the error and the error itself (often io.EOF). 407 // ReadBytes returns err != nil if and only if the returned data does not end in 408 // delim. 409 func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) { 410 slice, err := b.readSlice(delim) 411 // return a copy of slice. The buffer's backing array may 412 // be overwritten by later calls. 413 line = append(line, slice...) 414 return line, err 415 } 416 417 // readSlice is like ReadBytes but returns a reference to internal buffer data. 418 func (b *Buffer) readSlice(delim byte) (line []byte, err error) { 419 i := IndexByte(b.buf[b.off:], delim) 420 end := b.off + i + 1 421 if i < 0 { 422 end = len(b.buf) 423 err = io.EOF 424 } 425 line = b.buf[b.off:end] 426 b.off = end 427 b.lastRead = opRead 428 return line, err 429 } 430 431 // ReadString reads until the first occurrence of delim in the input, 432 // returning a string containing the data up to and including the delimiter. 433 // If ReadString encounters an error before finding a delimiter, 434 // it returns the data read before the error and the error itself (often io.EOF). 435 // ReadString returns err != nil if and only if the returned data does not end 436 // in delim. 437 func (b *Buffer) ReadString(delim byte) (line string, err error) { 438 slice, err := b.readSlice(delim) 439 return string(slice), err 440 } 441 442 // NewBuffer creates and initializes a new Buffer using buf as its 443 // initial contents. The new Buffer takes ownership of buf, and the 444 // caller should not use buf after this call. NewBuffer is intended to 445 // prepare a Buffer to read existing data. It can also be used to set 446 // the initial size of the internal buffer for writing. To do that, 447 // buf should have the desired capacity but a length of zero. 448 // 449 // In most cases, new(Buffer) (or just declaring a Buffer variable) is 450 // sufficient to initialize a Buffer. 451 func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} } 452 453 // NewBufferString creates and initializes a new Buffer using string s as its 454 // initial contents. It is intended to prepare a buffer to read an existing 455 // string. 456 // 457 // In most cases, new(Buffer) (or just declaring a Buffer variable) is 458 // sufficient to initialize a Buffer. 459 func NewBufferString(s string) *Buffer { 460 return &Buffer{buf: []byte(s)} 461 } 462