2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer
|
2010-12-03 05:34:57 +01:00
|
|
|
// object, creating another object (Reader or Writer) that also implements
|
|
|
|
// the interface but provides buffering and some help for textual I/O.
|
|
|
|
package bufio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
2011-12-07 02:11:29 +01:00
|
|
|
"unicode/utf8"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultBufSize = 4096
|
|
|
|
)
|
|
|
|
|
|
|
|
// Errors introduced by this package.
|
|
|
|
type Error struct {
|
2011-09-16 17:47:21 +02:00
|
|
|
ErrorString string
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (err *Error) Error() string { return err.ErrorString }
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
var (
|
2011-12-03 03:17:34 +01:00
|
|
|
ErrInvalidUnreadByte error = &Error{"bufio: invalid use of UnreadByte"}
|
|
|
|
ErrInvalidUnreadRune error = &Error{"bufio: invalid use of UnreadRune"}
|
|
|
|
ErrBufferFull error = &Error{"bufio: buffer full"}
|
|
|
|
ErrNegativeCount error = &Error{"bufio: negative count"}
|
|
|
|
errInternal error = &Error{"bufio: internal error"}
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// BufSizeError is the error representing an invalid buffer size.
|
|
|
|
type BufSizeError int
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b BufSizeError) Error() string {
|
2010-12-03 05:34:57 +01:00
|
|
|
return "bufio: bad buffer size " + strconv.Itoa(int(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffered input.
|
|
|
|
|
|
|
|
// Reader implements buffering for an io.Reader object.
|
|
|
|
type Reader struct {
|
|
|
|
buf []byte
|
|
|
|
rd io.Reader
|
|
|
|
r, w int
|
2011-12-03 03:17:34 +01:00
|
|
|
err error
|
2010-12-03 05:34:57 +01:00
|
|
|
lastByte int
|
|
|
|
lastRuneSize int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReaderSize creates a new Reader whose buffer has the specified size,
|
2011-10-27 01:57:58 +02:00
|
|
|
// which must be greater than one. If the argument io.Reader is already a
|
2010-12-03 05:34:57 +01:00
|
|
|
// Reader with large enough size, it returns the underlying Reader.
|
|
|
|
// It returns the Reader and any error.
|
2011-12-03 03:17:34 +01:00
|
|
|
func NewReaderSize(rd io.Reader, size int) (*Reader, error) {
|
2011-10-27 01:57:58 +02:00
|
|
|
if size <= 1 {
|
2010-12-03 05:34:57 +01:00
|
|
|
return nil, BufSizeError(size)
|
|
|
|
}
|
|
|
|
// Is it already a Reader?
|
|
|
|
b, ok := rd.(*Reader)
|
|
|
|
if ok && len(b.buf) >= size {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
b = new(Reader)
|
|
|
|
b.buf = make([]byte, size)
|
|
|
|
b.rd = rd
|
|
|
|
b.lastByte = -1
|
|
|
|
b.lastRuneSize = -1
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader returns a new Reader whose buffer has the default size.
|
|
|
|
func NewReader(rd io.Reader) *Reader {
|
|
|
|
b, err := NewReaderSize(rd, defaultBufSize)
|
|
|
|
if err != nil {
|
|
|
|
// cannot happen - defaultBufSize is a valid size
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill reads a new chunk into the buffer.
|
|
|
|
func (b *Reader) fill() {
|
|
|
|
// Slide existing data to beginning.
|
|
|
|
if b.r > 0 {
|
|
|
|
copy(b.buf, b.buf[b.r:b.w])
|
|
|
|
b.w -= b.r
|
|
|
|
b.r = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read new data.
|
|
|
|
n, e := b.rd.Read(b.buf[b.w:])
|
|
|
|
b.w += n
|
|
|
|
if e != nil {
|
|
|
|
b.err = e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) readErr() error {
|
2011-09-16 17:47:21 +02:00
|
|
|
err := b.err
|
|
|
|
b.err = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Peek returns the next n bytes without advancing the reader. The bytes stop
|
|
|
|
// being valid at the next read call. If Peek returns fewer than n bytes, it
|
|
|
|
// also returns an error explaining why the read is short. The error is
|
|
|
|
// ErrBufferFull if n is larger than b's buffer size.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) Peek(n int) ([]byte, error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if n < 0 {
|
|
|
|
return nil, ErrNegativeCount
|
|
|
|
}
|
|
|
|
if n > len(b.buf) {
|
|
|
|
return nil, ErrBufferFull
|
|
|
|
}
|
|
|
|
for b.w-b.r < n && b.err == nil {
|
|
|
|
b.fill()
|
|
|
|
}
|
|
|
|
m := b.w - b.r
|
|
|
|
if m > n {
|
|
|
|
m = n
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
err := b.readErr()
|
2010-12-03 05:34:57 +01:00
|
|
|
if m < n && err == nil {
|
|
|
|
err = ErrBufferFull
|
|
|
|
}
|
|
|
|
return b.buf[b.r : b.r+m], err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads data into p.
|
|
|
|
// It returns the number of bytes read into p.
|
2011-01-21 19:19:03 +01:00
|
|
|
// It calls Read at most once on the underlying Reader,
|
|
|
|
// hence n may be less than len(p).
|
2011-12-07 02:11:29 +01:00
|
|
|
// At EOF, the count will be zero and err will be io.EOF.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) Read(p []byte) (n int, err error) {
|
2011-01-21 19:19:03 +01:00
|
|
|
n = len(p)
|
|
|
|
if n == 0 {
|
2011-09-16 17:47:21 +02:00
|
|
|
return 0, b.readErr()
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
if b.w == b.r {
|
|
|
|
if b.err != nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
return 0, b.readErr()
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
|
|
|
if len(p) >= len(b.buf) {
|
|
|
|
// Large read, empty buffer.
|
|
|
|
// Read directly into p to avoid copy.
|
|
|
|
n, b.err = b.rd.Read(p)
|
|
|
|
if n > 0 {
|
|
|
|
b.lastByte = int(p[n-1])
|
|
|
|
b.lastRuneSize = -1
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
return n, b.readErr()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
b.fill()
|
|
|
|
if b.w == b.r {
|
2011-09-16 17:47:21 +02:00
|
|
|
return 0, b.readErr()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
|
|
|
|
if n > b.w-b.r {
|
|
|
|
n = b.w - b.r
|
|
|
|
}
|
|
|
|
copy(p[0:n], b.buf[b.r:])
|
|
|
|
b.r += n
|
|
|
|
b.lastByte = int(b.buf[b.r-1])
|
|
|
|
b.lastRuneSize = -1
|
|
|
|
return n, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadByte reads and returns a single byte.
|
|
|
|
// If no byte is available, returns an error.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) ReadByte() (c byte, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
b.lastRuneSize = -1
|
|
|
|
for b.w == b.r {
|
|
|
|
if b.err != nil {
|
2011-09-16 17:47:21 +02:00
|
|
|
return 0, b.readErr()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
b.fill()
|
|
|
|
}
|
|
|
|
c = b.buf[b.r]
|
|
|
|
b.r++
|
|
|
|
b.lastByte = int(c)
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnreadByte unreads the last byte. Only the most recently read byte can be unread.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) UnreadByte() error {
|
2010-12-03 05:34:57 +01:00
|
|
|
b.lastRuneSize = -1
|
|
|
|
if b.r == b.w && b.lastByte >= 0 {
|
|
|
|
b.w = 1
|
|
|
|
b.r = 0
|
|
|
|
b.buf[0] = byte(b.lastByte)
|
|
|
|
b.lastByte = -1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.r <= 0 {
|
|
|
|
return ErrInvalidUnreadByte
|
|
|
|
}
|
|
|
|
b.r--
|
|
|
|
b.lastByte = -1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadRune reads a single UTF-8 encoded Unicode character and returns the
|
|
|
|
// rune and its size in bytes.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) ReadRune() (r rune, size int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
for b.r+utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) && b.err == nil {
|
|
|
|
b.fill()
|
|
|
|
}
|
|
|
|
b.lastRuneSize = -1
|
|
|
|
if b.r == b.w {
|
2011-09-16 17:47:21 +02:00
|
|
|
return 0, 0, b.readErr()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
r, size = rune(b.buf[b.r]), 1
|
|
|
|
if r >= 0x80 {
|
|
|
|
r, size = utf8.DecodeRune(b.buf[b.r:b.w])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
b.r += size
|
|
|
|
b.lastByte = int(b.buf[b.r-1])
|
|
|
|
b.lastRuneSize = size
|
2011-12-02 20:34:41 +01:00
|
|
|
return r, size, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnreadRune unreads the last rune. If the most recent read operation on
|
|
|
|
// the buffer was not a ReadRune, UnreadRune returns an error. (In this
|
|
|
|
// regard it is stricter than UnreadByte, which will unread the last byte
|
|
|
|
// from any read operation.)
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) UnreadRune() error {
|
2010-12-03 05:34:57 +01:00
|
|
|
if b.lastRuneSize < 0 || b.r == 0 {
|
|
|
|
return ErrInvalidUnreadRune
|
|
|
|
}
|
|
|
|
b.r -= b.lastRuneSize
|
|
|
|
b.lastByte = -1
|
|
|
|
b.lastRuneSize = -1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffered returns the number of bytes that can be read from the current buffer.
|
|
|
|
func (b *Reader) Buffered() int { return b.w - b.r }
|
|
|
|
|
|
|
|
// ReadSlice reads until the first occurrence of delim in the input,
|
|
|
|
// returning a slice pointing at the bytes in the buffer.
|
|
|
|
// The bytes stop being valid at the next read call.
|
|
|
|
// If ReadSlice encounters an error before finding a delimiter,
|
2011-12-07 02:11:29 +01:00
|
|
|
// it returns all the data in the buffer and the error itself (often io.EOF).
|
2010-12-03 05:34:57 +01:00
|
|
|
// ReadSlice fails with error ErrBufferFull if the buffer fills without a delim.
|
|
|
|
// Because the data returned from ReadSlice will be overwritten
|
|
|
|
// by the next I/O operation, most clients should use
|
|
|
|
// ReadBytes or ReadString instead.
|
|
|
|
// ReadSlice returns err != nil if and only if line does not end in delim.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) ReadSlice(delim byte) (line []byte, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
// Look in buffer.
|
|
|
|
if i := bytes.IndexByte(b.buf[b.r:b.w], delim); i >= 0 {
|
|
|
|
line1 := b.buf[b.r : b.r+i+1]
|
|
|
|
b.r += i + 1
|
|
|
|
return line1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read more into buffer, until buffer fills or we find delim.
|
|
|
|
for {
|
|
|
|
if b.err != nil {
|
|
|
|
line := b.buf[b.r:b.w]
|
|
|
|
b.r = b.w
|
2011-09-16 17:47:21 +02:00
|
|
|
return line, b.readErr()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
n := b.Buffered()
|
|
|
|
b.fill()
|
|
|
|
|
|
|
|
// Search new part of buffer
|
|
|
|
if i := bytes.IndexByte(b.buf[n:b.w], delim); i >= 0 {
|
|
|
|
line := b.buf[0 : n+i+1]
|
|
|
|
b.r = n + i + 1
|
|
|
|
return line, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer is full?
|
|
|
|
if b.Buffered() >= len(b.buf) {
|
|
|
|
b.r = b.w
|
|
|
|
return b.buf, ErrBufferFull
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// ReadLine tries to return a single line, not including the end-of-line bytes.
|
|
|
|
// If the line was too long for the buffer then isPrefix is set and the
|
|
|
|
// beginning of the line is returned. The rest of the line will be returned
|
|
|
|
// from future calls. isPrefix will be false when returning the last fragment
|
|
|
|
// of the line. The returned buffer is only valid until the next call to
|
|
|
|
// ReadLine. ReadLine either returns a non-nil line or it returns an error,
|
|
|
|
// never both.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) {
|
2011-05-20 02:18:15 +02:00
|
|
|
line, err = b.ReadSlice('\n')
|
|
|
|
if err == ErrBufferFull {
|
2011-10-27 01:57:58 +02:00
|
|
|
// Handle the case where "\r\n" straddles the buffer.
|
|
|
|
if len(line) > 0 && line[len(line)-1] == '\r' {
|
|
|
|
// Put the '\r' back on buf and drop it from line.
|
|
|
|
// Let the next call to ReadLine check for "\r\n".
|
|
|
|
if b.r == 0 {
|
|
|
|
// should be unreachable
|
|
|
|
panic("bufio: tried to rewind past start of buffer")
|
|
|
|
}
|
|
|
|
b.r--
|
|
|
|
line = line[:len(line)-1]
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
return line, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(line) == 0 {
|
2011-12-07 02:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
line = nil
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = nil
|
|
|
|
|
|
|
|
if line[len(line)-1] == '\n' {
|
2011-10-27 01:57:58 +02:00
|
|
|
drop := 1
|
|
|
|
if len(line) > 1 && line[len(line)-2] == '\r' {
|
|
|
|
drop = 2
|
|
|
|
}
|
|
|
|
line = line[:len(line)-drop]
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// ReadBytes reads until the first occurrence of delim in the input,
|
|
|
|
// returning a slice containing the data up to and including the delimiter.
|
|
|
|
// If ReadBytes encounters an error before finding a delimiter,
|
2011-12-07 02:11:29 +01:00
|
|
|
// it returns the data read before the error and the error itself (often io.EOF).
|
2011-03-17 00:05:44 +01:00
|
|
|
// ReadBytes returns err != nil if and only if the returned data does not end in
|
|
|
|
// delim.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) ReadBytes(delim byte) (line []byte, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
// Use ReadSlice to look for array,
|
|
|
|
// accumulating full buffers.
|
|
|
|
var frag []byte
|
|
|
|
var full [][]byte
|
|
|
|
err = nil
|
|
|
|
|
|
|
|
for {
|
2011-12-03 03:17:34 +01:00
|
|
|
var e error
|
2010-12-03 05:34:57 +01:00
|
|
|
frag, e = b.ReadSlice(delim)
|
|
|
|
if e == nil { // got final fragment
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if e != ErrBufferFull { // unexpected error
|
|
|
|
err = e
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a copy of the buffer.
|
|
|
|
buf := make([]byte, len(frag))
|
|
|
|
copy(buf, frag)
|
|
|
|
full = append(full, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate new buffer to hold the full pieces and the fragment.
|
|
|
|
n := 0
|
|
|
|
for i := range full {
|
|
|
|
n += len(full[i])
|
|
|
|
}
|
|
|
|
n += len(frag)
|
|
|
|
|
|
|
|
// Copy full pieces and fragment in.
|
|
|
|
buf := make([]byte, n)
|
|
|
|
n = 0
|
|
|
|
for i := range full {
|
|
|
|
n += copy(buf[n:], full[i])
|
|
|
|
}
|
|
|
|
copy(buf[n:], frag)
|
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadString reads until the first occurrence of delim in the input,
|
|
|
|
// returning a string containing the data up to and including the delimiter.
|
|
|
|
// If ReadString encounters an error before finding a delimiter,
|
2011-12-07 02:11:29 +01:00
|
|
|
// it returns the data read before the error and the error itself (often io.EOF).
|
2011-03-17 00:05:44 +01:00
|
|
|
// ReadString returns err != nil if and only if the returned data does not end in
|
|
|
|
// delim.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Reader) ReadString(delim byte) (line string, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
bytes, e := b.ReadBytes(delim)
|
|
|
|
return string(bytes), e
|
|
|
|
}
|
|
|
|
|
|
|
|
// buffered output
|
|
|
|
|
|
|
|
// Writer implements buffering for an io.Writer object.
|
|
|
|
type Writer struct {
|
2011-12-03 03:17:34 +01:00
|
|
|
err error
|
2010-12-03 05:34:57 +01:00
|
|
|
buf []byte
|
|
|
|
n int
|
|
|
|
wr io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWriterSize creates a new Writer whose buffer has the specified size,
|
|
|
|
// which must be greater than zero. If the argument io.Writer is already a
|
|
|
|
// Writer with large enough size, it returns the underlying Writer.
|
|
|
|
// It returns the Writer and any error.
|
2011-12-03 03:17:34 +01:00
|
|
|
func NewWriterSize(wr io.Writer, size int) (*Writer, error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if size <= 0 {
|
|
|
|
return nil, BufSizeError(size)
|
|
|
|
}
|
|
|
|
// Is it already a Writer?
|
|
|
|
b, ok := wr.(*Writer)
|
|
|
|
if ok && len(b.buf) >= size {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
b = new(Writer)
|
|
|
|
b.buf = make([]byte, size)
|
|
|
|
b.wr = wr
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWriter returns a new Writer whose buffer has the default size.
|
|
|
|
func NewWriter(wr io.Writer) *Writer {
|
|
|
|
b, err := NewWriterSize(wr, defaultBufSize)
|
|
|
|
if err != nil {
|
|
|
|
// cannot happen - defaultBufSize is valid size
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush writes any buffered data to the underlying io.Writer.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Writer) Flush() error {
|
2010-12-03 05:34:57 +01:00
|
|
|
if b.err != nil {
|
|
|
|
return b.err
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
if b.n == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
n, e := b.wr.Write(b.buf[0:b.n])
|
|
|
|
if n < b.n && e == nil {
|
|
|
|
e = io.ErrShortWrite
|
|
|
|
}
|
|
|
|
if e != nil {
|
|
|
|
if n > 0 && n < b.n {
|
|
|
|
copy(b.buf[0:b.n-n], b.buf[n:b.n])
|
|
|
|
}
|
|
|
|
b.n -= n
|
|
|
|
b.err = e
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
b.n = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Available returns how many bytes are unused in the buffer.
|
|
|
|
func (b *Writer) Available() int { return len(b.buf) - b.n }
|
|
|
|
|
|
|
|
// Buffered returns the number of bytes that have been written into the current buffer.
|
|
|
|
func (b *Writer) Buffered() int { return b.n }
|
|
|
|
|
|
|
|
// Write writes the contents of p into the buffer.
|
|
|
|
// It returns the number of bytes written.
|
|
|
|
// If nn < len(p), it also returns an error explaining
|
|
|
|
// why the write is short.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Writer) Write(p []byte) (nn int, err error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
for len(p) > b.Available() && b.err == nil {
|
|
|
|
var n int
|
|
|
|
if b.Buffered() == 0 {
|
2010-12-03 05:34:57 +01:00
|
|
|
// Large write, empty buffer.
|
|
|
|
// Write directly from p to avoid copy.
|
|
|
|
n, b.err = b.wr.Write(p)
|
2011-04-07 19:09:10 +02:00
|
|
|
} else {
|
|
|
|
n = copy(b.buf[b.n:], p)
|
|
|
|
b.n += n
|
|
|
|
b.Flush()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
nn += n
|
|
|
|
p = p[n:]
|
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
if b.err != nil {
|
|
|
|
return nn, b.err
|
|
|
|
}
|
|
|
|
n := copy(b.buf[b.n:], p)
|
|
|
|
b.n += n
|
|
|
|
nn += n
|
|
|
|
return nn, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteByte writes a single byte.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Writer) WriteByte(c byte) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
if b.err != nil {
|
|
|
|
return b.err
|
|
|
|
}
|
|
|
|
if b.Available() <= 0 && b.Flush() != nil {
|
|
|
|
return b.err
|
|
|
|
}
|
|
|
|
b.buf[b.n] = c
|
|
|
|
b.n++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteRune writes a single Unicode code point, returning
|
|
|
|
// the number of bytes written and any error.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Writer) WriteRune(r rune) (size int, err error) {
|
2011-12-02 20:34:41 +01:00
|
|
|
if r < utf8.RuneSelf {
|
|
|
|
err = b.WriteByte(byte(r))
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
if b.err != nil {
|
|
|
|
return 0, b.err
|
|
|
|
}
|
|
|
|
n := b.Available()
|
|
|
|
if n < utf8.UTFMax {
|
|
|
|
if b.Flush(); b.err != nil {
|
|
|
|
return 0, b.err
|
|
|
|
}
|
|
|
|
n = b.Available()
|
|
|
|
if n < utf8.UTFMax {
|
|
|
|
// Can only happen if buffer is silly small.
|
2011-12-02 20:34:41 +01:00
|
|
|
return b.WriteString(string(r))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
size = utf8.EncodeRune(b.buf[b.n:], r)
|
2010-12-03 05:34:57 +01:00
|
|
|
b.n += size
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteString writes a string.
|
|
|
|
// It returns the number of bytes written.
|
|
|
|
// If the count is less than len(s), it also returns an error explaining
|
|
|
|
// why the write is short.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (b *Writer) WriteString(s string) (int, error) {
|
2011-04-07 19:09:10 +02:00
|
|
|
nn := 0
|
|
|
|
for len(s) > b.Available() && b.err == nil {
|
|
|
|
n := copy(b.buf[b.n:], s)
|
|
|
|
b.n += n
|
|
|
|
nn += n
|
|
|
|
s = s[n:]
|
|
|
|
b.Flush()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
if b.err != nil {
|
|
|
|
return nn, b.err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
n := copy(b.buf[b.n:], s)
|
|
|
|
b.n += n
|
|
|
|
nn += n
|
|
|
|
return nn, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// buffered input and output
|
|
|
|
|
|
|
|
// ReadWriter stores pointers to a Reader and a Writer.
|
|
|
|
// It implements io.ReadWriter.
|
|
|
|
type ReadWriter struct {
|
|
|
|
*Reader
|
|
|
|
*Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReadWriter allocates a new ReadWriter that dispatches to r and w.
|
|
|
|
func NewReadWriter(r *Reader, w *Writer) *ReadWriter {
|
|
|
|
return &ReadWriter{r, w}
|
|
|
|
}
|