123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 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())
- }
|