From 012e3d815818b3c06b0ae2480debdb3b88ea3368 Mon Sep 17 00:00:00 2001 From: Slavomir Date: Sun, 8 Aug 2021 17:47:29 +0200 Subject: [PATCH] Add ReadNBytes method to Decoder --- decoder.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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())