mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-17 07:01:20 +00:00
.
This commit is contained in:
42
binary/byteslice.go
Normal file
42
binary/byteslice.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package binary
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
type ByteSlice []byte
|
||||||
|
|
||||||
|
func (self ByteSlice) Equals(other Binary) bool {
|
||||||
|
if o, ok := other.(ByteSlice); ok {
|
||||||
|
return bytes.Equal(self, o)
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self ByteSlice) Less(other Binary) bool {
|
||||||
|
if o, ok := other.(ByteSlice); ok {
|
||||||
|
return bytes.Compare(self, o) < 0 // -1 if a < b
|
||||||
|
} else {
|
||||||
|
panic("Cannot compare unequal types")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self ByteSlice) ByteSize() int {
|
||||||
|
return len(self)+4
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self ByteSlice) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
|
var n_ int
|
||||||
|
_, err = UInt32(len(self)).WriteTo(w)
|
||||||
|
if err != nil { return n, err }
|
||||||
|
n_, err = w.Write([]byte(self))
|
||||||
|
return int64(n_+4), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadByteSlice(r io.Reader) ByteSlice {
|
||||||
|
length := int(ReadUInt32(r))
|
||||||
|
bytes := make([]byte, length)
|
||||||
|
_, err := io.ReadFull(r, bytes)
|
||||||
|
if err != nil { panic(err) }
|
||||||
|
return ByteSlice(bytes)
|
||||||
|
}
|
@ -18,6 +18,8 @@ const (
|
|||||||
|
|
||||||
TYPE_STRING = Byte(0x10)
|
TYPE_STRING = Byte(0x10)
|
||||||
TYPE_BYTESLICE = Byte(0x11)
|
TYPE_BYTESLICE = Byte(0x11)
|
||||||
|
|
||||||
|
TYPE_TIME = Byte(0x20)
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetBinaryType(o Binary) Byte {
|
func GetBinaryType(o Binary) Byte {
|
||||||
@ -38,6 +40,8 @@ func GetBinaryType(o Binary) Byte {
|
|||||||
case String: return TYPE_STRING
|
case String: return TYPE_STRING
|
||||||
case ByteSlice: return TYPE_BYTESLICE
|
case ByteSlice: return TYPE_BYTESLICE
|
||||||
|
|
||||||
|
case Time: return TYPE_TIME
|
||||||
|
|
||||||
default: panic("Unsupported type")
|
default: panic("Unsupported type")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,6 +63,8 @@ func ReadBinary(r io.Reader) Binary {
|
|||||||
case TYPE_STRING: return ReadString(r)
|
case TYPE_STRING: return ReadString(r)
|
||||||
case TYPE_BYTESLICE:return ReadByteSlice(r)
|
case TYPE_BYTESLICE:return ReadByteSlice(r)
|
||||||
|
|
||||||
|
case TYPE_TIME: return ReadTime(r)
|
||||||
|
|
||||||
default: panic("Unsupported type")
|
default: panic("Unsupported type")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
package binary
|
package binary
|
||||||
|
|
||||||
import "io"
|
import "io"
|
||||||
import "bytes"
|
|
||||||
|
|
||||||
type String string
|
type String string
|
||||||
type ByteSlice []byte
|
|
||||||
|
|
||||||
// String
|
// String
|
||||||
|
|
||||||
@ -39,42 +37,3 @@ func ReadString(r io.Reader) String {
|
|||||||
if err != nil { panic(err) }
|
if err != nil { panic(err) }
|
||||||
return String(bytes)
|
return String(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ByteSlice
|
|
||||||
|
|
||||||
func (self ByteSlice) Equals(other Binary) bool {
|
|
||||||
if o, ok := other.(ByteSlice); ok {
|
|
||||||
return bytes.Equal(self, o)
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self ByteSlice) Less(other Binary) bool {
|
|
||||||
if o, ok := other.(ByteSlice); ok {
|
|
||||||
return bytes.Compare(self, o) < 0 // -1 if a < b
|
|
||||||
} else {
|
|
||||||
panic("Cannot compare unequal types")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self ByteSlice) ByteSize() int {
|
|
||||||
return len(self)+4
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self ByteSlice) WriteTo(w io.Writer) (n int64, err error) {
|
|
||||||
var n_ int
|
|
||||||
_, err = UInt32(len(self)).WriteTo(w)
|
|
||||||
if err != nil { return n, err }
|
|
||||||
n_, err = w.Write([]byte(self))
|
|
||||||
return int64(n_+4), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadByteSlice(r io.Reader) ByteSlice {
|
|
||||||
length := int(ReadUInt32(r))
|
|
||||||
bytes := make([]byte, length)
|
|
||||||
_, err := io.ReadFull(r, bytes)
|
|
||||||
if err != nil { panic(err) }
|
|
||||||
return ByteSlice(bytes)
|
|
||||||
}
|
|
||||||
|
38
binary/time.go
Normal file
38
binary/time.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package binary
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Time struct {
|
||||||
|
time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self Time) Equals(other Binary) bool {
|
||||||
|
if o, ok := other.(Time); ok {
|
||||||
|
return self.Equal(o.Time)
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self Time) Less(other Binary) bool {
|
||||||
|
if o, ok := other.(Time); ok {
|
||||||
|
return self.Before(o.Time)
|
||||||
|
} else {
|
||||||
|
panic("Cannot compare unequal types")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self Time) ByteSize() int {
|
||||||
|
return 8
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self Time) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
return Int64(self.Unix()).WriteTo(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadTime(r io.Reader) Time {
|
||||||
|
return Time{time.Unix(int64(ReadInt64(r)), 0)}
|
||||||
|
}
|
Reference in New Issue
Block a user