liveread

package module
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2023 License: MIT Imports: 7 Imported by: 1

README

Go Live Read

donation link

A module that enables you to concurrently read a file and do something with the data.

Installation


  go get github.com/AspieSoft/go-liveread

Usage


import (
  "github.com/AspieSoft/go-liveread"
)

func main(){
  reader, err := liveread.Read("my/file.txt")
  
  // read/peek at the first 4 bytes
  b, err := reader.Get(0, 4)

  // read/peek at 4 bytes starting at index 12
  b, err := reader.Get(12, 4)

  // peek at the next 10 bytes
  b, err := reader.Peek(10)

  // discard the bytes that you no longer need to read
  discarded, err := reader.Discard(10)

  // read until you reach a specific byte
  b, err := reader.ReadBytes('\n')

  // similar to ReadBytes, but does not discard automatically
  // also allows you to specify a starting point
  b, err := reader.PeekBytes(0, '\n')

  // read to a byte array instead of just a single byte
  b, err := reader.ReadToBytes([]byte("/>"))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ERROR_EOF error = io.EOF

ERROR_EOF is an alias of `io.EOF`

View Source
var ERROR_EOF_Save error = errors.New("EOF_Save")

ERROR_EOF_Save is only returned when using restore points

in most cases, you will need to use `io.EOF` to check for EOF errors

Functions

This section is empty.

Types

type Reader

type Reader[T maxLenOpts] struct {
	Reader *bufio.Reader
	File   *os.File
	// contains filtered or unexported fields
}

func Read

func Read[T maxLenOpts](path string, bufSize ...uint) (*Reader[T], error)

Read a file and concurrently peek at the bytes before it is done being read

@path: the file path you wish to read

@bufSize: the number of bytes to read at a time (min = 10, max = maxLen/4)

Type {uint8|uint16}: this sets the maxLen for how many bytes can be read to the queue before the overflow is used

func (*Reader[T]) Buffered

func (reader *Reader[T]) Buffered() int

Buffered returns the number of bytes that can be read from the current buffer

func (*Reader[T]) DelFirstSave added in v1.1.0

func (reader *Reader[T]) DelFirstSave()

Experimental: DelFirstSave removes the first restore point and may shift the one being used

func (*Reader[T]) DelSave added in v1.1.0

func (reader *Reader[T]) DelSave()

Experimental: DelSave removes the last restore point and may shift the one being used

func (*Reader[T]) Discard

func (reader *Reader[T]) Discard(size uint) (discarded uint, err error)

Discard removes a number of bytes from memory when they no longer are needed

if there are no more bytes to remove, a smaller size will be returned with an io.EOF error

func (*Reader[T]) Get

func (reader *Reader[T]) Get(start uint, size uint) ([]byte, error)

Get reads bytes from a starting point and grabs a number of bytes equal to the size

if there are no more bytes to read, a smaller size will be returned with an io.EOF error

this method does not discard anything after a read

func (*Reader[T]) Peek

func (reader *Reader[T]) Peek(size uint) ([]byte, error)

Peek reads a number of bytes equal to the size

if there are no more bytes to read, a smaller size will be returned with an io.EOF error

this method does not discard anything after a read

func (*Reader[T]) PeekByte

func (reader *Reader[T]) PeekByte(index uint) (byte, error)

PeekByte returns a single byte at an index

if there are no more bytes to read, a smaller size will be returned with an io.EOF error

this mothod is similar the Peek method, it does not discard anything after a read

func (*Reader[T]) PeekBytes

func (reader *Reader[T]) PeekBytes(start uint, delim byte) ([]byte, error)

PeekBytes returns bytes until the first occurrence of delim

this mothod is similar the Peek method, it does not discard anything after a read

func (*Reader[T]) PeekToBytes

func (reader *Reader[T]) PeekToBytes(start uint, delim []byte) ([]byte, error)

PeekToBytes is similar to PeekBytes, but it allows you to detect a []byte instead

func (*Reader[T]) ReadByte

func (reader *Reader[T]) ReadByte() (byte, error)

ReadByte returns a single byte at an index

func (*Reader[T]) ReadBytes

func (reader *Reader[T]) ReadBytes(delim byte) ([]byte, error)

ReadBytes returns bytes until the first occurrence of delim

func (*Reader[T]) ReadToBytes

func (reader *Reader[T]) ReadToBytes(delim []byte) ([]byte, error)

ReadToBytes is similar to ReadBytes, but it allows you to detect a []byte instead

func (*Reader[T]) Restore added in v1.1.0

func (reader *Reader[T]) Restore(offset ...uint)

Experimental: Restore tells the reading functions to start pulling from the restore point chosen

@offset[0] says which save index to pill from, starting with the last index at 1

if @offset[0] == 0, the save index will be turned off, and the restore point will continue to pull from the original buffer

if @offset[1] == 1, then when the restore point runs out of bytes, the previous restore point, or main reader, will start to get used

if @offset[1] == 2, then it will act like `0`, but will return nil in place of an error

by default (@offset[1] == 0), if the restore point runs out of bytes, it will return the error `liveread.ERROR_EOF_Save`

note: this method will append a restore reader to a list, and running the UnRestore method will revert back to the previous restore reader if one was active

func (*Reader[T]) RestoreReset added in v1.1.0

func (reader *Reader[T]) RestoreReset(offset ...uint)

Experimental: RestoreReset will reset the last restore point without creating a new one

func (*Reader[T]) RestoreResetFirst added in v1.2.1

func (reader *Reader[T]) RestoreResetFirst(offset ...uint)

Experimental: RestoreReset will reset the first restore point without creating a new one

func (*Reader[T]) Save added in v1.1.0

func (reader *Reader[T]) Save()

Experimental: Save creates a new restore point to bring back the next set of discarded bytes

note: not all reading methods will support save points, and some may continue to use the file reader

func (*Reader[T]) Seek

func (reader *Reader[T]) Seek(offset int64, whence int) (ret int64, err error)

Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any. The behavior of Seek on a file opened with O_APPEND is not specified.

func (*Reader[T]) Size

func (reader *Reader[T]) Size() int

Size returns the size of the underlying buffer in bytes

func (*Reader[T]) UnRestore added in v1.1.0

func (reader *Reader[T]) UnRestore()

Experimental: UnRestore removes a restore reader from the end of the list

func (*Reader[T]) UnRestoreFirst added in v1.1.0

func (reader *Reader[T]) UnRestoreFirst()

Experimental: UnRestoreFirst removes a restore reader from the start of list

Jump to

Keyboard shortcuts

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