1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-15 20:15:24 +00:00

StreamWriter: replaced slice with bytes.Buffer (#402)

This commit is contained in:
Intyre 2020-06-23 00:56:20 +02:00 committed by GitHub
parent 953271b8e6
commit d17db5b1d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,69 +1,63 @@
package d2common package d2common
import "bytes"
// StreamWriter allows you to create a byte array by streaming in writes of various sizes // StreamWriter allows you to create a byte array by streaming in writes of various sizes
type StreamWriter struct { type StreamWriter struct {
data []byte data *bytes.Buffer
} }
// CreateStreamWriter creates a new StreamWriter instance // CreateStreamWriter creates a new StreamWriter instance
func CreateStreamWriter() *StreamWriter { func CreateStreamWriter() *StreamWriter {
result := &StreamWriter{ result := &StreamWriter{
data: make([]byte, 0), data: new(bytes.Buffer),
} }
return result return result
} }
// PushByte writes a byte to the stream // PushByte writes a byte to the stream
func (v *StreamWriter) PushByte(val byte) { func (v *StreamWriter) PushByte(val byte) {
v.data = append(v.data, val) v.data.WriteByte(val)
} }
// PushUint16 writes an uint16 word to the stream // PushUint16 writes an uint16 word to the stream
func (v *StreamWriter) PushUint16(val uint16) { func (v *StreamWriter) PushUint16(val uint16) {
v.data = append(v.data, byte(val&0xFF)) v.data.WriteByte(byte(val) & 0xFF)
v.data = append(v.data, byte((val>>8)&0xFF)) v.data.WriteByte(byte(val>>8) & 0xFF)
} }
// PushInt16 writes a int16 word to the stream // PushInt16 writes a int16 word to the stream
func (v *StreamWriter) PushInt16(val int16) { func (v *StreamWriter) PushInt16(val int16) {
v.data = append(v.data, byte(val&0xFF)) v.data.WriteByte(byte(val) & 0xFF)
v.data = append(v.data, byte((val>>8)&0xFF)) v.data.WriteByte(byte(val>>8) & 0xFF)
} }
// PushUint32 writes a uint32 dword to the stream // PushUint32 writes a uint32 dword to the stream
func (v *StreamWriter) PushUint32(val uint32) { func (v *StreamWriter) PushUint32(val uint32) {
v.data = append(v.data, byte(val&0xFF)) v.data.WriteByte(byte(val) & 0xFF)
v.data = append(v.data, byte((val>>8)&0xFF)) v.data.WriteByte(byte(val>>8) & 0xFF)
v.data = append(v.data, byte((val>>16)&0xFF)) v.data.WriteByte(byte(val>>16) & 0xFF)
v.data = append(v.data, byte((val>>24)&0xFF)) v.data.WriteByte(byte(val>>24) & 0xFF)
} }
// PushUint64 writes a uint64 qword to the stream // PushUint64 writes a uint64 qword to the stream
func (v *StreamWriter) PushUint64(val uint64) { func (v *StreamWriter) PushUint64(val uint64) {
v.data = append(v.data, byte(val&0xFF)) v.data.WriteByte(byte(val) & 0xFF)
v.data = append(v.data, byte((val>>8)&0xFF)) v.data.WriteByte(byte(val>>8) & 0xFF)
v.data = append(v.data, byte((val>>16)&0xFF)) v.data.WriteByte(byte(val>>16) & 0xFF)
v.data = append(v.data, byte((val>>24)&0xFF)) v.data.WriteByte(byte(val>>24) & 0xFF)
v.data = append(v.data, byte((val>>32)&0xFF)) v.data.WriteByte(byte(val>>32) & 0xFF)
v.data = append(v.data, byte((val>>40)&0xFF)) v.data.WriteByte(byte(val>>40) & 0xFF)
v.data = append(v.data, byte((val>>48)&0xFF)) v.data.WriteByte(byte(val>>48) & 0xFF)
v.data = append(v.data, byte((val>>56)&0xFF)) v.data.WriteByte(byte(val>>56) & 0xFF)
} }
// PushInt64 writes a uint64 qword to the stream // PushInt64 writes a uint64 qword to the stream
func (v *StreamWriter) PushInt64(val int64) { func (v *StreamWriter) PushInt64(val int64) {
result := uint64(val) v.PushUint64(uint64(val))
v.data = append(v.data, byte(result&0xFF))
v.data = append(v.data, byte((result>>8)&0xFF))
v.data = append(v.data, byte((result>>16)&0xFF))
v.data = append(v.data, byte((result>>24)&0xFF))
v.data = append(v.data, byte((result>>32)&0xFF))
v.data = append(v.data, byte((result>>40)&0xFF))
v.data = append(v.data, byte((result>>48)&0xFF))
v.data = append(v.data, byte((result>>56)&0xFF))
} }
// GetBytes returns the the byte slice of the underlying data // GetBytes returns the the byte slice of the underlying data
func (v *StreamWriter) GetBytes() []byte { func (v *StreamWriter) GetBytes() []byte {
return v.data return v.data.Bytes()
} }