Documentation ¶
Overview ¶
Package io2 provides utilities for the "io" and "io/fs" package.
Index ¶
- Variables
- func NopReadCloser(r io.Reader) io.ReadCloser
- func NopReadSeekCloser(r io.ReadSeeker) io.ReadSeekCloser
- func NopReadWriteCloser(rw io.ReadWriter) io.ReadWriteCloser
- func NopWriteCloser(w io.Writer) io.WriteCloser
- type Delegator
- func Delegate(i interface{}) *Delegator
- func DelegateReadCloser(i io.ReadCloser) *Delegator
- func DelegateReadSeekCloser(i io.ReadSeekCloser) *Delegator
- func DelegateReadSeeker(i io.ReadSeeker) *Delegator
- func DelegateReadWriteCloser(i io.ReadWriteCloser) *Delegator
- func DelegateReadWriteSeeker(i io.ReadWriteSeeker) *Delegator
- func DelegateReadWriter(i io.ReadWriter) *Delegator
- func DelegateReader(i io.Reader) *Delegator
- func DelegateWriteCloser(i io.WriteCloser) *Delegator
- func DelegateWriteSeekCloser(i WriteSeekCloser) *Delegator
- func DelegateWriteSeeker(i io.WriteSeeker) *Delegator
- func DelegateWriter(i io.Writer) *Delegator
- type MultiReadCloser
- type MultiReadSeekCloser
- type MultiReadSeeker
- type MultiReader
- type WriteSeekBuffer
- func (b *WriteSeekBuffer) Bytes() []byte
- func (b *WriteSeekBuffer) Close() error
- func (b *WriteSeekBuffer) Len() int
- func (b *WriteSeekBuffer) Offset() int
- func (b *WriteSeekBuffer) Seek(offset int64, whence int) (int64, error)
- func (b *WriteSeekBuffer) Truncate(n int)
- func (b *WriteSeekBuffer) Write(p []byte) (int, error)
- type WriteSeekCloser
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
DelegateWriter returns a Delegator with the provided Write function.
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) Len ¶
func (b *WriteSeekBuffer) Len() int
Len returns the number of bytes of the buffer; b.Len() == len(b.Bytes()).
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.