2022-06-04 23:06:40 +00:00
|
|
|
package chunk
|
2022-05-13 22:11:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Chunk2 is a type 2 chunk.
|
|
|
|
// Neither the stream ID nor the
|
|
|
|
// message length is included; this chunk has the same stream ID and
|
|
|
|
// message length as the preceding chunk.
|
|
|
|
type Chunk2 struct {
|
|
|
|
ChunkStreamID byte
|
|
|
|
TimestampDelta uint32
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:57:29 +00:00
|
|
|
// Read reads the chunk.
|
|
|
|
func (c *Chunk2) Read(r io.Reader, chunkBodyLen int) error {
|
|
|
|
header := make([]byte, 4)
|
|
|
|
_, err := r.Read(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ChunkStreamID = header[0] & 0x3F
|
2022-06-04 23:06:40 +00:00
|
|
|
c.TimestampDelta = uint32(header[1])<<16 | uint32(header[2])<<8 | uint32(header[3])
|
2022-05-16 09:57:29 +00:00
|
|
|
|
|
|
|
c.Body = make([]byte, chunkBodyLen)
|
|
|
|
_, err = r.Read(c.Body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-13 22:11:01 +00:00
|
|
|
// Write writes the chunk.
|
|
|
|
func (c Chunk2) Write(w io.Writer) error {
|
|
|
|
header := make([]byte, 4)
|
2022-06-07 19:09:57 +00:00
|
|
|
header[0] = 2<<6 | c.ChunkStreamID
|
2022-05-13 22:11:01 +00:00
|
|
|
header[1] = byte(c.TimestampDelta >> 16)
|
|
|
|
header[2] = byte(c.TimestampDelta >> 8)
|
|
|
|
header[3] = byte(c.TimestampDelta)
|
|
|
|
_, err := w.Write(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(c.Body)
|
|
|
|
return err
|
|
|
|
}
|