| | | 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; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | using CoreWCF.Diagnostics; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Dispatcher |
| | | 12 | | { |
| | | 13 | | public class MessageBodyStream : Stream |
| | | 14 | | { |
| | | 15 | | private readonly Message _message; |
| | | 16 | | private XmlDictionaryReader _reader; |
| | | 17 | | private long _position; |
| | | 18 | | private readonly string _wrapperName, _wrapperNs; |
| | | 19 | | private readonly string _elementName, _elementNs; |
| | | 20 | | private readonly bool _isRequest; |
| | 17 | 21 | | public MessageBodyStream(Message message, string wrapperName, string wrapperNs, string elementName, string eleme |
| | | 22 | | { |
| | 17 | 23 | | _message = message; |
| | 17 | 24 | | _position = 0; |
| | 17 | 25 | | _wrapperName = wrapperName; |
| | 17 | 26 | | _wrapperNs = wrapperNs; |
| | 17 | 27 | | _elementName = elementName; |
| | 17 | 28 | | _elementNs = elementNs; |
| | 17 | 29 | | _isRequest = isRequest; |
| | 17 | 30 | | } |
| | | 31 | | |
| | | 32 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 33 | | { |
| | 143 | 34 | | EnsureStreamIsOpen(); |
| | 143 | 35 | | if (buffer == null) |
| | | 36 | | { |
| | 0 | 37 | | throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(buffer)), _message); |
| | | 38 | | } |
| | | 39 | | |
| | 143 | 40 | | if (offset < 0) |
| | | 41 | | { |
| | 0 | 42 | | throw TraceUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(offset), offset, |
| | 0 | 43 | | SRCommon.ValueMustBeNonNegative), _message); |
| | | 44 | | } |
| | | 45 | | |
| | 143 | 46 | | if (count < 0) |
| | | 47 | | { |
| | 0 | 48 | | throw TraceUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(count), count, |
| | 0 | 49 | | SRCommon.ValueMustBeNonNegative), _message); |
| | | 50 | | } |
| | | 51 | | |
| | 143 | 52 | | if (buffer.Length - offset < count) |
| | | 53 | | { |
| | 0 | 54 | | throw TraceUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.SFxInvalidStreamOffsetLength, off |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | try |
| | | 58 | | { |
| | 143 | 59 | | if (_reader == null) |
| | | 60 | | { |
| | 16 | 61 | | _reader = _message.GetReaderAtBodyContents(); |
| | 16 | 62 | | if (_wrapperName != null) |
| | | 63 | | { |
| | 15 | 64 | | _reader.MoveToContent(); |
| | 15 | 65 | | _reader.ReadStartElement(_wrapperName, _wrapperNs); |
| | | 66 | | } |
| | 16 | 67 | | _reader.MoveToContent(); |
| | 16 | 68 | | if (_reader.NodeType == XmlNodeType.EndElement) |
| | | 69 | | { |
| | 0 | 70 | | return 0; |
| | | 71 | | } |
| | | 72 | | |
| | 16 | 73 | | _reader.ReadStartElement(_elementName, _elementNs); |
| | | 74 | | } |
| | 143 | 75 | | if (_reader.MoveToContent() != XmlNodeType.Text) |
| | | 76 | | { |
| | 13 | 77 | | Exhaust(_reader); |
| | 13 | 78 | | return 0; |
| | | 79 | | } |
| | 130 | 80 | | int bytesRead = _reader.ReadContentAsBase64(buffer, offset, count); |
| | 130 | 81 | | _position += bytesRead; |
| | 130 | 82 | | if (bytesRead == 0) |
| | | 83 | | { |
| | 2 | 84 | | Exhaust(_reader); |
| | | 85 | | } |
| | 130 | 86 | | return bytesRead; |
| | | 87 | | } |
| | 0 | 88 | | catch (Exception ex) |
| | | 89 | | { |
| | 0 | 90 | | if (Fx.IsFatal(ex)) |
| | | 91 | | { |
| | 0 | 92 | | throw; |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new IOException(SR.SFxStreamIOException, ex)); |
| | | 96 | | } |
| | 143 | 97 | | } |
| | | 98 | | |
| | | 99 | | private void EnsureStreamIsOpen() |
| | | 100 | | { |
| | 143 | 101 | | if (_message.State == MessageState.Closed) |
| | | 102 | | { |
| | 0 | 103 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException( |
| | 0 | 104 | | _isRequest ? SR.SFxStreamRequestMessageClosed : SR.SFxStreamResponseMessageClosed)); |
| | | 105 | | } |
| | 143 | 106 | | } |
| | | 107 | | |
| | | 108 | | private static void Exhaust(XmlDictionaryReader reader) |
| | | 109 | | { |
| | 15 | 110 | | if (reader != null) |
| | | 111 | | { |
| | 57 | 112 | | while (reader.Read()) |
| | | 113 | | { |
| | | 114 | | // drain |
| | | 115 | | } |
| | | 116 | | } |
| | 15 | 117 | | } |
| | | 118 | | |
| | | 119 | | public override long Position |
| | | 120 | | { |
| | | 121 | | get |
| | | 122 | | { |
| | 0 | 123 | | EnsureStreamIsOpen(); |
| | 0 | 124 | | return _position; |
| | | 125 | | } |
| | 0 | 126 | | set { throw TraceUtility.ThrowHelperError(new NotSupportedException(), _message); } |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | protected override void Dispose(bool isDisposing) |
| | | 130 | | { |
| | 13 | 131 | | _message.Close(); |
| | 13 | 132 | | if (_reader != null) |
| | | 133 | | { |
| | 10 | 134 | | _reader.Dispose(); |
| | 10 | 135 | | _reader = null; |
| | | 136 | | } |
| | 13 | 137 | | base.Dispose(isDisposing); |
| | 13 | 138 | | } |
| | | 139 | | |
| | 10 | 140 | | public override bool CanRead { get { return _message.State != MessageState.Closed; } } |
| | 1 | 141 | | public override bool CanSeek { get { return false; } } |
| | 0 | 142 | | public override bool CanWrite { get { return false; } } |
| | | 143 | | public override long Length |
| | | 144 | | { |
| | | 145 | | get |
| | | 146 | | { |
| | 0 | 147 | | throw TraceUtility.ThrowHelperError(new NotSupportedException(), _message); |
| | | 148 | | } |
| | | 149 | | } |
| | 0 | 150 | | public override void Flush() { throw TraceUtility.ThrowHelperError(new NotSupportedException(), _message); } |
| | 0 | 151 | | public override long Seek(long offset, SeekOrigin origin) { throw TraceUtility.ThrowHelperError(new NotSupported |
| | 0 | 152 | | public override void SetLength(long value) { throw TraceUtility.ThrowHelperError(new NotSupportedException(), _m |
| | 0 | 153 | | public override void Write(byte[] buffer, int offset, int count) { throw TraceUtility.ThrowHelperError(new NotSu |
| | | 154 | | } |
| | | 155 | | } |