1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -05:00

remove Buffer.Append method

This commit is contained in:
Darien Raymond 2018-04-19 22:56:55 +02:00
parent 1ad429d1d4
commit 1425fd2ba9
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
17 changed files with 28 additions and 35 deletions

View File

@ -43,7 +43,7 @@ func TestReaderWriter(t *testing.T) {
writePayload := func(writer *Writer, payload ...byte) error { writePayload := func(writer *Writer, payload ...byte) error {
b := buf.New() b := buf.New()
b.Append(payload) b.Write(payload)
return writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)) return writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
} }

View File

@ -36,15 +36,8 @@ func (b *Buffer) Clear() {
} }
// AppendBytes appends one or more bytes to the end of the buffer. // AppendBytes appends one or more bytes to the end of the buffer.
func (b *Buffer) AppendBytes(bytes ...byte) int { func (b *Buffer) AppendBytes(bytes ...byte) (int, error) {
return b.Append(bytes) return b.Write(bytes)
}
// Append appends a byte array to the end of the buffer.
func (b *Buffer) Append(data []byte) int {
nBytes := copy(b.v[b.end:], data)
b.end += int32(nBytes)
return nBytes
} }
// AppendSupplier appends the content of a BytesWriter to the buffer. // AppendSupplier appends the content of a BytesWriter to the buffer.

View File

@ -15,7 +15,7 @@ func TestBufferClear(t *testing.T) {
defer buffer.Release() defer buffer.Release()
payload := "Bytes" payload := "Bytes"
buffer.Append([]byte(payload)) buffer.Write([]byte(payload))
assert(buffer.Len(), Equals, int32(len(payload))) assert(buffer.Len(), Equals, int32(len(payload)))
buffer.Clear() buffer.Clear()

View File

@ -29,7 +29,7 @@ func TestAuthenticationReaderWriter(t *testing.T) {
rand.Read(rawPayload) rand.Read(rawPayload)
payload := buf.NewSize(payloadSize) payload := buf.NewSize(payloadSize)
payload.Append(rawPayload) payload.Write(rawPayload)
assert(payload.Len(), Equals, int32(payloadSize)) assert(payload.Len(), Equals, int32(payloadSize))
cache := buf.NewSize(160 * 1024) cache := buf.NewSize(160 * 1024)
@ -94,11 +94,11 @@ func TestAuthenticationReaderWriterPacket(t *testing.T) {
var payload buf.MultiBuffer var payload buf.MultiBuffer
pb1 := buf.New() pb1 := buf.New()
pb1.Append([]byte("abcd")) pb1.Write([]byte("abcd"))
payload.Append(pb1) payload.Append(pb1)
pb2 := buf.New() pb2 := buf.New()
pb2.Append([]byte("efgh")) pb2.Write([]byte("efgh"))
payload.Append(pb2) payload.Append(pb2)
assert(writer.WriteMultiBuffer(payload), IsNil) assert(writer.WriteMultiBuffer(payload), IsNil)

View File

@ -29,7 +29,7 @@ func TestNormalChunkWriting(t *testing.T) {
[]byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}))) []byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36})))
b := buf.NewSize(256) b := buf.NewSize(256)
b.Append([]byte{11, 12, 13, 14, 15, 16, 17, 18}) b.Write([]byte{11, 12, 13, 14, 15, 16, 17, 18})
err := writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)) err := writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
assert(err, IsNil) assert(err, IsNil)
assert(buffer.Bytes(), Equals, []byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18}) assert(buffer.Bytes(), Equals, []byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18})

View File

