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

     1  package rw
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/sagernet/sing/common"
     7  )
     8  
     9  var ZeroBytes = make([]byte, 1024)
    10  
    11  func WriteByte(writer io.Writer, b byte) error {
    12  	return common.Error(writer.Write([]byte{b}))
    13  }
    14  
    15  func WriteBytes(writer io.Writer, b []byte) error {
    16  	return common.Error(writer.Write(b))
    17  }
    18  
    19  func WriteZero(writer io.Writer) error {
    20  	return WriteByte(writer, 0)
    21  }
    22  
    23  func WriteZeroN(writer io.Writer, size int) error {
    24  	var index int
    25  	for index < size {
    26  		next := index + 1024
    27  		if next > size {
    28  			_, err := writer.Write(ZeroBytes[:size-index])
    29  			return err
    30  		} else {
    31  			_, err := writer.Write(ZeroBytes)
    32  			if err != nil {
    33  				return err
    34  			}
    35  			index = next
    36  		}
    37  	}
    38  	return nil
    39  }
    40  
    41  func WriteString(writer io.Writer, str string) error {
    42  	return WriteBytes(writer, []byte(str))
    43  }