1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

limit size of written data

This commit is contained in:
Darien Raymond 2016-12-02 14:35:28 +01:00
parent 2cf44393fb
commit b575de2a55
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 18 additions and 2 deletions

View File

@ -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().

View File

@ -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()