@ -232,7 +232,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload []byte) (*buf.Buff
return nil, newError("failed to write address").Base(err) return nil, newError("failed to write address").Base(err)
} }
buffer.Append(payload) buffer.Write(payload)
if !account.Cipher.IsAEAD() && request.Option.Has(RequestOptionOneTimeAuth) { if !account.Cipher.IsAEAD() && request.Option.Has(RequestOptionOneTimeAuth) {
authenticator := NewAuthenticator(HeaderKeyGenerator(account.Key, iv)) authenticator := NewAuthenticator(HeaderKeyGenerator(account.Key, iv))

View File

@ -105,7 +105,7 @@ func TestTCPRequest(t *testing.T) {
runTest := func(request *protocol.RequestHeader, payload []byte) { runTest := func(request *protocol.RequestHeader, payload []byte) {
data := buf.New() data := buf.New()
defer data.Release() defer data.Release()
data.Append(payload) data.Write(payload)
cache := buf.New() cache := buf.New()
defer cache.Release() defer cache.Release()

View File

@ -255,7 +255,7 @@ func writeSocks4Response(writer io.Writer, errCode byte, address net.Address, po
buffer.AppendBytes(0x00, errCode) buffer.AppendBytes(0x00, errCode)
common.Must(buffer.AppendSupplier(serial.WriteUint16(port.Value()))) common.Must(buffer.AppendSupplier(serial.WriteUint16(port.Value())))
buffer.Append(address.IP()) buffer.Write(address.IP())
_, err := writer.Write(buffer.Bytes()) _, err := writer.Write(buffer.Bytes())
return err return err
} }
@ -292,7 +292,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, data []byte) (*buf.Buffer,
b.Release() b.Release()
return nil, err return nil, err
} }
b.Append(data) b.Write(data)
return b, nil return b, nil
} }
@ -358,9 +358,9 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
account := rawAccount.(*Account) account := rawAccount.(*Account)
b.AppendBytes(0x01, byte(len(account.Username))) b.AppendBytes(0x01, byte(len(account.Username)))
b.Append([]byte(account.Username)) b.Write([]byte(account.Username))
b.AppendBytes(byte(len(account.Password))) b.AppendBytes(byte(len(account.Password)))
b.Append([]byte(account.Password)) b.Write([]byte(account.Password))
} }
if _, err := writer.Write(b.Bytes()); err != nil { if _, err := writer.Write(b.Bytes()); err != nil {

View File

@ -24,7 +24,7 @@ func TestUDPEncoding(t *testing.T) {
content := []byte{'a'} content := []byte{'a'}
payload := buf.New() payload := buf.New()
payload.Append(content) payload.Write(content)
assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(payload)), IsNil) assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(payload)), IsNil)
reader := NewUDPReader(b) reader := NewUDPReader(b)

View File

@ -70,8 +70,8 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
defer buffer.Release() defer buffer.Release()
buffer.AppendBytes(Version) buffer.AppendBytes(Version)
buffer.Append(c.requestBodyIV[:]) buffer.Write(c.requestBodyIV[:])
buffer.Append(c.requestBodyKey[:]) buffer.Write(c.requestBodyKey[:])
buffer.AppendBytes(c.responseHeader, byte(header.Option)) buffer.AppendBytes(c.responseHeader, byte(header.Option))
padingLen := dice.Roll(16) padingLen := dice.Roll(16)

View File

