1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-19 13:45:23 +00:00
OpenDiablo2/d2common/d2datautils/stream_writer.go
lord 0218cad717
organize d2common pakage (#716)
* move music path enumerations into d2resource

* move text dictionary (.tbl) loader into d2fileformats sub-package d2tbl

* lint fix, add doc file for d2tbl

* moved data_dictionary.go into d2fileformats sub-package d2txt, added doc file

* added sub-packages d2geom for geometry-related things, and d2path for path-related things

* moved calcstring.go to d2calculation

* move bitmuncher, bitstream, stream reader/writer from d2common into sub-package d2datautils

* fix lint errors in d2datadict loaders (caused by moving stuf around in d2common)

* move size.go into d2geom

* move d2common/cache.go into sub-package d2common/d2cache

* renamed d2debugutil to d2util, moved utility functions from d2common into d2util
2020-09-08 15:58:35 -04:00

69 lines
1.6 KiB
Go

package d2datautils
import "bytes"
const (
byteMask = 0xFF
)
// StreamWriter allows you to create a byte array by streaming in writes of various sizes
type StreamWriter struct {
data *bytes.Buffer
}
// CreateStreamWriter creates a new StreamWriter instance
func CreateStreamWriter() *StreamWriter {
result := &StreamWriter{
data: new(bytes.Buffer),
}
return result
}
// PushByte writes a byte to the stream
func (v *StreamWriter) PushByte(val byte) {
v.data.WriteByte(val)
}
// PushUint16 writes an uint16 word to the stream
func (v *StreamWriter) PushUint16(val uint16) {
for count := 0; count < bytesPerInt16; count++ {
shift := count * bitsPerByte
v.data.WriteByte(byte(val>>shift) & byteMask)
}
}
// PushInt16 writes a int16 word to the stream
func (v *StreamWriter) PushInt16(val int16) {
for count := 0; count < bytesPerInt16; count++ {
shift := count * bitsPerByte
v.data.WriteByte(byte(val>>shift) & byteMask)
}
}
// PushUint32 writes a uint32 dword to the stream
func (v *StreamWriter) PushUint32(val uint32) {
for count := 0; count < bytesPerInt32; count++ {
shift := count * bitsPerByte
v.data.WriteByte(byte(val>>shift) & byteMask)
}
}
// PushUint64 writes a uint64 qword to the stream
func (v *StreamWriter) PushUint64(val uint64) {
for count := 0; count < bytesPerInt64; count++ {
shift := count * bitsPerByte
v.data.WriteByte(byte(val>>shift) & byteMask)
}
}
// PushInt64 writes a uint64 qword to the stream
func (v *StreamWriter) PushInt64(val int64) {
v.PushUint64(uint64(val))
}
// GetBytes returns the the byte slice of the underlying data
func (v *StreamWriter) GetBytes() []byte {
return v.data.Bytes()
}