mirror of
https://github.com/aler9/gortsplib
synced 2025-10-04 23:02:45 +08:00
allow writing primitives to static buffers
This commit is contained in:
@@ -2,9 +2,7 @@ package base
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -203,36 +201,65 @@ func (res *Response) ReadIgnoreFrames(maxPayloadSize int, rb *bufio.Reader) erro
|
||||
}
|
||||
}
|
||||
|
||||
// Write writes a Response.
|
||||
func (res Response) Write(w io.Writer) error {
|
||||
// WriteSize returns the size of a Response.
|
||||
func (res Response) WriteSize() int {
|
||||
n := 0
|
||||
|
||||
if res.StatusMessage == "" {
|
||||
if status, ok := statusMessages[res.StatusCode]; ok {
|
||||
res.StatusMessage = status
|
||||
}
|
||||
}
|
||||
|
||||
_, err := w.Write([]byte(rtspProtocol10 + " " +
|
||||
n += len([]byte(rtspProtocol10 + " " +
|
||||
strconv.FormatInt(int64(res.StatusCode), 10) + " " +
|
||||
res.StatusMessage + "\r\n"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(res.Body) != 0 {
|
||||
res.Header["Content-Length"] = HeaderValue{strconv.FormatInt(int64(len(res.Body)), 10)}
|
||||
}
|
||||
|
||||
err = res.Header.write(w)
|
||||
if err != nil {
|
||||
return err
|
||||
n += res.Header.writeSize()
|
||||
|
||||
n += body(res.Body).writeSize()
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
// WriteTo writes a Response.
|
||||
func (res Response) WriteTo(buf []byte) (int, error) {
|
||||
if res.StatusMessage == "" {
|
||||
if status, ok := statusMessages[res.StatusCode]; ok {
|
||||
res.StatusMessage = status
|
||||
}
|
||||
}
|
||||
|
||||
return body(res.Body).write(w)
|
||||
pos := 0
|
||||
|
||||
pos += copy(buf[pos:], []byte(rtspProtocol10+" "+
|
||||
strconv.FormatInt(int64(res.StatusCode), 10)+" "+
|
||||
res.StatusMessage+"\r\n"))
|
||||
|
||||
if len(res.Body) != 0 {
|
||||
res.Header["Content-Length"] = HeaderValue{strconv.FormatInt(int64(len(res.Body)), 10)}
|
||||
}
|
||||
|
||||
pos += res.Header.writeTo(buf[pos:])
|
||||
|
||||
pos += body(res.Body).writeTo(buf[pos:])
|
||||
|
||||
return pos, nil
|
||||
}
|
||||
|
||||
// Write writes a Response.
|
||||
func (res Response) Write() ([]byte, error) {
|
||||
buf := make([]byte, res.WriteSize())
|
||||
_, err := res.WriteTo(buf)
|
||||
return buf, err
|
||||
}
|
||||
|
||||
// String implements fmt.Stringer.
|
||||
func (res Response) String() string {
|
||||
var buf bytes.Buffer
|
||||
res.Write(&buf)
|
||||
return buf.String()
|
||||
buf, _ := res.Write()
|
||||
return string(buf)
|
||||
}
|
||||
|
Reference in New Issue
Block a user