Guest User

Untitled

a guest
Jul 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. private class BinaryWriter : System.IO.BinaryWriter
  2. {
  3. private bool[] curByte = new bool[8];
  4. private byte curBitIndx = 0;
  5. private System.Collections.BitArray ba;
  6.  
  7. public BinaryWriter(Stream s) : base(s) { }
  8.  
  9. public override void Flush()
  10. {
  11. base.Write(ConvertToByte(curByte));
  12. base.Flush();
  13. }
  14.  
  15. public override void Write(bool value)
  16. {
  17. curByte[curBitIndx] = value;
  18. curBitIndx++;
  19.  
  20. if (curBitIndx == 8)
  21. {
  22. base.Write(ConvertToByte(curByte));
  23. this.curBitIndx = 0;
  24. this.curByte = new bool[8];
  25. }
  26. }
  27.  
  28. public override void Write(byte value)
  29. {
  30. ba = new BitArray(new byte[] { value });
  31. for (byte i = 0; i < 8; i++)
  32. {
  33. this.Write(ba[i]);
  34. }
  35. ba = null;
  36. }
  37.  
  38. public override void Write(byte[] buffer)
  39. {
  40. for (int i = 0; i < buffer.Length; i++)
  41. {
  42. this.Write((byte)buffer[i]);
  43. }
  44. }
  45.  
  46. public override void Write(uint value)
  47. {
  48. ba = new BitArray(BitConverter.GetBytes(value));
  49. for (byte i = 0; i < 32; i++)
  50. {
  51. this.Write(ba[i]);
  52. }
  53. ba = null;
  54. }
  55.  
  56. public override void Write(ulong value)
  57. {
  58. ba = new BitArray(BitConverter.GetBytes(value));
  59. for (byte i = 0; i < 64; i++)
  60. {
  61. this.Write(ba[i]);
  62. }
  63. ba = null;
  64. }
  65.  
  66. public override void Write(ushort value)
  67. {
  68. ba = new BitArray(BitConverter.GetBytes(value));
  69. for (byte i = 0; i < 16; i++)
  70. {
  71. this.Write(ba[i]);
  72. }
  73. ba = null;
  74. }
  75.  
  76. private static byte ConvertToByte(bool[] bools)
  77. {
  78. byte b = 0;
  79.  
  80. byte bitIndex = 0;
  81. for (int i = 0; i < 8; i++)
  82. {
  83. if (bools[i])
  84. {
  85. b |= (byte)(((byte)1) << bitIndex);
  86. }
  87. bitIndex++;
  88. }
  89.  
  90. return b;
  91. }
  92. }
  93.  
  94. private class BinaryReader : System.IO.BinaryReader
  95. {
  96. private bool[] curByte = new bool[8];
  97. private byte curBitIndx = 0;
  98. private BitArray ba;
  99.  
  100. public BinaryReader(Stream s) : base(s)
  101. {
  102. ba = new BitArray(new byte[] { base.ReadByte() });
  103. ba.CopyTo(curByte, 0);
  104. ba = null;
  105. }
  106.  
  107. public override bool ReadBoolean()
  108. {
  109. if (curBitIndx == 8)
  110. {
  111. ba = new BitArray(new byte[] { base.ReadByte() });
  112. ba.CopyTo(curByte, 0);
  113. ba = null;
  114. this.curBitIndx = 0;
  115. }
  116.  
  117. bool b = curByte[curBitIndx];
  118. curBitIndx++;
  119. return b;
  120. }
  121.  
  122. public override byte ReadByte()
  123. {
  124. bool[] bar = new bool[8];
  125. byte i;
  126. for (i = 0; i < 8; i++)
  127. {
  128. bar[i] = this.ReadBoolean();
  129. }
  130.  
  131. byte b = 0;
  132. byte bitIndex = 0;
  133. for (i = 0; i < 8; i++)
  134. {
  135. if (bar[i])
  136. {
  137. b |= (byte)(((byte)1) << bitIndex);
  138. }
  139. bitIndex++;
  140. }
  141. return b;
  142. }
  143.  
  144. public override byte[] ReadBytes(int count)
  145. {
  146. byte[] bytes = new byte[count];
  147. for (int i = 0; i < count; i++)
  148. {
  149. bytes[i] = this.ReadByte();
  150. }
  151. return bytes;
  152. }
  153.  
  154. public override ushort ReadUInt16()
  155. {
  156. byte[] bytes = ReadBytes(2);
  157. return BitConverter.ToUInt16(bytes, 0);
  158. }
  159.  
  160. public override uint ReadUInt32()
  161. {
  162. byte[] bytes = ReadBytes(4);
  163. return BitConverter.ToUInt32(bytes, 0);
  164. }
  165.  
  166. public override ulong ReadUInt64()
  167. {
  168. byte[] bytes = ReadBytes(8);
  169. return BitConverter.ToUInt64(bytes, 0);
  170. }
  171. }
Add Comment
Please, Sign In to add comment