Advertisement
Guest User

PangyaBinaryWriter

a guest
Nov 12th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace PangyaAPI.BinaryModels
  10. {
  11.     public class PangyaBinaryWriter : BinaryWriter
  12.     {
  13.         public PangyaBinaryWriter(Stream output)
  14.         : base(output)
  15.         {
  16.         }
  17.  
  18.         public PangyaBinaryWriter()
  19.         {
  20.             this.OutStream = new MemoryStream();
  21.         }
  22.  
  23.        
  24.         /// <summary>
  25.         /// Obtém o array de bytes da stream
  26.         /// </summary>
  27.         public byte[] GetBytes()
  28.         {
  29.             if (OutStream is MemoryStream)
  30.                 return ((MemoryStream)OutStream).ToArray();
  31.  
  32.            
  33.             using (var memoryStream = new MemoryStream())
  34.             {
  35.                 memoryStream.GetBuffer();
  36.                 OutStream.CopyTo(memoryStream);
  37.                 return memoryStream.ToArray();
  38.             }
  39.         }
  40.  
  41.         public void Write(byte[][] messages)
  42.         {
  43.             foreach (var message in messages)
  44.             {
  45.                 Write(message);
  46.             }
  47.         }
  48.  
  49.         /// <summary>
  50.         /// Escreve String no Formato Pangya { 00, 00 (tamanho), data (valor)  } e avança a posição atual pelo número de bytes escritos
  51.         /// </summary>
  52.         public void WritePStr(string data)
  53.         {
  54.             var current = GetBytes();
  55.  
  56.             int size = data.Length;
  57.  
  58.             if (size >= short.MaxValue)
  59.                 return;
  60.  
  61.             Write((short)size);
  62.  
  63.             current = GetBytes();
  64.            
  65.             Write(data.ToCharArray());
  66.  
  67.             current = GetBytes();
  68.         }
  69.  
  70.         /// <summary>
  71.         /// Escreve um texto baseado em um tamanho fixo de bytes
  72.         /// </summary>
  73.         /// <param name="message">String à escrever</param>
  74.         /// <param name="length">Tamanho total de bytes</param>
  75.         public void WriteStr(string message, int length)
  76.         {
  77.             Write(Encoding.ASCII.GetBytes(message.PadRight(length, (char)0x00)));
  78.         }
  79.  
  80.         public void WriteByte(byte src)
  81.         {
  82.             Write(src);
  83.         }
  84.         public void WriteBytes(List<byte[]> list)
  85.         {
  86.             list.ForEach(item => Write(item));
  87.         }
  88.         public void WriteUInt16(UInt16 src)
  89.         {
  90.             byte[] result = BitConverter.GetBytes(src);
  91.             Write(result);
  92.         }
  93.         public void WriteInt16(Int16 src)
  94.         {
  95.             byte[] result = BitConverter.GetBytes(src);
  96.             Write(result);
  97.         }
  98.  
  99.         public void WriteUInt32(UInt32 src)
  100.         {
  101.             byte[] result = BitConverter.GetBytes(src);
  102.             Write(result);
  103.         }
  104.         public void WriteUInt64(UInt64 src)
  105.         {
  106.             byte[] result = BitConverter.GetBytes(src);
  107.             Write(result);
  108.         }
  109.  
  110.         public void WriteUInt64(Int64 src)
  111.         {
  112.             byte[] result = BitConverter.GetBytes(src);
  113.             Write(result);
  114.         }
  115.         public void WriteUInt32(Int32 src)
  116.         {
  117.             byte[] result = BitConverter.GetBytes(src);
  118.             Write(result);
  119.         }
  120.         public void WriteDouble(Double src)
  121.         {
  122.             byte[] result = BitConverter.GetBytes(src);
  123.             Write(result);
  124.         }
  125.  
  126.         public void WriteSingle(Single src)
  127.         {
  128.             byte[] result = BitConverter.GetBytes(src);
  129.             Write(result);
  130.         }
  131.        
  132.         #region outros writes
  133.         public void WriteByte(byte? src)
  134.         {
  135.             byte[] result = BitConverter.GetBytes((byte)src);
  136.  
  137.             Write(result);
  138.         }
  139.  
  140.         public void WriteUInt16(UInt16? src)
  141.         {
  142.             byte[] result = BitConverter.GetBytes((UInt16)src);
  143.  
  144.             Write(result);
  145.         }
  146.  
  147.         public void WriteUInt16(Int16? src)
  148.         {
  149.             byte[] result = BitConverter.GetBytes((Int16)src);
  150.  
  151.             Write(result);
  152.         }
  153.         public void WriteUInt32(UInt32? src)
  154.         {
  155.             byte[] result = BitConverter.GetBytes((UInt32)src);
  156.  
  157.             Write(result);
  158.         }
  159.         public void WriteUInt32(Int32? src)
  160.         {
  161.             byte[] result = BitConverter.GetBytes((Int32)src);
  162.             Write(result);
  163.         }
  164.         public void WriteUInt64(UInt64? src)
  165.         {
  166.             byte[] result = BitConverter.GetBytes((UInt64)src);
  167.  
  168.             Write(result);
  169.         }
  170.  
  171.         public void WriteUInt64(Int64? src)
  172.         {
  173.             byte[] result = BitConverter.GetBytes((Int64)src);
  174.  
  175.             Write(result);
  176.         }
  177.      
  178.         public void WriteDouble(Double? src)
  179.         {
  180.             byte[] result = BitConverter.GetBytes((Double)src);
  181.  
  182.             Write(result);
  183.         }
  184.         public void WriteSingle(Single? src)
  185.         {
  186.             byte[] result = BitConverter.GetBytes((Single)src);
  187.             Write(result);
  188.         }
  189.         #endregion
  190.  
  191.         public void Write_Hex(string _bytes)
  192.         {
  193.             _bytes = _bytes.Replace(" ", "");
  194.             for (int i = 0; i < _bytes.Length / 2; i++)
  195.             {
  196.                 WriteByte((byte)Convert.ToByte(_bytes.Substring(i * 2, 2), 16));
  197.             }
  198.         }
  199.  
  200.      
  201.  
  202.  
  203.         public void WriteEmptyBytes(int length)
  204.         {
  205.             Write(new byte[length]);
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement