TableBase.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. public class SerializeUtil
  5. {
  6. public static void WriteInteger(ref Stream input, int val)
  7. {
  8. byte[] b = BitConverter.GetBytes(val);
  9. Array.Reverse(b);
  10. input.Write(b, 0, b.Length);
  11. }
  12. public static void WriteFloat(ref Stream input, float val)
  13. {
  14. byte[] b = BitConverter.GetBytes(val);
  15. Array.Reverse(b);
  16. input.Write(b, 0, b.Length);
  17. }
  18. public static void WriteString(ref Stream input, string str)
  19. {
  20. if (str == null)
  21. {
  22. WriteVariant(ref input, 0);
  23. return;
  24. }
  25. byte[] b = Encoding.UTF8.GetBytes(str);
  26. int count = b.Length;
  27. WriteVariant(ref input, count + 1);
  28. input.Write(b, 0, b.Length);
  29. }
  30. public static int ReadInteger(Stream input)
  31. {
  32. return (input.ReadByte() << 24) + (input.ReadByte() << 16) + (input.ReadByte() << 8) + input.ReadByte();
  33. }
  34. /// <summary>
  35. /// 读取浮点类型值
  36. /// </summary>
  37. /// <param name="input"></param>
  38. /// <returns></returns>
  39. public static float ReadFloat(Stream input)
  40. {
  41. byte[] buffer = new byte[4];
  42. buffer[3] = (byte)input.ReadByte();
  43. buffer[2] = (byte)input.ReadByte();
  44. buffer[1] = (byte)input.ReadByte();
  45. buffer[0] = (byte)input.ReadByte();
  46. return BitConverter.ToSingle(buffer, 0);
  47. }
  48. public static string ReadString(Stream input)
  49. {
  50. int byteCount = ReadVariant(input);
  51. switch (byteCount)
  52. {
  53. case 0:
  54. return null;
  55. case 1:
  56. return "";
  57. }
  58. byteCount--;
  59. byte[] buffer = new byte[byteCount];
  60. ReadFully(input, buffer, 0, byteCount);
  61. return Encoding.UTF8.GetString(buffer, 0, byteCount);
  62. }
  63. static void ReadFully(Stream input, byte[] buffer, int offset, int length)
  64. {
  65. while (length > 0)
  66. {
  67. int count = input.Read(buffer, offset, length);
  68. if (count <= 0) throw new EndOfStreamException();
  69. offset += count;
  70. length -= count;
  71. }
  72. }
  73. public static void WriteVariant(ref Stream input, int val)
  74. {
  75. if (input == null)
  76. return;
  77. if (val <= (Math.Pow(2, 7) - 1))
  78. {
  79. input.WriteByte((byte)val);
  80. }
  81. else if (val <= (Math.Pow(2, 14) - 1))
  82. {
  83. input.WriteByte((byte)((val & 0x7F) | 0x80));
  84. input.WriteByte((byte)(val >> 7));
  85. }
  86. else if (val <= (Math.Pow(2, 21) - 1))
  87. {
  88. input.WriteByte((byte)((val & 0x7F) | 0x80));
  89. input.WriteByte((byte)(((val >> 7) & 0x7F) | 0x80));
  90. input.WriteByte((byte)((val >> 14)));
  91. }
  92. else if (val <= (Math.Pow(2, 28) - 1))
  93. {
  94. input.WriteByte((byte)((val & 0x7F) | 0x80));
  95. input.WriteByte((byte)(((val >> 7) & 0x7F) | 0x80));
  96. input.WriteByte((byte)(((val >> 14) & 0x7F) | 0x80));
  97. input.WriteByte((byte)((val >> 21)));
  98. }
  99. else
  100. {
  101. input.WriteByte((byte)((val & 0x7F) | 0x80));
  102. input.WriteByte((byte)(((val >> 7) & 0x7F) | 0x80));
  103. input.WriteByte((byte)(((val >> 14) & 0x7F) | 0x80));
  104. input.WriteByte((byte)(((val >> 21) & 0x7F) | 0x80));
  105. input.WriteByte((byte)((val >> 28)));
  106. }
  107. }
  108. /// <summary>
  109. /// 读取变长数据【读取的字节是和WriteVariant相对应的,一个数值7位表示】
  110. /// </summary>
  111. /// <param name="input"></param>
  112. /// <returns></returns>
  113. public static int ReadVariant(Stream input)
  114. {
  115. int b = input.ReadByte();
  116. int result = b & 0x7F;
  117. if ((b & 0x80) != 0)
  118. {
  119. b = input.ReadByte();
  120. result |= (b & 0x7F) << 7;
  121. if ((b & 0x80) != 0)
  122. {
  123. b = input.ReadByte();
  124. result |= (b & 0x7F) << 14;
  125. if ((b & 0x80) != 0)
  126. {
  127. b = input.ReadByte();
  128. result |= (b & 0x7F) << 21;
  129. if ((b & 0x80) != 0) result |= (input.ReadByte() & 0x7F) << 28;
  130. }
  131. }
  132. }
  133. return result;
  134. }
  135. }