xio

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 8, 2020 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

Package xio provides extensions and hacks on top of io from the stdlib

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnseekableReader means a Seek was attempted from the start or end
	// of an io.Reader that only supports streaming.
	ErrUnseekableReader = errors.New("Unseekable reader")
)

Functions

func Splice added in v0.2.1

func Splice(path string, data []byte, off int64) error

Splice safely embedds new data in the middle of the file at path.

Notes:

  • requires creating a copy of the original file that is len(data) larger
  • allocates a temp-file in os.TempDir

TODO: potential tweaks

  • optional overwrite N bytes at offset
  • optional temp file path to use
  • context for read/write deadlines or cancellation
  • fail early if path is a directory

func SpliceFile added in v0.2.1

func SpliceFile(f *os.File, data []byte, off int64) error

SpliceF safely embedds new data in the middle of the file.

Notes:

  • The provided file descriptor is closed on return via a defer

func SpliceR added in v0.2.1

func SpliceR(path string, r io.Reader, size int, off int64) error

SpliceR is like Splice but copies size bytes from the reader at offset.

func TempFileCopy added in v0.2.1

func TempFileCopy(path, prefix string) (string, error)

TempFileCopy copies path to a new temp file

Types

type ScannableReader

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

ScannableReader wraps poorly behaved io.Reader implementations to make them safe to use with bufio.Scanner. In particular, when the underlying reader returns an error, it ensures that negative values for bytes read don't bubble out. Instead, this will return 0 along with the original error. One source of such bugs returning values directly from golang.org/x/sys/unix.Read.

func NewScannableReader

func NewScannableReader(r io.Reader) *ScannableReader

NewScanableReader wraps an io.Reader in a ScannableReader

func (ScannableReader) Read

func (sr ScannableReader) Read(p []byte) (n int, err error)

Read implements io.Reader and "fixes" poorly behaved implementations during error conditions

type TrackingReader

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

TrackingReader wraps an io.Reader to that tracks the offset as bytes are read. Additionally, it adds a best-effort io.Seeker implementation. For a pure io.Reader that is limited to usage of io.SeeekCurrent and otherwise fails for seeks relative to the start or end of the stream.

Example
text := "some text"
r := NewTrackingReader(reader{strings.NewReader(text)})

if _, err := io.Copy(os.Stdout, r); err != nil {
	log.Fatal(err)
}
if r.Offset() != int64(len(text)) {
	log.Fatalf("offset got %d want %d", r.Offset(), len(text))
}
Output:

some text

func NewTrackingReader

func NewTrackingReader(r io.Reader) *TrackingReader

NewTrackingReader wraps an io.Reader in a TrackingReader

func (TrackingReader) Offset

func (tr TrackingReader) Offset() int64

Offset returns the current position of the reader.

func (*TrackingReader) Read

func (tr *TrackingReader) Read(p []byte) (n int, err error)

Read is a pass-thru to the underlying Read.

func (*TrackingReader) Seek

func (tr *TrackingReader) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker. If the wrapped io.Reader also implements io.Seeker this is a pass-thru. Otherwise, only io.SeekCurrent is supported and ErrUnseekableReader is returned for seeks from start/end.

Jump to

Keyboard shortcuts

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