Documentation
¶
Index ¶
- Variables
- func CloseIf(c interface{}) error
- func CopyDir(srcDir, dstDir string, copyMode Mode) error
- func CopyRegular(srcPath, dstPath string, fileInfo os.FileInfo) error
- func Count(r io.Reader, sep string) (cnt int, tailMatch bool, err error)
- func CountAny(r io.Reader, sep string) (cnt int, tailMatch bool, err error)
- func CountAnySize(r io.Reader, sep string, size int) (cnt int, tailMatch bool, err error)
- func CountLines(r io.Reader) (lines int, err error)
- func CountLinesSize(r io.Reader, size int) (lines int, err error)
- func CountSize(r io.Reader, sep string, size int) (cnt int, tailMatch bool, err error)
- func DynamicReadSeeker(getter func(off int64) (io.Reader, error), size int64) io.ReadSeeker
- func EOFReader() io.Reader
- func LimitReadSeeker(r io.ReadSeeker, n int64) io.ReadSeeker
- func SeekerLen(s io.Seeker) (int64, error)
- func SniffCopy(dst io.Writer, src io.ReadSeeker) (int64, error)
- func SniffRead(p []byte, src io.ReadSeeker) (int, error)
- func WatchReader(r io.Reader, watcher Watcher) io.Reader
- type CloserFunc
- type DynamicReaderFunc
- type LimitedReadSeeker
- type Mode
- type ReadReplayer
- type ReadSniffer
- type Stater
- type Watcher
- type WatcherFunc
- type WriterFunc
- type WriterFuncPrintfLike
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotImplemented = errors.New("not implemented")
Functions ¶
func CloseIf ¶ added in v1.2.47
func CloseIf(c interface{}) error
CloseIf call Close() if arg implements io.Closer
func CopyDir ¶
CopyDir copies or hardlinks the contents of one directory to another, properly handling mods, and soft links
func Count ¶
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 ¶
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 ¶
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 ¶
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 ¶
CountLinesSize counts the number of lines by \n.
func CountSize ¶
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 ¶
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 ¶
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)
}
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 SniffRead ¶
func SniffRead(p []byte, src io.ReadSeeker) (int, error)
SniffRead reads up to len(p) bytes into p.
func WatchReader ¶
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.
type DynamicReaderFunc ¶
DynamicReaderFunc returns a Reader that's from the provided input reader getter function.
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.
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 ¶
Stater is the interface that wraps the basic Stat method. Stat returns the FileInfo structure describing file.
type WatcherFunc ¶
type WriterFunc ¶
type WriterFuncPrintfLike ¶
type WriterFuncPrintfLike func(format string, args ...interface{})