diff --git a/decoder.go b/decoder.go index 3717c21..91271c3 100644 --- a/decoder.go +++ b/decoder.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "errors" "fmt" + "io" "math" "reflect" "strings" @@ -455,6 +456,23 @@ func (d *Decoder) ReadByteArray() (out []byte, err error) { return } +func readNBytes(n int, reader io.ByteReader) ([]byte, error) { + buf := make([]byte, n) + for i := 0; i < n; i++ { + b, err := reader.ReadByte() + if err != nil { + return nil, err + } + buf[i] = b + } + + return buf, nil +} + +func (d *Decoder) ReadNBytes(n int) (out []byte, err error) { + return readNBytes(n, d) +} + func (d *Decoder) ReadByte() (out byte, err error) { if d.Remaining() < TypeSize.Byte { err = fmt.Errorf("required [1] byte, remaining [%d]", d.Remaining())