Skip to content

Commit

Permalink
Add ReadNBytes method to Decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Aug 8, 2021
1 parent bad3c9f commit 012e3d8
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"io"
"math"
"reflect"
"strings"
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 012e3d8

Please sign in to comment.