From 26366f82bd59628c4cbfd0b68de596a61f3912cb Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 24 Jun 2021 21:30:32 +0100 Subject: [PATCH] env: add skeleton of io.Reader wrapper Signed-off-by: Alejandro Mery --- env/reader.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 env/reader.go diff --git a/env/reader.go b/env/reader.go new file mode 100644 index 0000000..8c56147 --- /dev/null +++ b/env/reader.go @@ -0,0 +1,55 @@ +package env + +import ( + "bytes" + "io" + "os" +) + +const ( + MinimumBufferSize = 32 + DefaultBufferSize = 4096 +) + +type Reader struct { + in io.Reader + buf *bytes.Buffer + out *bytes.Buffer +} + +func (rd *Reader) Read(b []byte) (int, error) { + return rd.in.Read(b) +} + +// +// Constructors +// +func NewReaderSize(in io.Reader, size int) *Reader { + if size < MinimumBufferSize { + size = DefaultBufferSize + } + + return &Reader{ + in: in, + buf: bytes.NewBuffer(make([]byte, 0, size)), + out: bytes.NewBuffer(make([]byte, 0, size)), + } +} + +func NewReader(in io.Reader) *Reader { + return NewReaderSize(in, DefaultBufferSize) +} + +func NewReaderFileSize(filename string, size int) (*Reader, error) { + f, err := os.Open(filename) + if err != nil { + return nil, err + } + + rd := NewReaderSize(f, size) + return rd, nil +} + +func NewReaderFile(filename string) (*Reader, error) { + return NewReaderFileSize(filename, DefaultBufferSize) +}