| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | internal abstract class DetectEofStream : DelegatingStream |
| | | 11 | | { |
| | | 12 | | protected DetectEofStream(Stream stream) |
| | 621 | 13 | | : base(stream) |
| | | 14 | | { |
| | 621 | 15 | | IsAtEof = false; |
| | 621 | 16 | | } |
| | | 17 | | |
| | 18677 | 18 | | protected bool IsAtEof { get; private set; } |
| | | 19 | | |
| | | 20 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo |
| | | 21 | | { |
| | 6495 | 22 | | if (IsAtEof) |
| | | 23 | | { |
| | 0 | 24 | | return 0; |
| | | 25 | | } |
| | 6495 | 26 | | int returnValue = await base.ReadAsync(buffer, offset, count, cancellationToken); |
| | 6495 | 27 | | if (returnValue == 0) |
| | | 28 | | { |
| | 9 | 29 | | ReceivedEof(); |
| | | 30 | | } |
| | 6495 | 31 | | return returnValue; |
| | 6495 | 32 | | } |
| | | 33 | | |
| | | 34 | | public override int ReadByte() |
| | | 35 | | { |
| | 11406 | 36 | | int returnValue = base.ReadByte(); |
| | 11406 | 37 | | if (returnValue == -1) |
| | | 38 | | { |
| | 17 | 39 | | ReceivedEof(); |
| | | 40 | | } |
| | 11406 | 41 | | return returnValue; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 45 | | { |
| | 11513 | 46 | | if (IsAtEof) |
| | | 47 | | { |
| | 8 | 48 | | return 0; |
| | | 49 | | } |
| | 11505 | 50 | | int returnValue = base.Read(buffer, offset, count); |
| | 11505 | 51 | | if (returnValue == 0) |
| | | 52 | | { |
| | 4 | 53 | | ReceivedEof(); |
| | | 54 | | } |
| | 11505 | 55 | | return returnValue; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private void ReceivedEof() |
| | | 59 | | { |
| | 30 | 60 | | if (!IsAtEof) |
| | | 61 | | { |
| | 18 | 62 | | IsAtEof = true; |
| | 18 | 63 | | OnReceivedEof(); |
| | | 64 | | } |
| | 30 | 65 | | } |
| | | 66 | | |
| | | 67 | | protected virtual void OnReceivedEof() |
| | | 68 | | { |
| | 18 | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |