io

package
v1.2.38 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

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

Functions

func CopyDir

func CopyDir(srcDir, dstDir string, copyMode Mode) error

CopyDir copies or hardlinks the contents of one directory to another, properly handling mods, and soft links

func CopyRegular

func CopyRegular(srcPath, dstPath string, fileInfo os.FileInfo) error

func Count

func Count(r io.Reader, sep string) (cnt int, tailMatch bool, err error)

Count counts the number of non-overlapping instances of sep in r. If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s. tailMatch returns true if tail bytes match sep

Example
package main

import (
	"bytes"
	"fmt"
	"log"

	io_ "github.com/searKing/golang/go/io"
)

func main() {
	cnt, tailMatch, err := io_.Count(bytes.NewReader([]byte("abcdef")), "b")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
	cnt, tailMatch, err = io_.Count(bytes.NewReader([]byte("abcdef")), "f")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
	cnt, tailMatch, err = io_.Count(bytes.NewReader([]byte("abcdef")), "cd")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
	cnt, tailMatch, err = io_.Count(bytes.NewReader([]byte("abcdef")), "cb")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)

}
Output:

cnt: 1, tailMatch: false
cnt: 1, tailMatch: true
cnt: 1, tailMatch: false
cnt: 0, tailMatch: false

func CountAny

func CountAny(r io.Reader, sep string) (cnt int, tailMatch bool, err error)

CountAnySize counts the number of non-overlapping instances of sep in r. If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s.

func CountAnySize

func CountAnySize(r io.Reader, sep string, size int) (cnt int, tailMatch bool, err error)

CountAnySize counts the number of non-overlapping instances of sep in r. If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s.

func CountLines

func CountLines(r io.Reader) (lines int, err error)

CountLines counts the number of lines by \n.

Example
package main

import (
	"bytes"
	"fmt"
	"log"

	io_ "github.com/searKing/golang/go/io"
)

func main() {
	cnt, err := io_.CountLines(bytes.NewReader([]byte("abc\ndef")))
	if err != nil {
		log.Fatal(err)
	}
	if cnt != 2 {
		log.Fatalf("got %d, want 2", cnt)
	}
	fmt.Printf("cnt: %d\n", cnt)

}
Output:

cnt: 2

func CountLinesSize

func CountLinesSize(r io.Reader, size int) (lines int, err error)

CountLinesSize counts the number of lines by \n.

func CountSize

func CountSize(r io.Reader, sep string, size int) (cnt int, tailMatch bool, err error)

CountSize counts the number of non-overlapping instances of sep in r. tailMatch returns true if tail bytes match sep

Example
package main

import (
	"bytes"
	"fmt"
	"log"

	io_ "github.com/searKing/golang/go/io"
)

func main() {
	cnt, tailMatch, err := io_.CountSize(bytes.NewReader([]byte("abcdef")), "b", 1)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
	cnt, tailMatch, err = io_.CountSize(bytes.NewReader([]byte("abcdef")), "f", 1)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
	cnt, tailMatch, err = io_.CountSize(bytes.NewReader([]byte("abcdef")), "cd", 1)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
	cnt, tailMatch, err = io_.CountSize(bytes.NewReader([]byte("abcdef")), "cb", 1)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)

}
Output:

cnt: 1, tailMatch: false
cnt: 1, tailMatch: true
cnt: 1, tailMatch: false
cnt: 0, tailMatch: false

func DynamicReadSeeker

func DynamicReadSeeker(getter func(off int64) (io.Reader, error), size int64) io.ReadSeeker

DynamicReadSeeker returns a ReadSeeker that reads from r got by getter at an offset. The underlying implementation is a *dynamicReadSeeker.

Example
package main

import (
	"io"
	"log"
	"os"
	"strings"

	io_ "github.com/searKing/golang/go/io"
)

func main() {
	r := strings.NewReader("some io.Reader stream to be read\n")
	if _, err := io.Copy(os.Stdout, r); err != nil {
		log.Fatal(err)
	}

	ignoreOff := len("some ")

	// dynamic behaves like a reader for "io.Reader stream to be read\n"
	dynamic := io_.DynamicReadSeeker(func(off int64) (reader io.Reader, e error) {
		if off >= 0 {
			off += int64(ignoreOff)
		}
		_, err := r.Seek(off, io.SeekStart)
		// to omit r's io.Seeker
		return io.MultiReader(r), err
	}, r.Size()-int64(ignoreOff))

	_, _ = dynamic.Seek(int64(len("io.Reader ")), io.SeekStart)
	if _, err := io.Copy(os.Stdout, dynamic); err != nil {
		log.Fatal(err)
	}

	_, _ = dynamic.Seek(int64(-len("stream to be read\n")), io.SeekEnd)
	if _, err := io.Copy(os.Stdout, dynamic); err != nil {
		log.Fatal(err)
	}

}
Output:

some io.Reader stream to be read
stream to be read
stream to be read

func EOFReader

func EOFReader() io.Reader

EOFReader returns a Reader that return EOF anytime.

Example
package main

import (
	"fmt"
	"io"
	"log"

	io_ "github.com/searKing/golang/go/io"
)

func main() {
	r := io_.EOFReader()

	printall := func(r io.Reader) {
		b, err := io.ReadAll(r)
		if err != nil {
			log.Fatal(err)
		}

		fmt.Printf("%s", b)
	}

	printall(r)

}
Output:

func LimitReadSeeker

func LimitReadSeeker(r io.ReadSeeker, n int64) io.ReadSeeker

LimitReadSeeker returns a Reader that reads from r but stops with EOF after n bytes. The underlying implementation is a *LimitedReader.

Example
package main

import (
	"fmt"
	"io"
	"log"
	"os"
	"strings"

	io_ "github.com/searKing/golang/go/io"
)

func main() {
	r := strings.NewReader("some io.Reader stream to be read\n")
	if _, err := io.Copy(os.Stdout, r); err != nil {
		log.Fatal(err)
	}

	limit := io_.LimitReadSeeker(r, int64(len("some io.Reader stream")))

	_, _ = limit.Seek(int64(len("some io.Reader ")), io.SeekStart)
	if _, err := io.Copy(os.Stdout, limit); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("\n")

	_, _ = limit.Seek(int64(-len("stream to be read\n")), io.SeekEnd)
	if _, err := io.Copy(os.Stdout, limit); err != nil {
		log.Fatal(err)
	}

}
Output:

some io.Reader stream to be read
stream
stream

func SeekerLen

func SeekerLen(s io.Seeker) (int64, error)

SeekerLen returns the length of the file and an error, if any.

func SniffCopy

func SniffCopy(dst io.Writer, src io.ReadSeeker) (int64, error)

SniffCopy copies the seekable reader to an io.Writer

func SniffRead

func SniffRead(p []byte, src io.ReadSeeker) (int, error)

SniffRead reads up to len(p) bytes into p.

func WatchReader

func WatchReader(r io.Reader, watcher Watcher) io.Reader

WatchReader returns a Reader that's watch the Read state of the provided input reader.

Example
package main

import (
	"fmt"
	"io"
	"log"
	"strings"

	io_ "github.com/searKing/golang/go/io"
)

func main() {
	r := strings.NewReader("some io.Reader stream to be read\n")
	watch := io_.WatchReader(r, io_.WatcherFunc(func(p []byte, n int, err error) (int, error) {
		if err != nil && err != io.EOF {
			log.Fatal(err)
		}
		fmt.Printf("%s", p[:n])
		return n, err
	}))

	printall := func(r io.Reader) {
		b, err := io.ReadAll(r)
		if err != nil {
			log.Fatal(err)
		}

		fmt.Printf("%s", b)
	}

	printall(watch)

}
Output:

some io.Reader stream to be read
some io.Reader stream to be read

Types

type CloserFunc

type CloserFunc func() error

The CloserFunc type is an adapter to allow the use of ordinary functions as io.Closer handlers. If f is a function with the appropriate signature, CloserFunc(f) is a Handler that calls f.

func (CloserFunc) Close

func (f CloserFunc) Close() error

Close calls f(w, r).

type DynamicReaderFunc

type DynamicReaderFunc func() io.Reader

DynamicReaderFunc returns a Reader that's from the provided input reader getter function.

func (DynamicReaderFunc) Read

func (f DynamicReaderFunc) Read(p []byte) (n int, err error)

type LimitedReadSeeker

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

LimitedReadSeeker A LimitSeekable reads from R but limits the size of the file N bytes. Read returns EOF when N <= 0 or when the underlying R returns EOF.

func (*LimitedReadSeeker) Read

func (l *LimitedReadSeeker) Read(p []byte) (n int, err error)

func (*LimitedReadSeeker) Seek

func (l *LimitedReadSeeker) Seek(offset int64, whence int) (int64, error)

type Mode

type Mode int

Mode indicates whether to use hardlink or copy content

const (
	// Content creates a new file, and copies the content of the file
	Content Mode = iota
	// Hardlink creates a new hardlink to the existing file
	Hardlink
)

type ReadReplayer

type ReadReplayer interface {
	io.Reader
	Replay() ReadReplayer
}

ReadReplayer is the interface that groups the basic Read and Replay methods.

func ReplayReader

func ReplayReader(r io.Reader) ReadReplayer

ReplayReader returns a Reader that allows replay and read from the provided input reader. data is buffered always. buffered data is taken first, if Replay() is called.

Example
package main

import (
	"fmt"
	"io"
	"log"
	"strings"

	io_ "github.com/searKing/golang/go/io"
)

func main() {
	r := strings.NewReader("MSG:some io.Reader stream to be read")
	replayR := io_.ReplayReader(r)

	printReplay := func(r io.Reader, n int) {
		b := make([]byte, n)
		_, err := r.Read(b)
		if err != nil {
			log.Fatal(err)
		}

		fmt.Printf("%s", b)
	}

	printall := func(r io.Reader) {
		b, err := io.ReadAll(r)
		if err != nil {
			log.Fatal(err)
		}

		fmt.Printf("%s", b)
	}

	// print "MSG:"
	printReplay(replayR, len("MSG:"))
	fmt.Printf("\n")

	// start replay
	replayR.Replay()
	// print "MSG:"
	printReplay(replayR, len("MSG:"))
	fmt.Printf("\n")

	// start replay
	replayR.Replay()
	// print "MSG:"
	printall(replayR)
	fmt.Printf("\n")

	// start replay
	replayR.Replay()
	// print "MSG:"
	printall(replayR)
	fmt.Printf("\n")

}
Output:

MSG:
MSG:
MSG:some io.Reader stream to be read
MSG:some io.Reader stream to be read

type ReadSniffer

type ReadSniffer interface {
	io.Reader
	Sniff(sniffing bool) ReadSniffer
}

ReadSniffer is the interface that groups the basic Read and Sniff methods.

func SniffReader

func SniffReader(r io.Reader) ReadSniffer

SniffReader returns a Reader that allows sniff and read from the provided input reader. data is buffered if Sniff(true) is called. buffered data is taken first, if Sniff(false) is called.

Example
package main

import (
	"fmt"
	"io"
	"log"
	"strings"

	io_ "github.com/searKing/golang/go/io"
)

func main() {
	r := strings.NewReader("MSG:some io.Reader stream to be read\n")
	sniff := io_.SniffReader(r)

	printSniff := func(r io.Reader, n int) {
		b := make([]byte, n)
		_, err := r.Read(b)
		if err != nil {
			log.Fatal(err)
		}

		fmt.Printf("%s", b)
	}

	printall := func(r io.Reader) {
		b, err := io.ReadAll(r)
		if err != nil {
			log.Fatal(err)
		}

		fmt.Printf("%s", b)
	}

	// start sniffing
	sniff.Sniff(true)
	// sniff "MSG:"
	printSniff(sniff, len("MSG:"))
	fmt.Printf("\n")

	// stop sniffing
	sniff.Sniff(false)
	printSniff(sniff, len("MSG:"))
	fmt.Printf("\n")

	// start sniffing again
	sniff.Sniff(true)
	// sniff "io.Reader"
	printSniff(sniff, len("some"))
	fmt.Printf("\n")

	// stop sniffing
	sniff.Sniff(false)
	printall(sniff)

}
Output:

MSG:
MSG:
some
some io.Reader stream to be read

type Stater

type Stater interface {
	Stat() (os.FileInfo, error)
}

Stater is the interface that wraps the basic Stat method. Stat returns the FileInfo structure describing file.

type Watcher

type Watcher interface {
	Watch(p []byte, n int, err error) (int, error)
}

type WatcherFunc

type WatcherFunc func(p []byte, n int, err error) (int, error)

func (WatcherFunc) Watch

func (f WatcherFunc) Watch(p []byte, n int, err error) (int, error)

type WriterFunc

type WriterFunc func(p []byte) (n int, err error)

func (WriterFunc) Write

func (f WriterFunc) Write(p []byte) (n int, err error)

type WriterFuncPrintfLike

type WriterFuncPrintfLike func(format string, args ...interface{})

func (WriterFuncPrintfLike) Write

func (f WriterFuncPrintfLike) Write(p []byte) (n int, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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