github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/rw/discard.go (about)

     1  package rw
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/sagernet/sing/common/buf"
     7  )
     8  
     9  var Discard io.Writer = discard{}
    10  
    11  type discard struct{}
    12  
    13  var _ io.ReaderFrom = discard{}
    14  
    15  func (discard) Write(p []byte) (int, error) {
    16  	return len(p), nil
    17  }
    18  
    19  func (discard) WriteString(s string) (int, error) {
    20  	return len(s), nil
    21  }
    22  
    23  func (discard) ReadFrom(r io.Reader) (n int64, err error) {
    24  	buffer := buf.Get(buf.BufferSize)
    25  	readSize := 0
    26  	for {
    27  		readSize, err = r.Read(buffer)
    28  		n += int64(readSize)
    29  		if err != nil {
    30  			buf.Put(buffer)
    31  			if err == io.EOF {
    32  				return n, nil
    33  			}
    34  			return
    35  		}
    36  	}
    37  }