BaseInputStream.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO
  7. {
  8. public abstract class BaseInputStream : Stream
  9. {
  10. private bool closed;
  11. public sealed override bool CanRead { get { return !closed; } }
  12. public sealed override bool CanSeek { get { return false; } }
  13. public sealed override bool CanWrite { get { return false; } }
  14. #if PORTABLE || NETFX_CORE
  15. protected override void Dispose(bool disposing)
  16. {
  17. if (disposing)
  18. {
  19. closed = true;
  20. }
  21. base.Dispose(disposing);
  22. }
  23. #else
  24. public override void Close()
  25. {
  26. closed = true;
  27. base.Close();
  28. }
  29. #endif
  30. public sealed override void Flush() {}
  31. public sealed override long Length { get { throw new NotSupportedException(); } }
  32. public sealed override long Position
  33. {
  34. get { throw new NotSupportedException(); }
  35. set { throw new NotSupportedException(); }
  36. }
  37. public override int Read(byte[] buffer, int offset, int count)
  38. {
  39. int pos = offset;
  40. try
  41. {
  42. int end = offset + count;
  43. while (pos < end)
  44. {
  45. int b = ReadByte();
  46. if (b == -1) break;
  47. buffer[pos++] = (byte) b;
  48. }
  49. }
  50. catch (IOException)
  51. {
  52. if (pos == offset) throw;
  53. }
  54. return pos - offset;
  55. }
  56. public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
  57. public sealed override void SetLength(long value) { throw new NotSupportedException(); }
  58. public sealed override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
  59. }
  60. }
  61. #pragma warning restore
  62. #endif