io2

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2022 License: MIT Imports: 7 Imported by: 2

README

github.com/jarxorg/io2

PkgGoDev Report Card Coverage Status

Go "io" package utilities.

Delegator

Delegator implements io.Reader, io.Writer, io.Seeker, io.Closer. Delegator can override the I/O functions that is useful for unit tests.

package main

import (
  "bytes"
  "errors"
  "fmt"
  "io/ioutil"

  "github.com/jarxorg/io2"
)

func main() {
  org := bytes.NewReader([]byte(`original`))

  r := io2.DelegateReader(org)
  r.ReadFunc = func(p []byte) (int, error) {
    return 0, errors.New("custom")
  }

  var err error
  _, err = ioutil.ReadAll(r)
  fmt.Printf("Error: %v\n", err)

  // Output: Error: custom
}
No-op Closer
// NopReadCloser returns a ReadCloser with a no-op Close method wrapping the provided interface.
// This function like io.NopCloser(io.Reader).
func NopReadCloser(r io.Reader) io.ReadCloser {
  return DelegateReader(r)
}

// NopReadWriteCloser returns a ReadWriteCloser with a no-op Close method wrapping the provided interface.
func NopReadWriteCloser(rw io.ReadWriter) io.ReadWriteCloser {
  return DelegateReadWriter(rw)
}

// NopReadSeekCloser returns a ReadSeekCloser with a no-op Close method wrapping the provided interface.
func NopReadSeekCloser(r io.ReadSeeker) io.ReadSeekCloser {
  return DelegateReadSeeker(r)
}

// NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the provided interface.
func NopWriteCloser(w io.Writer) io.WriteCloser {
  return DelegateWriter(w)
}

WriteSeeker

WriteSeekBuffer implements io.Writer, io.Seeker and io.Closer. NewWriteSeekBuffer(capacity int) returns the buffer.

// WriteSeekCloser is the interface that groups the basic Write, Seek and Close methods.
type WriteSeekCloser interface {
  io.Writer
  io.Seeker
  io.Closer
}
package main

import (
  "fmt"
  "io"

  "github.com/jarxorg/io2"
)

func main() {
  o := io2.NewWriteSeekBuffer(16)
  o.Write([]byte(`Hello!`))
  o.Truncate(o.Len() - 1)
  o.Write([]byte(` world!`))

  fmt.Println(string(o.Bytes()))

  o.Seek(-1, io.SeekEnd)
  o.Write([]byte(`?`))

  fmt.Println(string(o.Bytes()))

  // Output:
  // Hello world!
  // Hello world?
}

Multi Readers

io2 provides MultiReadCloser, MultiReadSeeker, MultiReadSeekCloser.

package main

import (
  "fmt"
  "io"
  "io/ioutil"
  "strings"

  "github.com/jarxorg/io2"
)

func main() {
  r, _ := io2.NewMultiReadSeeker(
    strings.NewReader("Hello !"),
    strings.NewReader(" World"),
  )

  r.Seek(5, io.SeekStart)
  p, _ := ioutil.ReadAll(r)
  fmt.Println(string(p))

  r.Seek(-5, io.SeekEnd)
  p, _ = ioutil.ReadAll(r)
  fmt.Println(string(p))

  // Output:
  // ! World
  // World
}

Documentation

Overview

Package io2 provides utilities for the "io" and "io/fs" package.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotImplemented "not implemented"
	ErrNotImplemented = errors.New("not implemented")
)

Functions

func NopReadCloser added in v0.3.0

func NopReadCloser(r io.Reader) io.ReadCloser

NopReadCloser returns a ReadCloser with a no-op Close method wrapping the provided interface. This function like io.NopCloser(io.Reader).

func NopReadSeekCloser

func NopReadSeekCloser(r io.ReadSeeker) io.ReadSeekCloser

NopReadSeekCloser returns a ReadSeekCloser with a no-op Close method wrapping the provided interface.

func NopReadWriteCloser

func NopReadWriteCloser(rw io.ReadWriter) io.ReadWriteCloser

NopReadWriteCloser returns a ReadWriteCloser with a no-op Close method wrapping the provided interface.

func NopWriteCloser

func NopWriteCloser(w io.Writer) io.WriteCloser

NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the provided interface.

Types

type Delegator

type Delegator struct {
	ReadFunc  func(p []byte) (n int, err error)
	WriteFunc func(p []byte) (n int, err error)
	SeekFunc  func(offset int64, whence int) (int64, error)
	CloseFunc func() error
}

Delegator implements Reader, Writer, Seeker, Closer.

func Delegate

func Delegate(i interface{}) *Delegator

Delegate returns a Delegator with the provided io interfaces (io.Reader, io.Seeker, io.Writer, io.Closer).

func DelegateReadCloser

func DelegateReadCloser(i io.ReadCloser) *Delegator

DelegateReadCloser returns a Delegator with the provided Read and Close functions.

func DelegateReadSeekCloser

func DelegateReadSeekCloser(i io.ReadSeekCloser) *Delegator

DelegateReadSeekCloser returns a Delegator with the provided Read, Seek and Close functions.

func DelegateReadSeeker

func DelegateReadSeeker(i io.ReadSeeker) *Delegator

DelegateReadSeeker returns a Delegator with the provided Read and Seek functions.

func DelegateReadWriteCloser

func DelegateReadWriteCloser(i io.ReadWriteCloser) *Delegator

DelegateReadWriteCloser returns a Delegator with the provided Read, Write and Close functions.

func DelegateReadWriteSeeker

func DelegateReadWriteSeeker(i io.ReadWriteSeeker) *Delegator

DelegateReadWriteSeeker returns a Delegator with the provided Read, Write and Seek functions.

func DelegateReadWriter

func DelegateReadWriter(i io.ReadWriter) *Delegator

DelegateReadWriter returns a Delegator with the provided Read and Write functions.

func DelegateReader

func DelegateReader(i io.Reader) *Delegator

DelegateReader returns a Delegator with the provided Read function.

Example
package main

import (
	"bytes"
	"errors"
	"fmt"
	"io/ioutil"

	"github.com/jarxorg/io2"
)

func main() {
	org := bytes.NewReader([]byte(`original`))

	r := io2.DelegateReader(org)
	r.ReadFunc = func(p []byte) (int, error) {
		return 0, errors.New("custom")
	}

	var err error
	_, err = ioutil.ReadAll(r)
	fmt.Printf("Error: %v\n", err)

}
Output:

Error: custom

func DelegateWriteCloser

func DelegateWriteCloser(i io.WriteCloser) *Delegator

DelegateWriteCloser returns a Delegator with the provided Write and Close functions.

func DelegateWriteSeekCloser

func DelegateWriteSeekCloser(i WriteSeekCloser) *Delegator

DelegateWriteSeekCloser returns a Delegator with the provided Write, Seek and Close functions.

func DelegateWriteSeeker

func DelegateWriteSeeker(i io.WriteSeeker) *Delegator

DelegateWriteSeeker returns a Delegator with the provided Write and Seek functions.

func DelegateWriter

func DelegateWriter(i io.Writer) *Delegator

DelegateWriter returns a Delegator with the provided Write function.

func (*Delegator) Close

func (d *Delegator) Close() error

Close calls CloseFunc().

func (*Delegator) Read

func (d *Delegator) Read(p []byte) (int, error)

Read calls ReadFunc(p).

func (*Delegator) Seek

func (d *Delegator) Seek(offset int64, whence int) (int64, error)

Seek calls SeekFunc(offset, whence).

func (*Delegator) Write

func (d *Delegator) Write(p []byte) (int, error)

Write calls WriteFunc(p).

type MultiReadCloser added in v0.6.0

type MultiReadCloser interface {
	MultiReader
	io.Closer
}

MultiReadCloser is the interface that groups the MultiReader and Close methods.

func NewMultiReadCloser added in v0.7.0

func NewMultiReadCloser(rs ...io.ReadCloser) MultiReadCloser

NewMultiReadCloser create a ReaderCloser that's the logical concatenation of the provided input readers.

type MultiReadSeekCloser added in v0.6.0

type MultiReadSeekCloser interface {
	MultiReadSeeker
	io.Closer
}

MultiReadSeekCloser is the interface that groups the MultiReadSeeker and Close methods.

func NewMultiFileReader added in v0.7.1

func NewMultiFileReader(filenames ...string) (MultiReadSeekCloser, error)

func NewMultiReadSeekCloser added in v0.7.0

func NewMultiReadSeekCloser(rs ...io.ReadSeekCloser) (MultiReadSeekCloser, error)

NewMultiReadSeekCloser creates a ReadSeekCloser that's the logical concatenation of the provided input readers.

type MultiReadSeeker added in v0.6.0

type MultiReadSeeker interface {
	MultiReader
	io.Seeker
	// SeekReader sets the offset of multiple readers.
	SeekReader(current int) (int64, error)
}

MultiReadCloser is the interface that groups the MultiReader, Seek and SeekReader methods.

Example
package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"strings"

	"github.com/jarxorg/io2"
)

func main() {
	r, _ := io2.NewMultiReadSeeker(
		strings.NewReader("Hello !"),
		strings.NewReader(" World"),
	)

	r.Seek(5, io.SeekStart)
	p, _ := ioutil.ReadAll(r)
	fmt.Println(string(p))

	r.Seek(-5, io.SeekEnd)
	p, _ = ioutil.ReadAll(r)
	fmt.Println(string(p))

}
Output:

! World
World

func NewMultiReadSeeker added in v0.7.0

func NewMultiReadSeeker(rs ...io.ReadSeeker) (MultiReadSeeker, error)

NewMultiReadSeeker creates a ReadSeeker that's the logical concatenation of the provided input readers.

func NewMultiStringReader added in v0.7.1

func NewMultiStringReader(strs ...string) MultiReadSeeker

type MultiReader added in v0.7.0

type MultiReader interface {
	io.Reader
	// Current returns a current index of multiple readers. The current starts 0.
	Current() int
}

MultiReader represents a multiple reader.

func NewMultiReader added in v0.7.0

func NewMultiReader(rs ...io.Reader) MultiReader

NewMultiReader creates a Reader that's the logical concatenation of the provided input readers.

type WriteSeekBuffer

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

WriteSeekBuffer implements io.WriteSeeker that using in-memory byte buffer.

func NewWriteSeekBuffer

func NewWriteSeekBuffer(capacity int) *WriteSeekBuffer

NewWriteSeekBuffer returns an WriteSeekBuffer with the initial capacity.

Example
package main

import (
	"fmt"
	"io"

	"github.com/jarxorg/io2"
)

func main() {
	o := io2.NewWriteSeekBuffer(0)
	o.Write([]byte(`Hello!`))
	o.Truncate(o.Len() - 1)
	o.Write([]byte(` world!`))

	fmt.Println(string(o.Bytes()))

	o.Seek(-1, io.SeekEnd)
	o.Write([]byte(`?`))

	fmt.Println(string(o.Bytes()))

}
Output:

Hello world!
Hello world?

func NewWriteSeekBufferBytes

func NewWriteSeekBufferBytes(buf []byte) *WriteSeekBuffer

NewWriteSeekBufferBytes returns an WriteSeekBuffer with the initial buffer.

func (*WriteSeekBuffer) Bytes

func (b *WriteSeekBuffer) Bytes() []byte

Bytes returns a slice of length b.Len() of the buffer.

func (*WriteSeekBuffer) Close

func (b *WriteSeekBuffer) Close() error

Close calls b.Truncate(0).

func (*WriteSeekBuffer) Len

func (b *WriteSeekBuffer) Len() int

Len returns the number of bytes of the buffer; b.Len() == len(b.Bytes()).

func (*WriteSeekBuffer) Offset

func (b *WriteSeekBuffer) Offset() int

Offset returns the offset.

func (*WriteSeekBuffer) Seek

func (b *WriteSeekBuffer) Seek(offset int64, whence int) (int64, error)

Seek sets the offset for the next Write to offset, interpreted according to whence:

SeekStart means relative to the start of the file,
SeekCurrent means relative to the current offset,
SeekEnd means relative to the end.

Seek returns the new offset relative to the start of the file and an error, if any.

func (*WriteSeekBuffer) Truncate

func (b *WriteSeekBuffer) Truncate(n int)

Truncate changes the size of the buffer with offset.

func (*WriteSeekBuffer) Write

func (b *WriteSeekBuffer) Write(p []byte) (int, error)

Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil.

type WriteSeekCloser

type WriteSeekCloser interface {
	io.Writer
	io.Seeker
	io.Closer
}

WriteSeekCloser is the interface that groups the basic Write, Seek and Close methods.

Jump to

Keyboard shortcuts

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