@ -42,7 +42,7 @@ func TestRequestSerialization(t *testing.T) {
common.Must(client.EncodeRequestHeader(expectedRequest, buffer)) common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
buffer2 := buf.New() buffer2 := buf.New()
buffer2.Append(buffer.Bytes()) buffer2.Write(buffer.Bytes())
sessionHistory := NewSessionHistory() sessionHistory := NewSessionHistory()
defer common.Close(sessionHistory) defer common.Close(sessionHistory)
@ -95,7 +95,7 @@ func TestInvalidRequest(t *testing.T) {
common.Must(client.EncodeRequestHeader(expectedRequest, buffer)) common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
buffer2 := buf.New() buffer2 := buf.New()
buffer2.Append(buffer.Bytes()) buffer2.Write(buffer.Bytes())
sessionHistory := NewSessionHistory() sessionHistory := NewSessionHistory()
defer common.Close(sessionHistory) defer common.Close(sessionHistory)
@ -135,7 +135,7 @@ func TestMuxRequest(t *testing.T) {
common.Must(client.EncodeRequestHeader(expectedRequest, buffer)) common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
buffer2 := buf.New() buffer2 := buf.New()
buffer2.Append(buffer.Bytes()) buffer2.Write(buffer.Bytes())
sessionHistory := NewSessionHistory() sessionHistory := NewSessionHistory()
defer common.Close(sessionHistory) defer common.Close(sessionHistory)

View File

@ -20,7 +20,7 @@ func TestSRTPWrite(t *testing.T) {
payload := buf.New() payload := buf.New()
payload.AppendSupplier(srtp.Write) payload.AppendSupplier(srtp.Write)
payload.Append(content) payload.Write(content)
assert(payload.Len(), Equals, int32(len(content))+srtp.Size()) assert(payload.Len(), Equals, int32(len(content))+srtp.Size())
} }

View File

@ -20,7 +20,7 @@ func TestUTPWrite(t *testing.T) {
payload := buf.New() payload := buf.New()
payload.AppendSupplier(utp.Write) payload.AppendSupplier(utp.Write)
payload.Append(content) payload.Write(content)
assert(payload.Len(), Equals, int32(len(content))+utp.Size()) assert(payload.Len(), Equals, int32(len(content))+utp.Size())
} }

View File

@ -85,7 +85,7 @@ func (w *KCPPacketWriter) Write(b []byte) (int, error) {
return len(eb), nil return len(eb), nil
})) }))
} else { } else {
bb.Append(b) bb.Write(b)
} }
_, err := w.Writer.Write(bb.Bytes()) _, err := w.Writer.Write(bb.Bytes())

View File

@ -76,7 +76,7 @@ func (s *DataSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []b
return false, nil return false, nil
} }
s.Data().Clear() s.Data().Clear()
s.Data().Append(buf[:dataLen]) s.Data().Write(buf[:dataLen])
buf = buf[dataLen:] buf = buf[dataLen:]
return true, buf return true, buf

View File

@ -24,7 +24,7 @@ func TestDataSegment(t *testing.T) {
Number: 4, Number: 4,
SendingNext: 5, SendingNext: 5,
} }
seg.Data().Append([]byte{'a', 'b', 'c', 'd'}) seg.Data().Write([]byte{'a', 'b', 'c', 'd'})
nBytes := seg.ByteSize() nBytes := seg.ByteSize()
bytes := make([]byte, nBytes) bytes := make([]byte, nBytes)

View File

@ -15,7 +15,7 @@ func TestPipeReadWrite(t *testing.T) {
pReader, pWriter := New() pReader, pWriter := New()
payload := []byte{'a', 'b', 'c', 'd'} payload := []byte{'a', 'b', 'c', 'd'}
b := buf.New() b := buf.New()
b.Append(payload) b.Write(payload)
assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil) assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil)
rb, err := pReader.ReadMultiBuffer() rb, err := pReader.ReadMultiBuffer()
@ -29,7 +29,7 @@ func TestPipeCloseError(t *testing.T) {
pReader, pWriter := New() pReader, pWriter := New()
payload := []byte{'a', 'b', 'c', 'd'} payload := []byte{'a', 'b', 'c', 'd'}
b := buf.New() b := buf.New()
b.Append(payload) b.Write(payload)
assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil) assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil)
pWriter.CloseError() pWriter.CloseError()
@ -44,7 +44,7 @@ func TestPipeClose(t *testing.T) {
pReader, pWriter := New() pReader, pWriter := New()
payload := []byte{'a', 'b', 'c', 'd'} payload := []byte{'a', 'b', 'c', 'd'}
b := buf.New() b := buf.New()
b.Append(payload) b.Write(payload)
assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil) assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil)
assert(pWriter.Close(), IsNil) assert(pWriter.Close(), IsNil)