From c8ceaa54253f82db12a1fe3bbc8ec19d41e44cac Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Mon, 7 Nov 2022 18:17:51 +0300 Subject: add ReadBytesToSlice() function --- bitreader.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/bitreader.go b/bitreader.go index d42b8e6..33383e9 100644 --- a/bitreader.go +++ b/bitreader.go @@ -132,6 +132,20 @@ func (reader *ReaderType) TryReadStringLen(length int) string { return text } +// TryReadBytesToSlice is a wrapper function that reads the specified amount of bytes +// from the parameter and puts each byte into a slice and returns this slice. +func (reader *ReaderType) TryReadBytesToSlice(bytes int) []byte { + var out []byte + for i := 0; i < bytes; i++ { + val, err := reader.ReadBytes(1) + if err != nil { + panic(err) + } + out = append(out, byte(val)) + } + return out +} + // SkipBits is a function that increases Reader index // based on given input bits number. // @@ -213,7 +227,7 @@ func (reader *ReaderType) ReadStringLen(length int) (string, error) { } // ReadBits is a function that reads the specified amount of bits -// specified in the parameter and returns the value, error +// from the parameter and returns the value, error // based on the output. It can read up to 64 bits. Returns the read // value in type uint64. // @@ -239,7 +253,7 @@ func (reader *ReaderType) ReadBits(bits int) (uint64, error) { } // ReadBytes is a function that reads the specified amount of bytes -// specified in the parameter and returns the value, error +// from the parameter and returns the value, error // based on the output. It can read up to 8 bytes. Returns the read // value in type uint64. // @@ -255,6 +269,22 @@ func (reader *ReaderType) ReadBytes(bytes int) (uint64, error) { return value, nil } +// ReadBytesToSlice is a function that reads the specified amount of bytes +// from the parameter and puts each byte into a slice and returns this slice. +// +// Returns an error if there are no remaining bytes. +func (reader *ReaderType) ReadBytesToSlice(bytes int) ([]byte, error) { + var out []byte + for i := 0; i < bytes; i++ { + val, err := reader.ReadBytes(1) + if err != nil { + return out, err + } + out = append(out, byte(val)) + } + return out, nil +} + // ReadBool is a function that reads one bit and returns the state, error // based on the output. Returns the read value in a bool format. // -- cgit v1.2.3