Browse Source

.

pull/9/head
Jae Kwon 10 years ago
parent
commit
f61fdb5845
4 changed files with 86 additions and 41 deletions
  1. +42
    -0
      binary/byteslice.go
  2. +6
    -0
      binary/codec.go
  3. +0
    -41
      binary/string.go
  4. +38
    -0
      binary/time.go

+ 42
- 0
binary/byteslice.go View 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)
}

+ 6
- 0
binary/codec.go View File

@ -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")
}
}

+ 0
- 41
binary/string.go View File

@ -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)
}

+ 38
- 0
binary/time.go View 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)}
}

Loading…
Cancel
Save