Golang標準庫之Buffer

Buffer

   Go標準庫Buffer是一個可變大小的字節緩衝區,可以用Wirte和Read方法操作它,在Go標準庫中,定義瞭如下關於Buffer的數據結構。

type Buffer struct {
    buf       []byte            // contents are the bytes buf[off : len(buf)]
    off       int               // read at &buf[off], write at &buf[len(buf)]
    runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Rune
    bootstrap [64]byte          // memory to hold first slice; helps small buffers (Printf) avoid allocation.
    lastRead  readOp            // last read operation, so that Unread* can work correctly.
}
// The readOp constants describe the last action performed on
// the buffer, so that UnreadRune and UnreadByte can
// check for invalid usage.
type readOp int
const (
    opInvalid  readOp = iota // Non-read operation.
    opReadRune               // Read rune.
    opRead                   // Any other read operation.
)

   如上定義,Buffer存儲的數據是在off到len(buf)區域之間,其他區域是沒有數據,而且只能從&buf[off]開始讀取數據和從&buf[len(buf)]寫數據,同時爲了避免對內存的多次操作,對於小的緩衝區,Buffer定義了bootstrap來避免多次內存的操作,runeBytes的定義也是如此目的,還有一個表示對Buffer的操作標識符lastRead。


Buffer的常見操作

  1. 初始化Buffer

    func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
    func NewBufferString(s string) *Buffer {
        return &Buffer{buf: []byte(s)}
    }

    方法NewBuffer使用buf作爲參數初始化Buffer,Buffer既可以被讀也可以被寫,如果是讀Buffer,buf需填充一定的數據,如果是寫,buf需有一定的容量(capacity),當然也可以通過new(Buffer)來初始化Buffer。另外一個方法NewBufferString用一個string來初始化可讀Buffer,並用string的內容填充Buffer.

  2. 讀寫操作

    func (b *Buffer) Read(p []byte) (n int, err error)
    func (b *Buffer) Next(n int) []byte
    func (b *Buffer) ReadByte() (c byte, err error)
    func (b *Buffer) ReadRune() (r rune, size int, err error)
    func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)
    func (b *Buffer) readSlice(delim byte) (line []byte, err error)
    func (b *Buffer) ReadString(delim byte) (line string, err error)
    func (b *Buffer) Write(p []byte) (n int, err error)
    func (b *Buffer) WriteString(s string) (n int, err error)
    func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)
    func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
    func (b *Buffer) WriteByte(c byte) error
    func (b *Buffer) WriteRune(r rune) (n int, err error)

    下面對Read,ReadRune,ReadBytes方法進行分析,對於方法Read, 其主要做三個步驟:第一,判斷Buffer是否爲空,如果是,則重置Buffer;第二,複製Buffer的buf的數據到p,並調整off的位置標識Buffer的可讀位置;第三,設置讀標識符爲opRead。

    func (b *Buffer) Read(p []byte) (n int, err error) {
        b.lastRead = opInvalid
        if b.off >= len(b.buf) {
            // Buffer is empty, reset to recover space.
            b.Truncate(0)
            if len(p) == 0 {
                return
            }
            return 0, io.EOF
        }
        n = copy(p, b.buf[b.off:])
        b.off += n
        if n > 0 {
            b.lastRead = opRead
        }
        return
    }

   方法ReadRune()定義瞭如何讀取Buffer中UTF8編碼的rune數據,同樣也需三個步驟,第一,判斷Buffer是否爲空,若是,重置Buffer;第二,設置讀操作符爲opReadRune;第三,判斷可讀位置off處的byte是否小於utf8.Runeself,若是,調整off位置並返回。否則,將Buffer的數據解碼成rune,調整off位置,返回解碼後的rune及大小。

