Streams.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 = 4096;
  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. /// <summary>Write the full contents of inStr to the destination stream outStr.</summary>
  49. /// <param name="inStr">Source stream.</param>
  50. /// <param name="outStr">Destination stream.</param>
  51. /// <exception cref="IOException">In case of IO failure.</exception>
  52. public static void PipeAll(Stream inStr, Stream outStr)
  53. {
  54. PipeAll(inStr, outStr, BufferSize);
  55. }
  56. /// <summary>Write the full contents of inStr to the destination stream outStr.</summary>
  57. /// <param name="inStr">Source stream.</param>
  58. /// <param name="outStr">Destination stream.</param>
  59. /// <param name="bufferSize">The size of temporary buffer to use.</param>
  60. /// <exception cref="IOException">In case of IO failure.</exception>
  61. public static void PipeAll(Stream inStr, Stream outStr, int bufferSize)
  62. {
  63. byte[] bs = new byte[bufferSize];
  64. int numRead;
  65. while ((numRead = inStr.Read(bs, 0, bs.Length)) > 0)
  66. {
  67. outStr.Write(bs, 0, numRead);
  68. }
  69. }
  70. /// <summary>
  71. /// Pipe all bytes from <c>inStr</c> to <c>outStr</c>, throwing <c>StreamFlowException</c> if greater
  72. /// than <c>limit</c> bytes in <c>inStr</c>.
  73. /// </summary>
  74. /// <param name="inStr">
  75. /// A <see cref="Stream"/>
  76. /// </param>
  77. /// <param name="limit">
  78. /// A <see cref="System.Int64"/>
  79. /// </param>
  80. /// <param name="outStr">
  81. /// A <see cref="Stream"/>
  82. /// </param>
  83. /// <returns>The number of bytes actually transferred, if not greater than <c>limit</c></returns>
  84. /// <exception cref="IOException"></exception>
  85. public static long PipeAllLimited(Stream inStr, long limit, Stream outStr)
  86. {
  87. byte[] bs = new byte[BufferSize];
  88. long total = 0;
  89. int numRead;
  90. while ((numRead = inStr.Read(bs, 0, bs.Length)) > 0)
  91. {
  92. if ((limit - total) < numRead)
  93. throw new StreamOverflowException("Data Overflow");
  94. total += numRead;
  95. outStr.Write(bs, 0, numRead);
  96. }
  97. return total;
  98. }
  99. /// <exception cref="IOException"></exception>
  100. public static void WriteBufTo(MemoryStream buf, Stream output)
  101. {
  102. buf.WriteTo(output);
  103. }
  104. /// <exception cref="IOException"></exception>
  105. public static int WriteBufTo(MemoryStream buf, byte[] output, int offset)
  106. {
  107. int size = (int)buf.Length;
  108. WriteBufTo(buf, new MemoryStream(output, offset, size));
  109. return size;
  110. }
  111. public static void WriteZeroes(Stream outStr, long count)
  112. {
  113. byte[] zeroes = new byte[BufferSize];
  114. while (count > BufferSize)
  115. {
  116. outStr.Write(zeroes, 0, BufferSize);
  117. count -= BufferSize;
  118. }
  119. outStr.Write(zeroes, 0, (int)count);
  120. }
  121. }
  122. }
  123. #pragma warning restore
  124. #endif