package network import ( "bytes" "encoding/binary" ) type DataWriter struct { buffer *bytes.Buffer } func NewDataWriter() *DataWriter { return &DataWriter{buffer: &bytes.Buffer{}} } func (writer *DataWriter) Byte(b byte) error { err := binary.Write(writer.buffer, binary.BigEndian, b) if err != nil { return err } return nil } func (writer *DataWriter) Bytes(bs []byte) error { err := binary.Write(writer.buffer, binary.BigEndian, bs) if err != nil { return err } return nil } func (writer *DataWriter) Uint8(u uint8) error { err := binary.Write(writer.buffer, binary.BigEndian, u) if err != nil { return err } return nil } func (writer *DataWriter) Uint16(u uint16) error { err := binary.Write(writer.buffer, binary.BigEndian, u) if err != nil { return err } return nil } func (writer *DataWriter) Uint32(u uint32) error { err := binary.Write(writer.buffer, binary.BigEndian, u) if err != nil { return err } return nil } func (writer *DataWriter) Uint64(u uint64) error { err := binary.Write(writer.buffer, binary.BigEndian, u) if err != nil { return err } return nil } func (writer *DataWriter) String(s string) error { err := binary.Write(writer.buffer, binary.BigEndian, []byte(s)) if err != nil { return err } return nil } func (writer *DataWriter) Len() int { return writer.buffer.Len() } func (writer *DataWriter) Cap() int { return writer.buffer.Cap() } func (writer *DataWriter) GetBytes() []byte { return writer.buffer.Bytes() } func (writer *DataWriter) ToReader() *DataReader { return NewDataReader(writer.buffer.Bytes()) }