ByteQueueInputStream.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  5. namespace Best.HTTP.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 Read(byte[] buffer, int offset, int count)
  30. {
  31. Streams.ValidateBufferArguments(buffer, offset, count);
  32. int bytesToRead = System.Math.Min(m_buffer.Available, count);
  33. m_buffer.RemoveData(buffer, offset, bytesToRead, 0);
  34. return bytesToRead;
  35. }
  36. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  37. public override int Read(Span<byte> buffer)
  38. {
  39. int bytesToRead = System.Math.Min(m_buffer.Available, buffer.Length);
  40. m_buffer.RemoveData(buffer[..bytesToRead], 0);
  41. return bytesToRead;
  42. }
  43. #endif
  44. public override int ReadByte()
  45. {
  46. if (m_buffer.Available == 0)
  47. return -1;
  48. return m_buffer.RemoveData(1, 0)[0];
  49. }
  50. public long Skip(long n)
  51. {
  52. int bytesToRemove = System.Math.Min((int)n, m_buffer.Available);
  53. m_buffer.RemoveData(bytesToRemove);
  54. return bytesToRemove;
  55. }
  56. public int Available
  57. {
  58. get { return m_buffer.Available; }
  59. }
  60. }
  61. }
  62. #pragma warning restore
  63. #endif