Guest User

Untitled

a guest
Jun 23rd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. /// <summary>
  5. /// Rio MemBinaryWriter 1.0
  6. /// </summary>
  7. public class MemBinaryWriter : IDisposable
  8. {
  9. private byte[] _Buffer;
  10. private int _Pos = 0;
  11.  
  12.  
  13. /// <summary>
  14. /// Current position in buffer
  15. /// </summary>
  16. public int Position
  17. {
  18. get { return _Pos; }
  19. set { _Pos = value; }
  20. }
  21.  
  22. /// <summary>
  23. /// Buffer lenght
  24. /// </summary>
  25. public int Lenght => _Buffer.Length;
  26.  
  27.  
  28.  
  29. /// <summary>
  30. /// Initializate the new instance of MemBinaryWriter with setted buffer capacity
  31. /// </summary>
  32. public MemBinaryWriter(int capacity)
  33. {
  34. _Buffer = new byte[capacity];
  35. }
  36.  
  37.  
  38. /// <summary>
  39. /// Initializate the new instance of MemBinaryWriter with unknown buffer capacity
  40. /// </summary>
  41. public MemBinaryWriter()
  42. {
  43. _Buffer = new byte[1];
  44. }
  45.  
  46.  
  47.  
  48. /// <summary>
  49. /// Get the buffer
  50. /// </summary>
  51. public byte[] GetBuffer()
  52. {
  53. return _Buffer;
  54. }
  55.  
  56.  
  57.  
  58. /// <summary>
  59. /// Write bytes into buffer
  60. /// </summary>
  61. public void WriteBytes(byte[] bytes)
  62. {
  63. if (bytes != null) //Bytes not a null
  64. {
  65. //Check array lenght, and expand if need
  66. if (bytes.Length >= (_Buffer.Length - _Pos))
  67. Array.Resize(ref _Buffer, _Buffer.Length + (bytes.Length - (_Buffer.Length - _Pos)));
  68.  
  69. //Write to buffer
  70. Array.Copy(bytes, 0, _Buffer, _Pos, bytes.Length);
  71.  
  72. //Offseting to the new position
  73. _Pos += bytes.Length;
  74. }
  75. }
  76.  
  77.  
  78.  
  79. /// <summary>
  80. /// Write any value, int16, double, byte, byte array and etc.
  81. /// </summary>
  82. public bool Write<T>(T value)
  83. {
  84. object obj = value;
  85. Type valType = typeof(T);
  86.  
  87. if (valType == typeof(byte[]))
  88. {
  89. WriteBytes((byte[])obj);
  90. return true;
  91. }
  92. else
  93. {
  94. byte[] array = null;
  95. switch (Type.GetTypeCode(valType))
  96. {
  97. case TypeCode.Boolean:
  98. array = BitConverter.GetBytes((bool)obj);
  99. break;
  100. case TypeCode.Char:
  101. array = BitConverter.GetBytes((char)obj);
  102. break;
  103. case TypeCode.Byte:
  104. array = new byte[] { (byte)obj };
  105. break;
  106. case TypeCode.Int16:
  107. array = BitConverter.GetBytes((short)obj);
  108. break;
  109. case TypeCode.UInt16:
  110. array = BitConverter.GetBytes((ushort)obj);
  111. break;
  112. case TypeCode.Int32:
  113. array = BitConverter.GetBytes((int)obj);
  114. break;
  115. case TypeCode.UInt32:
  116. array = BitConverter.GetBytes((uint)obj);
  117. break;
  118. case TypeCode.Int64:
  119. array = BitConverter.GetBytes((long)obj);
  120. break;
  121. case TypeCode.UInt64:
  122. array = BitConverter.GetBytes((ulong)obj);
  123. break;
  124. case TypeCode.Single:
  125. array = BitConverter.GetBytes((float)obj);
  126. break;
  127. case TypeCode.Double:
  128. array = BitConverter.GetBytes((double)obj);
  129. break;
  130.  
  131. default:
  132. return false;
  133. }
  134.  
  135. WriteBytes(array);
  136. return true;
  137. }
  138. }
  139.  
  140.  
  141.  
  142. /// <summary>
  143. /// Write string into buffer
  144. /// </summary>
  145. public void WriteString(string str)
  146. {
  147. if (str != null)
  148. WriteBytes(Encoding.UTF8.GetBytes(str + '\0')); //Plus zero terminator
  149. else
  150. WriteBytes(Encoding.UTF8.GetBytes(string.Empty + '\0')); //String are null!
  151. }
  152.  
  153.  
  154.  
  155. //Fake dispose :), its only for 'using (var bw = new MemBinaryWriter(arr)) {}'
  156. public void Dispose() { }
  157. }
Add Comment
Please, Sign In to add comment