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

     1  package rw
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/sagernet/sing/common"
     7  )
     8  
     9  func Skip(reader io.Reader) error {
    10  	return SkipN(reader, 1)
    11  }
    12  
    13  func SkipN(reader io.Reader, size int) error {
    14  	return common.Error(io.CopyN(Discard, reader, int64(size)))
    15  }
    16  
    17  func ReadByte(reader io.Reader) (byte, error) {
    18  	if br, isBr := reader.(io.ByteReader); isBr {
    19  		return br.ReadByte()
    20  	}
    21  	var b [1]byte
    22  	if err := common.Error(io.ReadFull(reader, b[:])); err != nil {
    23  		return 0, err
    24  	}
    25  	return b[0], nil
    26  }
    27  
    28  func ReadBytes(reader io.Reader, size int) ([]byte, error) {
    29  	b := make([]byte, size)
    30  	if err := common.Error(io.ReadFull(reader, b)); err != nil {
    31  		return nil, err
    32  	}
    33  	return b, nil
    34  }
    35  
    36  func ReadString(reader io.Reader, size int) (string, error) {
    37  	b, err := ReadBytes(reader, size)
    38  	if err != nil {
    39  		return "", err
    40  	}
    41  	return string(b), nil
    42  }