ByteQueueStream.cs 2.6 KB

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