allow writing primitives to static buffers

This commit is contained in:
aler9
2022-05-11 14:52:20 +02:00
parent ee6d7a87a3
commit c1b10a80be
19 changed files with 662 additions and 786 deletions

View File

@@ -3,9 +3,7 @@ package base
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
)
@@ -117,29 +115,51 @@ func (req *Request) ReadIgnoreFrames(maxPayloadSize int, rb *bufio.Reader) error
}
}
// Write writes a request.
func (req Request) Write(w io.Writer) error {
// WriteSize returns the size of a Request.
func (req Request) WriteSize() int {
n := 0
urStr := req.URL.CloneWithoutCredentials().String()
_, err := w.Write([]byte(string(req.Method) + " " + urStr + " " + rtspProtocol10 + "\r\n"))
if err != nil {
return err
}
n += len([]byte(string(req.Method) + " " + urStr + " " + rtspProtocol10 + "\r\n"))
if len(req.Body) != 0 {
req.Header["Content-Length"] = HeaderValue{strconv.FormatInt(int64(len(req.Body)), 10)}
}
err = req.Header.write(w)
if err != nil {
return err
n += req.Header.writeSize()
n += body(req.Body).writeSize()
return n
}
// WriteTo writes a Request.
func (req Request) WriteTo(buf []byte) (int, error) {
pos := 0
urStr := req.URL.CloneWithoutCredentials().String()
pos += copy(buf[pos:], []byte(string(req.Method)+" "+urStr+" "+rtspProtocol10+"\r\n"))
if len(req.Body) != 0 {
req.Header["Content-Length"] = HeaderValue{strconv.FormatInt(int64(len(req.Body)), 10)}
}
return body(req.Body).write(w)
pos += req.Header.writeTo(buf[pos:])
pos += body(req.Body).writeTo(buf[pos:])
return pos, nil
}
// Write writes a Request.
func (req Request) Write() ([]byte, error) {
buf := make([]byte, req.WriteSize())
_, err := req.WriteTo(buf)
return buf, err
}
// String implements fmt.Stringer.
func (req Request) String() string {
var buf bytes.Buffer
req.Write(&buf)
return buf.String()
buf, _ := req.Write()
return string(buf)
}