summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-store/v2/util/io.go
blob: c5135084a526b51dfcf48de6b3f6cc2bd530ad55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package util

import (
	"bytes"
	"io"
)

// ReaderSize defines a reader of known size in bytes.
type ReaderSize interface {
	io.Reader
	Size() int64
}

// ByteReaderSize implements ReaderSize for an in-memory byte-slice.
type ByteReaderSize struct {
	br bytes.Reader
	sz int64
}

// NewByteReaderSize returns a new ByteReaderSize instance reset to slice b.
func NewByteReaderSize(b []byte) *ByteReaderSize {
	rs := new(ByteReaderSize)
	rs.Reset(b)
	return rs
}

// Read implements io.Reader.
func (rs *ByteReaderSize) Read(b []byte) (int, error) {
	return rs.br.Read(b)
}

// Size implements ReaderSize.
func (rs *ByteReaderSize) Size() int64 {
	return rs.sz
}

// Reset resets the ReaderSize to be reading from b.
func (rs *ByteReaderSize) Reset(b []byte) {
	rs.br.Reset(b)
	rs.sz = int64(len(b))
}