OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / tendermint / go-wire / byteslice.go
1 package wire
2
3 import (
4         "io"
5         "math"
6
7         cmn "github.com/tendermint/tmlibs/common"
8 )
9
10 func WriteByteSlice(bz []byte, w io.Writer, n *int, err *error) {
11         WriteVarint(len(bz), w, n, err)
12         WriteTo(bz, w, n, err)
13 }
14
15 func ReadByteSlice(r io.Reader, lmt int, n *int, err *error) []byte {
16         length := ReadVarint(r, n, err)
17         if *err != nil {
18                 return nil
19         }
20         if length < 0 {
21                 *err = ErrBinaryReadInvalidLength
22                 return nil
23         }
24
25         // check that length is less than the maximum slice size
26         if length > math.MaxInt32 {
27                 *err = ErrBinaryReadOverflow
28                 return nil
29         }
30         if lmt != 0 && lmt < cmn.MaxInt(length, *n+length) {
31                 *err = ErrBinaryReadOverflow
32                 return nil
33         }
34
35         /*      if length == 0 {
36                 return nil // zero value for []byte
37         }*/
38
39         buf := make([]byte, length)
40         ReadFull(buf, r, n, err)
41         return buf
42 }