diskbufferreader

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: MIT Imports: 5 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

Overview

Maybe I need to set the read size? n, err := io.Reader.Read(something); n will not be more that 512 unless I set something (ran into this in archive) Only happens when Stop() is run. Why?? Make sure read time is similar between standard reader and dbr.

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) ReadAt added in v0.1.4

func (dbr *DiskBufferReader) ReadAt(out []byte, offset int64) (int, error)

ReadAt reads len(p) bytes into p starting at offset off in the underlying input source.

func (*DiskBufferReader) Reset

func (dbr *DiskBufferReader) Reset() error

Reset the reader position to the start.

func (*DiskBufferReader) Seek added in v0.1.4

func (dbr *DiskBufferReader) Seek(offset int64, whence int) (int64, error)

Seek sets the offset for the next Read or Write to offset.

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