1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-19 10:26:10 -04:00
v2fly/common/serial/bytes.go

73 lines
1.5 KiB
Go
Raw Normal View History

2015-12-12 07:11:49 -05:00
package serial
2016-01-29 04:57:52 -05:00
import (
"bytes"
)
2015-12-12 07:11:49 -05:00
type Bytes interface {
Bytes() []byte
}
type BytesLiteral []byte
func (this BytesLiteral) Value() []byte {
return []byte(this)
}
2016-01-29 04:57:52 -05:00
func (this BytesLiteral) Equals(another BytesLiteral) bool {
return bytes.Equal(this.Value(), another.Value())
}
2016-01-24 16:28:44 -05:00
func (this BytesLiteral) Uint8Value() uint8 {
return this.Value()[0]
}
func (this BytesLiteral) Uint16() Uint16Literal {
return Uint16Literal(this.Uint16Value())
}
func (this BytesLiteral) Uint16Value() uint16 {
value := this.Value()
return uint16(value[0])<<8 + uint16(value[1])
}
func (this BytesLiteral) IntValue() int {
value := this.Value()
return int(value[0])<<24 + int(value[1])<<16 + int(value[2])<<8 + int(value[3])
}
2016-01-22 11:56:03 -05:00
func (this BytesLiteral) Uint32Value() uint32 {
value := this.Value()
return uint32(value[0])<<24 +
uint32(value[1])<<16 +
uint32(value[2])<<8 +
uint32(value[3])
}
2015-12-12 07:11:49 -05:00
func (this BytesLiteral) Int64Value() int64 {
value := this.Value()
return int64(value[0])<<56 +
int64(value[1])<<48 +
int64(value[2])<<40 +
int64(value[3])<<32 +
int64(value[4])<<24 +
int64(value[5])<<16 +
int64(value[6])<<8 +
int64(value[7])
}
2016-01-18 06:58:04 -05:00
2016-01-21 06:52:09 -05:00
// String returns a string presentation of this ByteLiteral
2016-01-18 06:58:04 -05:00
func (this BytesLiteral) String() string {
return string(this.Value())
}
2016-01-20 11:45:50 -05:00
2016-01-21 06:52:09 -05:00
// All returns true if all bytes in the ByteLiteral are the same as given value.
func (this BytesLiteral) All(v byte) bool {
2016-01-21 07:05:16 -05:00
for _, b := range this {
if b != v {
return false
}
}
return true
}