// ReadRune reads and returns the next UTF-8-encoded
// Unicode code point from the buffer.
// If no bytes are available, the error returned is io.EOF.
// If the bytes are an erroneous UTF-8 encoding, it
// consumes one byte and returns U+FFFD, 1.
func (b *Buffer) ReadRune() (r rune, size int, err error) {
    b.lastRead = opInvalid
    if b.off >= len(b.buf) {
        // Buffer is empty, reset to recover space.
        b.Truncate(0)
        return 0, 0, io.EOF
    }
    b.lastRead = opReadRune
    c := b.buf[b.off]
    if c < utf8.RuneSelf {
        b.off++
        return rune(c), 1, nil
    }
    r, n := utf8.DecodeRune(b.buf[b.off:])
    b.off += n
    return r, n, nil
}

 方法ReadBytes(delim byte)讀取Buffer中從off到第一次delim之間的數據,並且包括delim,ReadBytes調用私有方法readSlice來實現,readSlice方法首先查找delim的位置,如果不存在,則返回從off到len(buf)之間的數據,如果存在,則返回off到off+location(delim)+1之間數據,其中加1是爲了包括delim,最後設置操作標識符爲opRead。


// 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,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadBytes returns err != nil if and only if the returned data does not end in
// delim.
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
    slice, err := b.readSlice(delim)
    // return a copy of slice. The buffer's backing array may
    // be overwritten by later calls.
    line = append(line, slice...)
    return
}
// readSlice is like ReadBytes but returns a reference to internal buffer data.
func (b *Buffer) readSlice(delim byte) (line []byte, err error) {
    i := IndexByte(b.buf[b.off:], delim)
    end := b.off + i + 1
    if i < 0 {
        end = len(b.buf)
        err = io.EOF
    }
    line = b.buf[b.off:end]
    b.off = end
    b.lastRead = opRead
    return line, err
}

 同樣對相應的Write,WriteRune,ReadFrom, WriteTo寫方法進行分析, 對於方法Write,相對Read方法來說,要簡單些,主要是擴展Buffer空間,然後將p中的數據複製到Buffer。

// Write appends the contents of p to the buffer, growing the buffer as
// needed. The return value n is the length of p; err is always nil. If the
// buffer becomes too large, Write will panic with ErrTooLarge.
func (b *Buffer) Write(p []byte) (n int, err error) {
    b.lastRead = opInvalid
    m := b.grow(len(p))
    return copy(b.buf[m:], p), nil
}

 對於方法WriteRune,首先判斷要寫的數據rune是否小於utf8.RuneSelf,若是,調用WriteByte將其寫入Buffer,若不是,則將要寫的數據rune編碼成utf8,並調用Write將其寫入Buffer。

// WriteRune appends the UTF-8 encoding of Unicode code point r to the
// buffer, returning its length and an error, which is always nil but is
// included to match bufio.Writer's WriteRune. The buffer is grown as needed;
// if it becomes too large, WriteRune will panic with ErrTooLarge.
func (b *Buffer) WriteRune(r rune) (n int, err error) {
    if r < utf8.RuneSelf {
        b.WriteByte(byte(r))
        return 1, nil
    }
    n = utf8.EncodeRune(b.runeBytes[0:], r)
    b.Write(b.runeBytes[0:n])
    return n, nil
}

ReadFrom方法從io.Reader或者實現io.Reader接口的實例中讀取所有數據到Buffer,默認情況下最少讀取512字節,如果Buffer空間不足512,需增加Buffer空間,該方法返回讀取的字節數以及錯誤信息。從下面可知ReadFrom首先判斷Buffer是否爲空,若空,則重置Buffer;其次是判斷Buffer的free空間是否足夠,若小於512且off+free小於512,表示Buffer從0到off之間的空間不足以存放當前Buffer中未讀數據的大小,此時設置一臨時緩衝區並使其空間Buffer的2倍加上MinRead(512)的空間,將原來Buffer的數據複製到臨時緩衝區,然後再把臨時緩衝區的數據複製到源Buffer,最後使用io.Reader的Read方法從io.Reader中讀取數據,直到遇到io.EOF。

