Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace PangyaAPI.BinaryModels
- {
- public class PangyaBinaryWriter : BinaryWriter
- {
- public PangyaBinaryWriter(Stream output)
- : base(output)
- {
- }
- public PangyaBinaryWriter()
- {
- this.OutStream = new MemoryStream();
- }
- /// <summary>
- /// Obtém o array de bytes da stream
- /// </summary>
- public byte[] GetBytes()
- {
- if (OutStream is MemoryStream)
- return ((MemoryStream)OutStream).ToArray();
- using (var memoryStream = new MemoryStream())
- {
- memoryStream.GetBuffer();
- OutStream.CopyTo(memoryStream);
- return memoryStream.ToArray();
- }
- }
- public void Write(byte[][] messages)
- {
- foreach (var message in messages)
- {
- Write(message);
- }
- }
- /// <summary>
- /// Escreve String no Formato Pangya { 00, 00 (tamanho), data (valor) } e avança a posição atual pelo número de bytes escritos
- /// </summary>
- public void WritePStr(string data)
- {
- var current = GetBytes();
- int size = data.Length;
- if (size >= short.MaxValue)
- return;
- Write((short)size);
- current = GetBytes();
- Write(data.ToCharArray());
- current = GetBytes();
- }
- /// <summary>
- /// Escreve um texto baseado em um tamanho fixo de bytes
- /// </summary>
- /// <param name="message">String à escrever</param>
- /// <param name="length">Tamanho total de bytes</param>
- public void WriteStr(string message, int length)
- {
- Write(Encoding.ASCII.GetBytes(message.PadRight(length, (char)0x00)));
- }
- public void WriteByte(byte src)
- {
- Write(src);
- }
- public void WriteBytes(List<byte[]> list)
- {
- list.ForEach(item => Write(item));
- }
- public void WriteUInt16(UInt16 src)
- {
- byte[] result = BitConverter.GetBytes(src);
- Write(result);
- }
- public void WriteInt16(Int16 src)
- {
- byte[] result = BitConverter.GetBytes(src);
- Write(result);
- }
- public void WriteUInt32(UInt32 src)
- {
- byte[] result = BitConverter.GetBytes(src);
- Write(result);
- }
- public void WriteUInt64(UInt64 src)
- {
- byte[] result = BitConverter.GetBytes(src);
- Write(result);
- }
- public void WriteUInt64(Int64 src)
- {
- byte[] result = BitConverter.GetBytes(src);
- Write(result);
- }
- public void WriteUInt32(Int32 src)
- {
- byte[] result = BitConverter.GetBytes(src);
- Write(result);
- }
- public void WriteDouble(Double src)
- {
- byte[] result = BitConverter.GetBytes(src);
- Write(result);
- }
- public void WriteSingle(Single src)
- {
- byte[] result = BitConverter.GetBytes(src);
- Write(result);
- }
- #region outros writes
- public void WriteByte(byte? src)
- {
- byte[] result = BitConverter.GetBytes((byte)src);
- Write(result);
- }
- public void WriteUInt16(UInt16? src)
- {
- byte[] result = BitConverter.GetBytes((UInt16)src);
- Write(result);
- }
- public void WriteUInt16(Int16? src)
- {
- byte[] result = BitConverter.GetBytes((Int16)src);
- Write(result);
- }
- public void WriteUInt32(UInt32? src)
- {
- byte[] result = BitConverter.GetBytes((UInt32)src);
- Write(result);
- }
- public void WriteUInt32(Int32? src)
- {
- byte[] result = BitConverter.GetBytes((Int32)src);
- Write(result);
- }
- public void WriteUInt64(UInt64? src)
- {
- byte[] result = BitConverter.GetBytes((UInt64)src);
- Write(result);
- }
- public void WriteUInt64(Int64? src)
- {
- byte[] result = BitConverter.GetBytes((Int64)src);
- Write(result);
- }
- public void WriteDouble(Double? src)
- {
- byte[] result = BitConverter.GetBytes((Double)src);
- Write(result);
- }
- public void WriteSingle(Single? src)
- {
- byte[] result = BitConverter.GetBytes((Single)src);
- Write(result);
- }
- #endregion
- public void Write_Hex(string _bytes)
- {
- _bytes = _bytes.Replace(" ", "");
- for (int i = 0; i < _bytes.Length / 2; i++)
- {
- WriteByte((byte)Convert.ToByte(_bytes.Substring(i * 2, 2), 16));
- }
- }
- public void WriteEmptyBytes(int length)
- {
- Write(new byte[length]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement