88
88
if b.buf == nil && n <= len(b.bootstrap) {
89
89
buf = b.bootstrap[0:]
90
} else if m+n <= cap(b.buf)/2 {
91
// We can slide things down instead of allocating a new
92
// slice. We only need m+n <= cap(b.buf) to slide, but
93
// we instead let capacity get twice as large so we
94
// don't spend all our time copying.
95
copy(b.buf[:], b.buf[b.off:])
91
98
// not enough space anywhere
92
99
buf = makeSlice(2*cap(b.buf) + n)
102
// Write appends the contents of p to the buffer. The return
103
// value n is the length of p; err is always nil.
104
// If the buffer becomes too large, Write will panic with
109
// Grow grows the buffer's capacity, if necessary, to guarantee space for
110
// another n bytes. After Grow(n), at least n bytes can be written to the
111
// buffer without another allocation.
112
// If n is negative, Grow will panic.
113
// If the buffer can't grow it will panic with ErrTooLarge.
114
func (b *Buffer) Grow(n int) {
116
panic("bytes.Buffer.Grow: negative count")
122
// Write appends the contents of p to the buffer, growing the buffer as
123
// needed. The return value n is the length of p; err is always nil. If the
124
// buffer becomes too large, Write will panic with ErrTooLarge.
106
125
func (b *Buffer) Write(p []byte) (n int, err error) {
107
126
b.lastRead = opInvalid
108
127
m := b.grow(len(p))
109
128
return copy(b.buf[m:], p), nil
112
// WriteString appends the contents of s to the buffer. The return
113
// value n is the length of s; err is always nil.
114
// If the buffer becomes too large, WriteString will panic with
131
// WriteString appends the contents of s to the buffer, growing the buffer as
132
// needed. The return value n is the length of s; err is always nil. If the
133
// buffer becomes too large, WriteString will panic with ErrTooLarge.
116
134
func (b *Buffer) WriteString(s string) (n int, err error) {
117
135
b.lastRead = opInvalid
118
136
m := b.grow(len(s))
125
143
// underlying buffer.
126
144
const MinRead = 512
128
// ReadFrom reads data from r until EOF and appends it to the buffer.
129
// The return value n is the number of bytes read.
130
// Any error except io.EOF encountered during the read
132
// If the buffer becomes too large, ReadFrom will panic with
146
// ReadFrom reads data from r until EOF and appends it to the buffer, growing
147
// the buffer as needed. The return value n is the number of bytes read. Any
148
// error except io.EOF encountered during the read is also returned. If the
149
// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
134
150
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
135
151
b.lastRead = opInvalid
136
152
// If buffer is empty, reset to recover space.
175
191
return make([]byte, n)
178
// WriteTo writes data to w until the buffer is drained or an error
179
// occurs. The return value n is the number of bytes written; it always
180
// fits into an int, but it is int64 to match the io.WriterTo interface.
181
// Any error encountered during the write is also returned.
194
// WriteTo writes data to w until the buffer is drained or an error occurs.
195
// The return value n is the number of bytes written; it always fits into an
196
// int, but it is int64 to match the io.WriterTo interface. Any error
197
// encountered during the write is also returned.
182
198
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
183
199
b.lastRead = opInvalid
184
200
if b.off < len(b.buf) {
206
// WriteByte appends the byte c to the buffer.
207
// The returned error is always nil, but is included
208
// to match bufio.Writer's WriteByte.
209
// If the buffer becomes too large, WriteByte will panic with
222
// WriteByte appends the byte c to the buffer, growing the buffer as needed.
223
// The returned error is always nil, but is included to match bufio.Writer's
224
// WriteByte. If the buffer becomes too large, WriteByte will panic with
211
226
func (b *Buffer) WriteByte(c byte) error {
212
227
b.lastRead = opInvalid
218
// WriteRune appends the UTF-8 encoding of Unicode
219
// code point r to the buffer, returning its length and
220
// an error, which is always nil but is included
221
// to match bufio.Writer's WriteRune.
222
// If the buffer becomes too large, WriteRune will panic with
233
// WriteRune appends the UTF-8 encoding of Unicode code point r to the
234
// buffer, returning its length and an error, which is always nil but is
235
// included to match bufio.Writer's WriteRune. The buffer is grown as needed;
236
// if it becomes too large, WriteRune will panic with ErrTooLarge.
224
237
func (b *Buffer) WriteRune(r rune) (n int, err error) {
225
238
if r < utf8.RuneSelf {
226
239
b.WriteByte(byte(r))
347
360
// ReadBytes returns err != nil if and only if the returned data does not end in
349
362
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
363
slice, err := b.readSlice(delim)
364
// return a copy of slice. The buffer's backing array may
365
// be overwritten by later calls.
366
line = append(line, slice...)
370
// readSlice is like ReadBytes but returns a reference to internal buffer data.
371
func (b *Buffer) readSlice(delim byte) (line []byte, err error) {
350
372
i := IndexByte(b.buf[b.off:], delim)
353
size = len(b.buf) - b.off
356
line = make([]byte, size)
357
copy(line, b.buf[b.off:])
378
line = b.buf[b.off:end]
362
384
// ReadString reads until the first occurrence of delim in the input,
366
388
// ReadString returns err != nil if and only if the returned data does not end
368
390
func (b *Buffer) ReadString(delim byte) (line string, err error) {
369
bytes, err := b.ReadBytes(delim)
370
return string(bytes), err
391
slice, err := b.readSlice(delim)
392
return string(slice), err
373
395
// NewBuffer creates and initializes a new Buffer using buf as its initial