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

     1  // Copyright 2020 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
     6  
     7  import (
     8  	"io"
     9  )
    10  
    11  // ReadSniffer is the interface that groups the basic Read and Sniff methods.
    12  type ReadSniffer interface {
    13  	io.Reader
    14  
    15  	// Sniff starts or stops sniffing, restarts if stop and start called one by one
    16  	// true to start sniffing all data unread actually
    17  	// false to return a multi reader with all data sniff buffered and source
    18  	Sniff(sniffing bool) ReadSniffer
    19  }
    20  
    21  // SniffReader returns a Reader that allows sniff and read from
    22  // the provided input reader.
    23  // data is buffered if Sniff(true) is called.
    24  // buffered data is taken first, if Sniff(false) is called.
    25  func SniffReader(r io.Reader) ReadSniffer {
    26  	if r, ok := r.(io.ReadSeeker); ok {
    27  		return newSniffReadSeeker(r)
    28  	}
    29  	return newSniffReader(r)
    30  }