Streams.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO
  6. {
  7. public sealed class Streams
  8. {
  9. private const int BufferSize = 512;
  10. private Streams()
  11. {
  12. }
  13. public static void Drain(Stream inStr)
  14. {
  15. byte[] bs = new byte[BufferSize];
  16. while (inStr.Read(bs, 0, bs.Length) > 0)
  17. {
  18. }
  19. }
  20. public static byte[] ReadAll(Stream inStr)
  21. {
  22. MemoryStream buf = new MemoryStream();
  23. PipeAll(inStr, buf);
  24. return buf.ToArray();
  25. }
  26. public static byte[] ReadAllLimited(Stream inStr, int limit)
  27. {
  28. MemoryStream buf = new MemoryStream();
  29. PipeAllLimited(inStr, limit, buf);
  30. return buf.ToArray();
  31. }
  32. public static int ReadFully(Stream inStr, byte[] buf)
  33. {
  34. return ReadFully(inStr, buf, 0, buf.Length);
  35. }
  36. public static int ReadFully(Stream inStr, byte[] buf, int off, int len)
  37. {
  38. int totalRead = 0;
  39. while (totalRead < len)
  40. {
  41. int numRead = inStr.Read(buf, off + totalRead, len - totalRead);
  42. if (numRead < 1)
  43. break;
  44. totalRead += numRead;
  45. }
  46. return totalRead;
  47. }
  48. public static void PipeAll(Stream inStr, Stream outStr)
  49. {
  50. byte[] bs = new byte[BufferSize];
  51. int numRead;
  52. while ((numRead = inStr.Read(bs, 0, bs.Length)) > 0)
  53. {
  54. outStr.Write(bs, 0, numRead);
  55. }
  56. }
  57. /// <summary>
  58. /// Pipe all bytes from <c>inStr</c> to <c>outStr</c>, throwing <c>StreamFlowException</c> if greater
  59. /// than <c>limit</c> bytes in <c>inStr</c>.
  60. /// </summary>
  61. /// <param name="inStr">
  62. /// A <see cref="Stream"/>
  63. /// </param>
  64. /// <param name="limit">
  65. /// A <see cref="System.Int64"/>
  66. /// </param>
  67. /// <param name="outStr">
  68. /// A <see cref="Stream"/>
  69. /// </param>
  70. /// <returns>The number of bytes actually transferred, if not greater than <c>limit</c></returns>
  71. /// <exception cref="IOException"></exception>
  72. public static long PipeAllLimited(Stream inStr, long limit, Stream outStr)
  73. {
  74. byte[] bs = new byte[BufferSize];
  75. long total = 0;
  76. int numRead;
  77. while ((numRead = inStr.Read(bs, 0, bs.Length)) > 0)
  78. {
  79. if ((limit - total) < numRead)
  80. throw new StreamOverflowException("Data Overflow");
  81. total += numRead;
  82. outStr.Write(bs, 0, numRead);
  83. }
  84. return total;
  85. }
  86. /// <exception cref="IOException"></exception>
  87. public static void WriteBufTo(MemoryStream buf, Stream output)
  88. {
  89. buf.WriteTo(output);
  90. }
  91. /// <exception cref="IOException"></exception>
  92. public static int WriteBufTo(MemoryStream buf, byte[] output, int offset)
  93. {
  94. #if PORTABLE || NETFX_CORE
  95. byte[] bytes = buf.ToArray();
  96. bytes.CopyTo(output, offset);
  97. return bytes.Length;
  98. #else
  99. int size = (int)buf.Length;
  100. buf.WriteTo(new MemoryStream(output, offset, size, true));
  101. return size;
  102. #endif
  103. }
  104. public static void WriteZeroes(Stream outStr, long count)
  105. {
  106. byte[] zeroes = new byte[BufferSize];
  107. while (count > BufferSize)
  108. {
  109. outStr.Write(zeroes, 0, BufferSize);
  110. count -= BufferSize;
  111. }
  112. outStr.Write(zeroes, 0, (int)count);
  113. }
  114. }
  115. }
  116. #pragma warning restore
  117. #endif