ReadHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization;
  4. namespace Mirror.SimpleWeb
  5. {
  6. public static class ReadHelper
  7. {
  8. /// <summary>
  9. /// Reads exactly length from stream
  10. /// </summary>
  11. /// <returns>outOffset + length</returns>
  12. /// <exception cref="ReadHelperException"></exception>
  13. public static int Read(Stream stream, byte[] outBuffer, int outOffset, int length)
  14. {
  15. int received = 0;
  16. try
  17. {
  18. while (received < length)
  19. {
  20. int read = stream.Read(outBuffer, outOffset + received, length - received);
  21. if (read == 0)
  22. {
  23. throw new ReadHelperException("returned 0");
  24. }
  25. received += read;
  26. }
  27. }
  28. catch (AggregateException ae)
  29. {
  30. // if interrupt is called we don't care about Exceptions
  31. Utils.CheckForInterupt();
  32. // rethrow
  33. ae.Handle(e => false);
  34. }
  35. if (received != length)
  36. {
  37. throw new ReadHelperException("returned not equal to length");
  38. }
  39. return outOffset + received;
  40. }
  41. /// <summary>
  42. /// Reads and returns results. This should never throw an exception
  43. /// </summary>
  44. public static bool TryRead(Stream stream, byte[] outBuffer, int outOffset, int length)
  45. {
  46. try
  47. {
  48. Read(stream, outBuffer, outOffset, length);
  49. return true;
  50. }
  51. catch (ReadHelperException)
  52. {
  53. return false;
  54. }
  55. catch (IOException)
  56. {
  57. return false;
  58. }
  59. catch (Exception e)
  60. {
  61. Log.Exception(e);
  62. return false;
  63. }
  64. }
  65. public static int? SafeReadTillMatch(Stream stream, byte[] outBuffer, int outOffset, int maxLength, byte[] endOfHeader)
  66. {
  67. try
  68. {
  69. int read = 0;
  70. int endIndex = 0;
  71. int endLength = endOfHeader.Length;
  72. while (true)
  73. {
  74. int next = stream.ReadByte();
  75. if (next == -1) // closed
  76. return null;
  77. if (read >= maxLength)
  78. {
  79. Log.Error("SafeReadTillMatch exceeded maxLength");
  80. return null;
  81. }
  82. outBuffer[outOffset + read] = (byte)next;
  83. read++;
  84. // if n is match, check n+1 next
  85. if (endOfHeader[endIndex] == next)
  86. {
  87. endIndex++;
  88. // when all is match return with read length
  89. if (endIndex >= endLength)
  90. {
  91. return read;
  92. }
  93. }
  94. // if n not match reset to 0
  95. else
  96. {
  97. endIndex = 0;
  98. }
  99. }
  100. }
  101. catch (IOException e)
  102. {
  103. Log.InfoException(e);
  104. return null;
  105. }
  106. catch (Exception e)
  107. {
  108. Log.Exception(e);
  109. return null;
  110. }
  111. }
  112. }
  113. [Serializable]
  114. public class ReadHelperException : Exception
  115. {
  116. public ReadHelperException(string message) : base(message) {}
  117. protected ReadHelperException(SerializationInfo info, StreamingContext context) : base(info, context)
  118. {
  119. }
  120. }
  121. }