]> Sergey Matveev's repositories - btrtrc.git/blob - peer_protocol/int.go
Remove an allocation reading message length
[btrtrc.git] / peer_protocol / int.go
1 package peer_protocol
2
3 import (
4         "bytes"
5         "encoding/binary"
6         "io"
7 )
8
9 type Integer uint32
10
11 func (i *Integer) Read(r io.Reader) error {
12         return binary.Read(r, binary.BigEndian, i)
13 }
14
15 func (i *Integer) UnmarshalBinary(b []byte) error {
16         return i.Read(bytes.NewReader(b))
17 }
18
19 func (i *Integer) ReadFrom(r io.Reader) error {
20         var b [4]byte
21         n, err := r.Read(b[:])
22         if n == 4 {
23                 *i = Integer(binary.BigEndian.Uint32(b[:]))
24                 return nil
25         }
26         if err == nil {
27                 return io.ErrUnexpectedEOF
28         }
29         return err
30 }
31
32 // It's perfectly fine to cast these to an int. TODO: Or is it?
33 func (i Integer) Int() int {
34         return int(i)
35 }
36
37 func (i Integer) Uint64() uint64 {
38         return uint64(i)
39 }
40
41 func (i Integer) Uint32() uint32 {
42         return uint32(i)
43 }