2019-11-06 22:12:15 -05:00
|
|
|
package common
|
2019-10-25 16:02:31 -04:00
|
|
|
|
|
|
|
// StreamWriter allows you to create a byte array by streaming in writes of various sizes
|
|
|
|
type StreamWriter struct {
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateStreamWriter creates a new StreamWriter instance
|
|
|
|
func CreateStreamWriter() *StreamWriter {
|
|
|
|
result := &StreamWriter{
|
|
|
|
data: make([]byte, 0),
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushByte writes a byte to the stream
|
|
|
|
func (v *StreamWriter) PushByte(val byte) {
|
|
|
|
v.data = append(v.data, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushWord writes an uint16 word to the stream
|
|
|
|
func (v *StreamWriter) PushWord(val uint16) {
|
|
|
|
v.data = append(v.data, byte(val&0xFF))
|
|
|
|
v.data = append(v.data, byte((val>>8)&0xFF))
|
|
|
|
}
|
|
|
|
|
2019-10-25 17:09:07 -04:00
|
|
|
// PushSWord writes a int16 word to the stream
|
|
|
|
func (v *StreamWriter) PushSWord(val int16) {
|
|
|
|
v.data = append(v.data, byte(val&0xFF))
|
|
|
|
v.data = append(v.data, byte((val>>8)&0xFF))
|
|
|
|
}
|
|
|
|
|
2019-10-25 16:02:31 -04:00
|
|
|
// PushDword writes a uint32 dword to the stream
|
|
|
|
func (v *StreamWriter) PushDword(val uint32) {
|
|
|
|
v.data = append(v.data, byte(val&0xFF))
|
|
|
|
v.data = append(v.data, byte((val>>8)&0xFF))
|
|
|
|
v.data = append(v.data, byte((val>>16)&0xFF))
|
|
|
|
v.data = append(v.data, byte((val>>24)&0xFF))
|
|
|
|
}
|
|
|
|
|
2019-10-25 17:09:07 -04:00
|
|
|
// GetBytes returns the the byte slice of the underlying data
|
2019-10-25 16:02:31 -04:00
|
|
|
func (v *StreamWriter) GetBytes() []byte {
|
|
|
|
return v.data
|
|
|
|
}
|