1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System;
using System.IO;
using System.Net.Http;
using System.Runtime;
using System.Xml;
abstract class XmlStreamedByteStreamReader : XmlByteStreamReader
{
protected XmlStreamedByteStreamReader(XmlDictionaryReaderQuotas quotas)
: base(quotas)
{
}
public static XmlStreamedByteStreamReader Create(Stream stream, XmlDictionaryReaderQuotas quotas)
{
return new StreamXmlStreamedByteStreamReader(stream, quotas);
}
public static XmlStreamedByteStreamReader Create(HttpRequestMessage httpRequestMessage, XmlDictionaryReaderQuotas quotas)
{
return new HttpRequestMessageStreamedBodyReader(httpRequestMessage, quotas);
}
public static XmlStreamedByteStreamReader Create(HttpResponseMessage httpResponseMessage, XmlDictionaryReaderQuotas quotas)
{
return new HttpResponseMessageStreamedBodyReader(httpResponseMessage, quotas);
}
protected override void OnClose()
{
this.ReleaseStream();
base.OnClose();
}
public override int ReadContentAsBase64(byte[] buffer, int index, int count)
{
EnsureInContent();
ByteStreamMessageUtility.EnsureByteBoundaries(buffer, index, count, true);
if (count == 0)
{
return 0;
}
Stream stream = this.GetStream();
int numBytesRead = stream.Read(buffer, index, count);
if (numBytesRead == 0)
{
this.position = ReaderPosition.EndElement;
}
return numBytesRead;
}
protected override byte[] OnToByteArray()
{
throw FxTrace.Exception.AsError(
new InvalidOperationException(SR.GetByteArrayFromStreamContentNotAllowed));
}
protected override Stream OnToStream()
{
Stream result = this.GetStream();
Fx.Assert(result != null, "The inner stream is null. Please check if the reader is closed or the ToStream method was already called before.");
this.ReleaseStream();
return result;
}
protected abstract Stream GetStream();
protected abstract void ReleaseStream();
public override bool TryGetBase64ContentLength(out int length)
{
// in ByteStream encoder, we're not concerned about individual xml nodes
// therefore we can just return the entire length of the stream
Stream stream = this.GetStream();
if (!this.IsClosed && stream.CanSeek)
{
long streamLength = stream.Length;
if (streamLength <= int.MaxValue)
{
length = (int)streamLength;
return true;
}
}
length = -1;
return false;
}
class StreamXmlStreamedByteStreamReader : XmlStreamedByteStreamReader
{
private Stream stream;
public StreamXmlStreamedByteStreamReader(Stream stream, XmlDictionaryReaderQuotas quotas)
: base(quotas)
{
Fx.Assert(stream != null, "The 'stream' parameter should not be null.");
this.stream = stream;
}
protected override Stream GetStream()
{
return this.stream;
}
protected override void ReleaseStream()
{
this.stream = null;
}
}
class HttpRequestMessageStreamedBodyReader : XmlStreamedByteStreamReader
{
private HttpRequestMessage httpRequestMessage;
public HttpRequestMessageStreamedBodyReader(HttpRequestMessage httpRequestMessage, XmlDictionaryReaderQuotas quotas)
: base(quotas)
{
Fx.Assert(httpRequestMessage != null, "The 'httpRequestMessage' parameter should not be null.");
this.httpRequestMessage = httpRequestMessage;
}
protected override Stream GetStream()
{
if (this.httpRequestMessage == null)
{
return null;
}
HttpContent content = this.httpRequestMessage.Content;
if (content != null)
{
return content.ReadAsStreamAsync().Result;
}
return new MemoryStream(EmptyArray<byte>.Instance);
}
protected override void ReleaseStream()
{
this.httpRequestMessage = null;
}
}
class HttpResponseMessageStreamedBodyReader : XmlStreamedByteStreamReader
{
private HttpResponseMessage httpResponseMessage;
public HttpResponseMessageStreamedBodyReader(HttpResponseMessage httpResponseMessage, XmlDictionaryReaderQuotas quotas)
: base(quotas)
{
Fx.Assert(httpResponseMessage != null, "The 'httpResponseMessage' parameter should not be null.");
this.httpResponseMessage = httpResponseMessage;
}
protected override Stream GetStream()
{
if (this.httpResponseMessage == null)
{
return null;
}
HttpContent content = this.httpResponseMessage.Content;
if (content != null)
{
return content.ReadAsStreamAsync().Result;
}
return new MemoryStream(EmptyArray<byte>.Instance);
}
protected override void ReleaseStream()
{
this.httpResponseMessage = null;
}
}
}
}
|