ByteQueueInputStream.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  6. {
  7. public sealed class ByteQueueInputStream
  8. : BaseInputStream
  9. {
  10. private readonly ByteQueue m_buffer;
  11. public ByteQueueInputStream()
  12. {
  13. this.m_buffer = new ByteQueue();
  14. }
  15. public void AddBytes(byte[] buf)
  16. {
  17. m_buffer.AddData(buf, 0, buf.Length);
  18. }
  19. public void AddBytes(byte[] buf, int bufOff, int bufLen)
  20. {
  21. m_buffer.AddData(buf, bufOff, bufLen);
  22. }
  23. public int Peek(byte[] buf)
  24. {
  25. int bytesToRead = System.Math.Min(m_buffer.Available, buf.Length);
  26. m_buffer.Read(buf, 0, bytesToRead, 0);
  27. return bytesToRead;
  28. }
  29. public override int ReadByte()
  30. {
  31. if (m_buffer.Available == 0)
  32. return -1;
  33. return m_buffer.RemoveData(1, 0)[0];
  34. }
  35. public override int Read(byte[] buf, int off, int len)
  36. {
  37. int bytesToRead = System.Math.Min(m_buffer.Available, len);
  38. m_buffer.RemoveData(buf, off, bytesToRead, 0);
  39. return bytesToRead;
  40. }
  41. public long Skip(long n)
  42. {
  43. int bytesToRemove = System.Math.Min((int)n, m_buffer.Available);
  44. m_buffer.RemoveData(bytesToRemove);
  45. return bytesToRemove;
  46. }
  47. public int Available
  48. {
  49. get { return m_buffer.Available; }
  50. }
  51. #if PORTABLE || NETFX_CORE
  52. //protected override void Dispose(bool disposing)
  53. //{
  54. // base.Dispose(disposing);
  55. //}
  56. #else
  57. public override void Close()
  58. {
  59. }
  60. #endif
  61. }
  62. }
  63. #pragma warning restore
  64. #endif