1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-12-31 06:25:32 -05:00

refactor bytes functions

This commit is contained in:
v2ray
2016-06-26 22:34:48 +02:00
parent d12d5b0593
commit 67ac925ee7
16 changed files with 78 additions and 64 deletions

View File

@@ -3,6 +3,8 @@ package alloc
import (
"io"
"github.com/v2ray/v2ray-core/common/serial"
)
const (
@@ -56,6 +58,16 @@ func (b *Buffer) AppendString(s string) *Buffer {
return b
}
func (b *Buffer) AppendUint16(v uint16) *Buffer {
b.Value = serial.Uint16ToBytes(v, b.Value)
return b
}
func (b *Buffer) AppendUint32(v uint32) *Buffer {
b.Value = serial.Uint32ToBytes(v, b.Value)
return b
}
// Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is
// no more than 16 bytes.
func (b *Buffer) Prepend(data []byte) *Buffer {
@@ -64,6 +76,22 @@ func (b *Buffer) Prepend(data []byte) *Buffer {
return b
}
func (b *Buffer) PrependBytes(data ...byte) *Buffer {
return b.Prepend(data)
}
func (b *Buffer) PrependUint16(v uint16) *Buffer {
b.SliceBack(2)
serial.Uint16ToBytes(v, b.Value[:0])
return b
}
func (b *Buffer) PrependUint32(v uint32) *Buffer {
b.SliceBack(4)
serial.Uint32ToBytes(v, b.Value[:0])
return b
}
// Bytes returns the content bytes of this Buffer.
func (b *Buffer) Bytes() []byte {
return b.Value