diff --git a/binary/byteslice.go b/binary/byteslice.go new file mode 100644 index 000000000..bcbde1b48 --- /dev/null +++ b/binary/byteslice.go @@ -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) +} diff --git a/binary/codec.go b/binary/codec.go index b729440f3..f6825e9b0 100644 --- a/binary/codec.go +++ b/binary/codec.go @@ -18,6 +18,8 @@ const ( TYPE_STRING = Byte(0x10) TYPE_BYTESLICE = Byte(0x11) + + TYPE_TIME = Byte(0x20) ) func GetBinaryType(o Binary) Byte { @@ -38,6 +40,8 @@ func GetBinaryType(o Binary) Byte { case String: return TYPE_STRING case ByteSlice: return TYPE_BYTESLICE + case Time: return TYPE_TIME + default: panic("Unsupported type") } } @@ -59,6 +63,8 @@ func ReadBinary(r io.Reader) Binary { case TYPE_STRING: return ReadString(r) case TYPE_BYTESLICE:return ReadByteSlice(r) + case TYPE_TIME: return ReadTime(r) + default: panic("Unsupported type") } } diff --git a/binary/string.go b/binary/string.go index 98a94cfc4..d84fc3253 100644 --- a/binary/string.go +++ b/binary/string.go @@ -1,10 +1,8 @@ package binary import "io" -import "bytes" type String string -type ByteSlice []byte // String @@ -39,42 +37,3 @@ func ReadString(r io.Reader) String { if err != nil { panic(err) } 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) -} diff --git a/binary/time.go b/binary/time.go new file mode 100644 index 000000000..9d02ea57e --- /dev/null +++ b/binary/time.go @@ -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)} +}