|
|
@ -6,7 +6,7 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
const (
|
|
|
|
DefaultOffset = 16
|
|
|
|
defaultOffset = 16
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
|
|
|
|
// Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
|
|
|
@ -33,7 +33,7 @@ func (b *Buffer) Release() {
|
|
|
|
// Clear clears the content of the buffer, results an empty buffer with
|
|
|
|
// Clear clears the content of the buffer, results an empty buffer with
|
|
|
|
// Len() = 0.
|
|
|
|
// Len() = 0.
|
|
|
|
func (b *Buffer) Clear() *Buffer {
|
|
|
|
func (b *Buffer) Clear() *Buffer {
|
|
|
|
b.offset = DefaultOffset
|
|
|
|
b.offset = defaultOffset
|
|
|
|
b.Value = b.head[b.offset:b.offset]
|
|
|
|
b.Value = b.head[b.offset:b.offset]
|
|
|
|
return b
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -58,6 +58,7 @@ func (b *Buffer) Prepend(data []byte) *Buffer {
|
|
|
|
return b
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Bytes returns the content bytes of this Buffer.
|
|
|
|
func (b *Buffer) Bytes() []byte {
|
|
|
|
func (b *Buffer) Bytes() []byte {
|
|
|
|
return b.Value
|
|
|
|
return b.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -74,6 +75,8 @@ func (b *Buffer) SliceFrom(from int) *Buffer {
|
|
|
|
return b
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SliceBack extends the Buffer to its front by offset bytes.
|
|
|
|
|
|
|
|
// Caller must ensure cumulated offset is no more than 16.
|
|
|
|
func (b *Buffer) SliceBack(offset int) *Buffer {
|
|
|
|
func (b *Buffer) SliceBack(offset int) *Buffer {
|
|
|
|
newoffset := b.offset - offset
|
|
|
|
newoffset := b.offset - offset
|
|
|
|
if newoffset < 0 {
|
|
|
|
if newoffset < 0 {
|
|
|
@ -103,6 +106,7 @@ func (b *Buffer) Write(data []byte) (int, error) {
|
|
|
|
return len(data), nil
|
|
|
|
return len(data), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read implements io.Reader.Read().
|
|
|
|
func (b *Buffer) Read(data []byte) (int, error) {
|
|
|
|
func (b *Buffer) Read(data []byte) (int, error) {
|
|
|
|
if b.Len() == 0 {
|
|
|
|
if b.Len() == 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
return 0, io.EOF
|
|
|
@ -144,8 +148,8 @@ func (p *bufferPool) allocate() *Buffer {
|
|
|
|
return &Buffer{
|
|
|
|
return &Buffer{
|
|
|
|
head: b,
|
|
|
|
head: b,
|
|
|
|
pool: p,
|
|
|
|
pool: p,
|
|
|
|
Value: b[DefaultOffset:],
|
|
|
|
Value: b[defaultOffset:],
|
|
|
|
offset: DefaultOffset,
|
|
|
|
offset: defaultOffset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|