ByteQueueStream.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.Crypto.Tls
  6. {
  7. public class ByteQueueStream
  8. : Stream
  9. {
  10. private readonly ByteQueue buffer;
  11. public ByteQueueStream()
  12. {
  13. this.buffer = new ByteQueue();
  14. }
  15. public virtual int Available
  16. {
  17. get { return buffer.Available; }
  18. }
  19. public override bool CanRead
  20. {
  21. get { return true; }
  22. }
  23. public override bool CanSeek
  24. {
  25. get { return false; }
  26. }
  27. public override bool CanWrite
  28. {
  29. get { return true; }
  30. }
  31. public override void Flush()
  32. {
  33. }
  34. public override long Length
  35. {
  36. get { throw new NotSupportedException(); }
  37. }
  38. public virtual int Peek(byte[] buf)
  39. {
  40. int bytesToRead = System.Math.Min(buffer.Available, buf.Length);
  41. buffer.Read(buf, 0, bytesToRead, 0);
  42. return bytesToRead;
  43. }
  44. public override long Position
  45. {
  46. get { throw new NotSupportedException(); }
  47. set { throw new NotSupportedException(); }
  48. }
  49. public virtual int Read(byte[] buf)
  50. {
  51. return Read(buf, 0, buf.Length);
  52. }
  53. public override int Read(byte[] buf, int off, int len)
  54. {
  55. int bytesToRead = System.Math.Min(buffer.Available, len);
  56. buffer.RemoveData(buf, off, bytesToRead, 0);
  57. return bytesToRead;
  58. }
  59. public override int ReadByte()
  60. {
  61. if (buffer.Available == 0)
  62. return -1;
  63. return buffer.RemoveData(1, 0)[0] & 0xFF;
  64. }
  65. public override long Seek(long offset, SeekOrigin origin)
  66. {
  67. throw new NotSupportedException();
  68. }
  69. public override void SetLength(long value)
  70. {
  71. throw new NotSupportedException();
  72. }
  73. public virtual int Skip(int n)
  74. {
  75. int bytesToSkip = System.Math.Min(buffer.Available, n);
  76. buffer.RemoveData(bytesToSkip);
  77. return bytesToSkip;
  78. }
  79. public virtual void Write(byte[] buf)
  80. {
  81. buffer.AddData(buf, 0, buf.Length);
  82. }
  83. public override void Write(byte[] buf, int off, int len)
  84. {
  85. buffer.AddData(buf, off, len);
  86. }
  87. public override void WriteByte(byte b)
  88. {
  89. buffer.AddData(new byte[]{ b }, 0, 1);
  90. }
  91. }
  92. }
  93. #pragma warning restore
  94. #endif