DetectEofStream.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.IO;
  7. abstract class DetectEofStream : DelegatingStream
  8. {
  9. bool isAtEof;
  10. protected DetectEofStream(Stream stream)
  11. : base(stream)
  12. {
  13. this.isAtEof = false;
  14. }
  15. protected bool IsAtEof
  16. {
  17. get { return this.isAtEof; }
  18. }
  19. public override int EndRead(IAsyncResult result)
  20. {
  21. int returnValue = base.EndRead(result);
  22. if (returnValue == 0)
  23. {
  24. ReceivedEof();
  25. }
  26. return returnValue;
  27. }
  28. public override int ReadByte()
  29. {
  30. int returnValue = base.ReadByte();
  31. if (returnValue == -1)
  32. {
  33. ReceivedEof();
  34. }
  35. return returnValue;
  36. }
  37. public override int Read(byte[] buffer, int offset, int count)
  38. {
  39. if (isAtEof)
  40. {
  41. return 0;
  42. }
  43. int returnValue = base.Read(buffer, offset, count);
  44. if (returnValue == 0)
  45. {
  46. ReceivedEof();
  47. }
  48. return returnValue;
  49. }
  50. void ReceivedEof()
  51. {
  52. if (!isAtEof)
  53. {
  54. this.isAtEof = true;
  55. this.OnReceivedEof();
  56. }
  57. }
  58. protected virtual void OnReceivedEof()
  59. {
  60. }
  61. }
  62. }