mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 10:08:15 -05:00
limit size of written data
This commit is contained in:
parent
2cf44393fb
commit
b575de2a55
@ -167,8 +167,11 @@ func (b *Buffer) IsFull() bool {
|
||||
|
||||
// Write implements Write method in io.Writer.
|
||||
func (b *Buffer) Write(data []byte) (int, error) {
|
||||
b.Append(data)
|
||||
return len(data), nil
|
||||
begin := b.Len()
|
||||
b.Value = b.Value[:cap(b.Value)]
|
||||
nBytes := copy(b.Value[begin:], data)
|
||||
b.Value = b.Value[:begin+nBytes]
|
||||
return nBytes, nil
|
||||
}
|
||||
|
||||
// Read implements io.Reader.Read().
|
||||
|
@ -59,6 +59,19 @@ func TestBufferString(t *testing.T) {
|
||||
assert.String(buffer.String()).Equals("Test String")
|
||||
}
|
||||
|
||||
func TestBufferWrite(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
buffer := NewLocalBuffer(24).Clear() // 16 + 8
|
||||
nBytes, err := buffer.Write([]byte("abcd"))
|
||||
assert.Error(err).IsNil()
|
||||
assert.Int(nBytes).Equals(4)
|
||||
nBytes, err = buffer.Write([]byte("abcde"))
|
||||
assert.Error(err).IsNil()
|
||||
assert.Int(nBytes).Equals(4)
|
||||
assert.String(buffer.String()).Equals("abcdabcd")
|
||||
}
|
||||
|
||||
func BenchmarkNewBuffer8192(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
buffer := NewBuffer()
|
||||
|
Loading…
Reference in New Issue
Block a user