Package pktline implements reading payloads form pkt-lines and encoding pkt-lines from payloads.




    View Source
    const (
    	// MaxPayloadSize is the maximum payload size of a pkt-line in bytes.
    	MaxPayloadSize = 65516
    	// For compatibility with canonical Git implementation, accept longer pkt-lines
    	OversizePayloadMax = 65520


    View Source
    var (
    	// FlushPkt are the contents of a flush-pkt pkt-line.
    	FlushPkt = []byte{'0', '0', '0', '0'}
    	// Flush is the payload to use with the Encode method to encode a flush-pkt.
    	Flush = []byte{}
    	// FlushString is the payload to use with the EncodeString method to encode a flush-pkt.
    	FlushString = ""
    	// ErrPayloadTooLong is returned by the Encode methods when any of the
    	// provided payloads is bigger than MaxPayloadSize.
    	ErrPayloadTooLong = errors.New("payload is too long")
    View Source
    var ErrInvalidPktLen = errors.New("invalid pkt-len found")

      ErrInvalidPktLen is returned by Err() when an invalid pkt-len is found.


      This section is empty.


      type Encoder

      type Encoder struct {
      	// contains filtered or unexported fields

        An Encoder writes pkt-lines to an output stream.

        0000000c foo 42

        func NewEncoder

        func NewEncoder(w io.Writer) *Encoder

          NewEncoder returns a new encoder that writes to w.

          func (*Encoder) Encode

          func (e *Encoder) Encode(payloads ...[]byte) error

            Encode encodes a pkt-line with the payload specified and write it to the output stream. If several payloads are specified, each of them will get streamed in their own pkt-lines.

            func (*Encoder) EncodeString

            func (e *Encoder) EncodeString(payloads ...string) error

              EncodeString works similarly as Encode but payloads are specified as strings.

              func (*Encoder) Encodef

              func (e *Encoder) Encodef(format string, a ...interface{}) error

                Encodef encodes a single pkt-line with the payload formatted as the format specifier. The rest of the arguments will be used in the format string.

                func (*Encoder) Flush

                func (e *Encoder) Flush() error

                  Flush encodes a flush-pkt to the output stream.

                  type Scanner

                  type Scanner struct {
                  	// contains filtered or unexported fields

                    Scanner provides a convenient interface for reading the payloads of a series of pkt-lines. It takes an io.Reader providing the source, which then can be tokenized through repeated calls to the Scan method.

                    After each Scan call, the Bytes method will return the payload of the corresponding pkt-line on a shared buffer, which will be 65516 bytes or smaller. Flush pkt-lines are represented by empty byte slices.

                    Scanning stops at EOF or the first I/O error.

                    PAYLOAD = "hello\n"
                    PAYLOAD = "world!\n"
                    FLUSH-PKT DETECTED

                    func NewScanner

                    func NewScanner(r io.Reader) *Scanner

                      NewScanner returns a new Scanner to read from r.

                      func (*Scanner) Bytes

                      func (s *Scanner) Bytes() []byte

                        Bytes returns the most recent payload generated by a call to Scan. The underlying array may point to data that will be overwritten by a subsequent call to Scan. It does no allocation.

                        func (*Scanner) Err

                        func (s *Scanner) Err() error

                          Err returns the first error encountered by the Scanner.

                          func (*Scanner) Scan

                          func (s *Scanner) Scan() bool

                            Scan advances the Scanner to the next pkt-line, whose payload will then be available through the Bytes method. Scanning stops at EOF or the first I/O error. After Scan returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF, Err will return nil.