BaseInputStream.cs 2.0 KB

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