ReadBytes reads a MessagePack 'bin' object from the reader and returns its value. It may use 'scratch' for storage if it is non-nil.
(scratch []byte)
| 972 | // from the reader and returns its value. It may |
| 973 | // use 'scratch' for storage if it is non-nil. |
| 974 | func (m *Reader) ReadBytes(scratch []byte) (b []byte, err error) { |
| 975 | var p []byte |
| 976 | var lead byte |
| 977 | p, err = m.R.Peek(2) |
| 978 | if err != nil { |
| 979 | return |
| 980 | } |
| 981 | lead = p[0] |
| 982 | var read int64 |
| 983 | switch lead { |
| 984 | case mbin8: |
| 985 | read = int64(p[1]) |
| 986 | m.R.Skip(2) |
| 987 | case mbin16: |
| 988 | p, err = m.R.Next(3) |
| 989 | if err != nil { |
| 990 | return |
| 991 | } |
| 992 | read = int64(big.Uint16(p[1:])) |
| 993 | case mbin32: |
| 994 | p, err = m.R.Next(5) |
| 995 | if err != nil { |
| 996 | return |
| 997 | } |
| 998 | read = int64(big.Uint32(p[1:])) |
| 999 | default: |
| 1000 | err = badPrefix(BinType, lead) |
| 1001 | return |
| 1002 | } |
| 1003 | if int64(cap(scratch)) < read { |
| 1004 | if read > int64(m.GetMaxElements()) { |
| 1005 | err = ErrLimitExceeded |
| 1006 | return |
| 1007 | } |
| 1008 | b = make([]byte, read) |
| 1009 | } else { |
| 1010 | b = scratch[0:read] |
| 1011 | } |
| 1012 | _, err = m.R.ReadFull(b) |
| 1013 | return |
| 1014 | } |
| 1015 | |
| 1016 | // ReadBytesLimit reads a MessagePack 'bin' object |
| 1017 | // from the reader and returns its value. It may |