// ReadFrom reads data from r until EOF and appends it to the buffer, growing
// the buffer as needed. The return value n is the number of bytes read. Any
// error except io.EOF encountered during the read is also returned. If the
// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
    b.lastRead = opInvalid
    // If buffer is empty, reset to recover space.
    if b.off >= len(b.buf) {
        b.Truncate(0)
    }
    for {
        if free := cap(b.buf) - len(b.buf); free < MinRead {
            // not enough space at end
            newBuf := b.buf
            if b.off+free < MinRead {
                // not enough space using beginning of buffer;
                // double buffer capacity
                newBuf = makeSlice(2*cap(b.buf) + MinRead)
            }
            copy(newBuf, b.buf[b.off:])
            b.buf = newBuf[:len(b.buf)-b.off]
            b.off = 0
        }
        m, e := r.Read(b.buf[len(b.buf):cap(b.buf)])
        b.buf = b.buf[0 : len(b.buf)+m]
        n += int64(m)
        if e == io.EOF {
            break
        }
        if e != nil {
            return n, e
        }
    }
    return n, nil // err is EOF, so return nil explicitly
}
 相對ReadFrom方法,WriteTo方法比較簡單,WriteTo將Buffer中的數據寫到io.Writer,直到Buffer中沒有數據,當Buffer爲空時,重置Buffer並返回。
// 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.
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
    b.lastRead = opInvalid
    if b.off < len(b.buf) {
        nBytes := b.Len()
        m, e := w.Write(b.buf[b.off:])
        if m > nBytes {
            panic("bytes.Buffer.WriteTo: invalid Write count")
        }
        b.off += m
        n = int64(m)
        if e != nil {
            return n, e
        }
        // all bytes should have been written, by definition of
        // Write method in io.Writer
        if m != nBytes {
            return n, io.ErrShortWrite
        }
    }
    // Buffer is now empty; reset.
    b.Truncate(0)
    return
}

3. 擴展空間和重置  

  Buffer的重置方法Reset()通過調用Truncate(n int)方法來實現清除Buffer的數據,Truncate丟棄除了從off開始的n個未讀數據之外的所有數據, 如果n爲0,那就重置Buffer。

 

// Truncate discards all but the first n unread bytes from the buffer.
// It panics if n is negative or greater than the length of the buffer.
func (b *Buffer) Truncate(n int) {
    b.lastRead = opInvalid
    switch {
    case n < 0 || n > b.Len():
        panic("bytes.Buffer: truncation out of range")
    case n == 0:
        // Reuse buffer space.
        b.off = 0
    }
    b.buf = b.buf[0 : b.off+n]
}
// Reset resets the buffer so it has no content.
// b.Reset() is the same as b.Truncate(0).
func (b *Buffer) Reset() { b.Truncate(0) }
 在對Buffer進行寫數據時,通常需要擴展其空間來使所有的數據都能寫入Buffer,Buffer用Grow(n int)方法來實現擴展Buffer空間的功能,該方法調用私有方法grow(n int)。

 

// grow grows the buffer to guarantee space for n more bytes.
// It returns the index where bytes should be written.
// If the buffer can't grow it will panic with ErrTooLarge.
func (b *Buffer) grow(n int) int {
    m := b.Len()
    // 如果Buffer爲空,重置Buffer
    if m == 0 && b.off != 0 {
        b.Truncate(0)
    }
    //空間增加n後超過Buffer的容量
    if len(b.buf)+n > cap(b.buf) {
        //聲明一個臨時buf
        var buf []byte
        //Buffer的buf只是被聲明,還沒有初始化,如果n小於bootstrap的空間,
        //直接將boostrap賦值給buf避免內存的操作而增加負載。
        if b.buf == nil && n <= len(b.bootstrap) {
            buf = b.bootstrap[0:]
        //如果滿足此條件,滑動b.buf的數據而不是分配一個新的slice空間,然後將b.buf的數據複製給你buf。
        } else if m+n <= cap(b.buf)/2 {
            copy(b.buf[:], b.buf[b.off:])
            buf = b.buf[:m]
        } else {
            //空間不足,重新分配空間
            buf = makeSlice(2*cap(b.buf) + n)
            copy(buf, b.buf[b.off:])
        }
        b.buf = buf
        b.off = 0
    }
    //擴展n的空間,並返回可以寫數據的位置
    b.buf = b.buf[0 : b.off+m+n]
    return b.off + m
}
// Grow grows the buffer's capacity, if necessary, to guarantee space for
// another n bytes. After Grow(n), at least n bytes can be written to the
// buffer without another allocation.
// If n is negative, Grow will panic.
// If the buffer can't grow it will panic with ErrTooLarge.
func (b *Buffer) Grow(n int) {
    if n < 0 {
        panic("bytes.Buffer.Grow: negative count")
    }
    m := b.grow(n)
    b.buf = b.buf[0:m]
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章