diskbufferreader

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2022 License: GPL-3.0 Imports: 6 Imported by: 0

README

Disk Buffer Reader

Are you tired of dealing with how to use a reader more than once? Me too! Rather than teeing a reader, reading it all, or other messy methods, try using your disk!

What it does

Disk buffer reader uses takes everything read from a reader, writes it to a temporary file on disk, and resets the reader to read from the start again. Once you're done reusing your reader, stop the recording function and use it as normal.

Example

func main() {
  readerOriginal := bytes.NewBuffer([]bytes("OneTwoThr")
  dbr, _ := diskBufferReader.NewReader(readerOriginal)
  defer dbr.Close() // Close reader and delete tmp file.
  message := make([]byte, 3)
  for i := 0; i < 3; i++ {
    dbr.Read(message)
    fmt.Println(message)
  }
  for i := 0; i < 3; i++ {
    dbr.Reset() // Reset reader to 0.
    dbr.Read(message)
    fmt.Println(message)
  }
  dbr.Stop() // Stop storing read bytes in tmp file.
}
One
Two
Thr
One
One
One

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DiskBufferReader

type DiskBufferReader struct {
	// contains filtered or unexported fields
}

DiskBufferReader uses an io.Reader and stores read bytes to a tmp file so the reader can be reset to the start.

func New

func New(r io.Reader) (*DiskBufferReader, error)

New takes an io.Reader and creates returns an initialized DiskBufferReader.

func (*DiskBufferReader) Close

func (dbr *DiskBufferReader) Close() error

Close the reader and delete the tmp file.

func (*DiskBufferReader) Read

func (dbr *DiskBufferReader) Read(out []byte) (int, error)

Read from the len(out) bytes from the reader starting at the current index.

func (*DiskBufferReader) Reset

func (dbr *DiskBufferReader) Reset() error

Reset the reader position to the start.

func (*DiskBufferReader) Stop

func (dbr *DiskBufferReader) Stop()

Stop storing the read bytes in the tmp file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL