github.com/searKing/golang/go@v1.2.117/io/example_test.go (about)

     1  // Copyright 2022 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package io_test
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"io"
    11  	"log"
    12  	"os"
    13  	"strings"
    14  
    15  	io_ "github.com/searKing/golang/go/io"
    16  )
    17  
    18  func ExampleSniffReader() {
    19  	r := strings.NewReader("MSG:some io.Reader stream to be read\n")
    20  	sniff := io_.SniffReader(r)
    21  
    22  	// start sniffing
    23  	sniff.Sniff(true)
    24  	// sniff "MSG:"
    25  	printSniff(sniff, len("MSG:"))
    26  	fmt.Printf("\n")
    27  
    28  	// stop sniffing
    29  	sniff.Sniff(false)
    30  	printSniff(sniff, len("MSG:"))
    31  	fmt.Printf("\n")
    32  
    33  	// start sniffing again
    34  	sniff.Sniff(true)
    35  	// sniff "io.Reader"
    36  	printSniff(sniff, len("some"))
    37  	fmt.Printf("\n")
    38  
    39  	// stop sniffing
    40  	sniff.Sniff(false)
    41  	printAll(sniff)
    42  
    43  	// Output:
    44  	// MSG:
    45  	// MSG:
    46  	// some
    47  	// some io.Reader stream to be read
    48  }
    49  func ExampleReplayReader() {
    50  	r := strings.NewReader("MSG:some io.Reader stream to be read")
    51  	replayR := io_.ReplayReader(r)
    52  
    53  	// print "MSG:"
    54  	printSniff(replayR, len("MSG:"))
    55  	fmt.Printf("\n")
    56  
    57  	// start replay
    58  	replayR.Replay()
    59  	// print "MSG:"
    60  	printSniff(replayR, len("MSG:"))
    61  	fmt.Printf("\n")
    62  
    63  	// start replay
    64  	replayR.Replay()
    65  	// print "MSG:"
    66  	printAll(replayR)
    67  	fmt.Printf("\n")
    68  
    69  	// start replay
    70  	replayR.Replay()
    71  	// print "MSG:"
    72  	printAll(replayR)
    73  	fmt.Printf("\n")
    74  
    75  	// Output:
    76  	// MSG:
    77  	// MSG:
    78  	// MSG:some io.Reader stream to be read
    79  	// MSG:some io.Reader stream to be read
    80  }
    81  
    82  func ExampleEOFReader() {
    83  	r := io_.EOFReader()
    84  
    85  	printAll(r)
    86  
    87  	// Output:
    88  	//
    89  }
    90  
    91  func ExampleWatchReader() {
    92  	r := strings.NewReader("some io.Reader stream to be read\n")
    93  	watch := io_.WatchReader(r, io_.WatcherFunc(func(p []byte, n int, err error) (int, error) {
    94  		if err != nil && err != io.EOF {
    95  			log.Fatal(err)
    96  		}
    97  		fmt.Printf("%s", p[:n])
    98  		return n, err
    99  	}))
   100  
   101  	printAll(watch)
   102  
   103  	// Output:
   104  	// some io.Reader stream to be read
   105  	// some io.Reader stream to be read
   106  }
   107  
   108  func ExampleLimitReadSeeker() {
   109  	r := strings.NewReader("some io.Reader stream to be read\n")
   110  	if _, err := io.Copy(os.Stdout, r); err != nil {
   111  		log.Fatal(err)
   112  	}
   113  
   114  	limit := io_.LimitReadSeeker(r, int64(len("some io.Reader stream")))
   115  
   116  	_, _ = limit.Seek(int64(len("some io.Reader ")), io.SeekStart)
   117  	if _, err := io.Copy(os.Stdout, limit); err != nil {
   118  		log.Fatal(err)
   119  	}
   120  	fmt.Printf("\n")
   121  
   122  	_, _ = limit.Seek(int64(-len("stream to be read\n")), io.SeekEnd)
   123  	if _, err := io.Copy(os.Stdout, limit); err != nil {
   124  		log.Fatal(err)
   125  	}
   126  
   127  	// Output:
   128  	// some io.Reader stream to be read
   129  	// stream
   130  	// stream
   131  }
   132  
   133  func ExampleDynamicReadSeeker() {
   134  	r := strings.NewReader("some io.Reader stream to be read\n")
   135  	if _, err := io.Copy(os.Stdout, r); err != nil {
   136  		log.Fatal(err)
   137  	}
   138  
   139  	ignoreOff := len("some ")
   140  
   141  	// dynamic behaves like a reader for "io.Reader stream to be read\n"
   142  	dynamic := io_.DynamicReadSeeker(func(off int64) (reader io.Reader, e error) {
   143  		if off >= 0 {
   144  			off += int64(ignoreOff)
   145  		}
   146  		_, err := r.Seek(off, io.SeekStart)
   147  		// to omit r's io.Seeker
   148  		return io.MultiReader(r), err
   149  	}, r.Size()-int64(ignoreOff))
   150  
   151  	_, _ = dynamic.Seek(int64(len("io.Reader ")), io.SeekStart)
   152  	if _, err := io.Copy(os.Stdout, dynamic); err != nil {
   153  		log.Fatal(err)
   154  	}
   155  
   156  	_, _ = dynamic.Seek(int64(-len("stream to be read\n")), io.SeekEnd)
   157  	if _, err := io.Copy(os.Stdout, dynamic); err != nil {
   158  		log.Fatal(err)
   159  	}
   160  
   161  	// Output:
   162  	// some io.Reader stream to be read
   163  	// stream to be read
   164  	// stream to be read
   165  }
   166  
   167  func ExampleCount() {
   168  	cnt, tailMatch, err := io_.Count(bytes.NewReader([]byte("abcdef")), "b")
   169  	if err != nil {
   170  		log.Fatal(err)
   171  	}
   172  	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
   173  	cnt, tailMatch, err = io_.Count(bytes.NewReader([]byte("abcdef")), "f")
   174  	if err != nil {
   175  		log.Fatal(err)
   176  	}
   177  	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
   178  	cnt, tailMatch, err = io_.Count(bytes.NewReader([]byte("abcdef")), "cd")
   179  	if err != nil {
   180  		log.Fatal(err)
   181  	}
   182  	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
   183  	cnt, tailMatch, err = io_.Count(bytes.NewReader([]byte("abcdef")), "cb")
   184  	if err != nil {
   185  		log.Fatal(err)
   186  	}
   187  	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
   188  
   189  	// Output:
   190  	// cnt: 1, tailMatch: false
   191  	// cnt: 1, tailMatch: true
   192  	// cnt: 1, tailMatch: false
   193  	// cnt: 0, tailMatch: false
   194  }
   195  
   196  func ExampleCountSize() {
   197  	cnt, tailMatch, err := io_.CountSize(bytes.NewReader([]byte("abcdef")), "b", 1)
   198  	if err != nil {
   199  		log.Fatal(err)
   200  	}
   201  	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
   202  	cnt, tailMatch, err = io_.CountSize(bytes.NewReader([]byte("abcdef")), "f", 1)
   203  	if err != nil {
   204  		log.Fatal(err)
   205  	}
   206  	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
   207  	cnt, tailMatch, err = io_.CountSize(bytes.NewReader([]byte("abcdef")), "cd", 1)
   208  	if err != nil {
   209  		log.Fatal(err)
   210  	}
   211  	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
   212  	cnt, tailMatch, err = io_.CountSize(bytes.NewReader([]byte("abcdef")), "cb", 1)
   213  	if err != nil {
   214  		log.Fatal(err)
   215  	}
   216  	fmt.Printf("cnt: %d, tailMatch: %t\n", cnt, tailMatch)
   217  
   218  	// Output:
   219  	// cnt: 1, tailMatch: false
   220  	// cnt: 1, tailMatch: true
   221  	// cnt: 1, tailMatch: false
   222  	// cnt: 0, tailMatch: false
   223  }
   224  
   225  func ExampleCountLines() {
   226  	cnt, err := io_.CountLines(bytes.NewReader([]byte("abc\ndef")))
   227  	if err != nil {
   228  		log.Fatal(err)
   229  	}
   230  	if cnt != 2 {
   231  		log.Fatalf("got %d, want 2", cnt)
   232  	}
   233  	fmt.Printf("cnt: %d\n", cnt)
   234  
   235  	// Output:
   236  	// cnt: 2
   237  }
   238  
   239  func printSniff(r io.Reader, n int) {
   240  	b := make([]byte, n)
   241  	n, err := r.Read(b)
   242  	if err != nil {
   243  		log.Fatal(err)
   244  	}
   245  
   246  	fmt.Printf("%s", b[:n])
   247  }
   248  
   249  func printAll(r io.Reader) {
   250  	b, err := io.ReadAll(r)
   251  	if err != nil {
   252  		log.Fatal(err)
   253  	}
   254  
   255  	fmt.Printf("%s", b)
   256  }