| | | 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 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | public abstract class StreamBodyWriter : BodyWriter |
| | | 11 | | { |
| | | 12 | | protected StreamBodyWriter(bool isBuffered) |
| | 4 | 13 | | : base(isBuffered) |
| | 4 | 14 | | { } |
| | | 15 | | |
| | | 16 | | protected abstract void OnWriteBodyContents(Stream stream); |
| | | 17 | | |
| | | 18 | | protected override BodyWriter OnCreateBufferedCopy(int maxBufferSize) |
| | | 19 | | { |
| | 0 | 20 | | using (BufferManagerOutputStream bufferedStream = new BufferManagerOutputStream(SR.MaxReceivedMessageSizeExc |
| | | 21 | | { |
| | 0 | 22 | | OnWriteBodyContents(bufferedStream); |
| | 0 | 23 | | byte[] bytesArray = bufferedStream.ToArray(out int size); |
| | 0 | 24 | | return new BufferedBytesStreamBodyWriter(bytesArray, size); |
| | | 25 | | } |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 29 | | { |
| | 4 | 30 | | using (XmlWriterBackedStream stream = new XmlWriterBackedStream(writer)) |
| | | 31 | | { |
| | 4 | 32 | | OnWriteBodyContents(stream); |
| | 4 | 33 | | } |
| | 4 | 34 | | } |
| | | 35 | | |
| | | 36 | | internal class XmlWriterBackedStream : Stream |
| | | 37 | | { |
| | | 38 | | private const string StreamElementName = "Binary"; |
| | | 39 | | private readonly XmlWriter _writer; |
| | | 40 | | |
| | 4 | 41 | | public XmlWriterBackedStream(XmlWriter writer) |
| | | 42 | | { |
| | 4 | 43 | | if (writer == null) |
| | | 44 | | { |
| | 0 | 45 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 46 | | } |
| | | 47 | | |
| | 4 | 48 | | _writer = writer; |
| | 4 | 49 | | } |
| | | 50 | | |
| | | 51 | | public override bool CanRead |
| | | 52 | | { |
| | 0 | 53 | | get { return false; } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public override bool CanSeek |
| | | 57 | | { |
| | 4 | 58 | | get { return false; } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public override bool CanWrite |
| | | 62 | | { |
| | 0 | 63 | | get { return true; } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public override void Flush() |
| | | 67 | | { |
| | 8 | 68 | | _writer.Flush(); |
| | 8 | 69 | | } |
| | | 70 | | |
| | | 71 | | public override long Length |
| | | 72 | | { |
| | | 73 | | get |
| | | 74 | | { |
| | 0 | 75 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format( |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public override long Position |
| | | 80 | | { |
| | | 81 | | get |
| | | 82 | | { |
| | 0 | 83 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format( |
| | | 84 | | } |
| | | 85 | | set |
| | | 86 | | { |
| | 0 | 87 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format( |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 92 | | { |
| | 0 | 93 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object |
| | | 97 | | { |
| | 0 | 98 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | public override int EndRead(IAsyncResult asyncResult) |
| | | 102 | | { |
| | 0 | 103 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public override int ReadByte() |
| | | 107 | | { |
| | 0 | 108 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | public override long Seek(long offset, SeekOrigin origin) |
| | | 112 | | { |
| | 0 | 113 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | public override void SetLength(long value) |
| | | 117 | | { |
| | 0 | 118 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.X |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 122 | | { |
| | 4 | 123 | | if (_writer.WriteState == WriteState.Content) |
| | | 124 | | { |
| | 0 | 125 | | _writer.WriteBase64(buffer, offset, count); |
| | | 126 | | } |
| | 4 | 127 | | else if (_writer.WriteState == WriteState.Start) |
| | | 128 | | { |
| | 4 | 129 | | _writer.WriteStartElement(StreamElementName, string.Empty); |
| | 4 | 130 | | _writer.WriteBase64(buffer, offset, count); |
| | | 131 | | } |
| | 4 | 132 | | } |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | internal class BufferedBytesStreamBodyWriter : StreamBodyWriter |
| | | 136 | | { |
| | | 137 | | private readonly byte[] _array; |
| | | 138 | | private readonly int _size; |
| | | 139 | | |
| | | 140 | | public BufferedBytesStreamBodyWriter(byte[] array, int size) |
| | 0 | 141 | | : base(true) |
| | | 142 | | { |
| | 0 | 143 | | _array = array; |
| | 0 | 144 | | _size = size; |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | protected override void OnWriteBodyContents(Stream stream) |
| | | 148 | | { |
| | 0 | 149 | | stream.Write(_array, 0, _size); |
| | 0 | 150 | | } |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | } |