| | | 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.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Runtime |
| | | 11 | | { |
| | | 12 | | internal class BufferedOutputStream : Stream |
| | | 13 | | { |
| | | 14 | | private BufferManager _bufferManager; |
| | | 15 | | private byte[][] _chunks; |
| | | 16 | | private int _chunkCount; |
| | | 17 | | private byte[] _currentChunk; |
| | | 18 | | private int _currentChunkSize; |
| | | 19 | | private int _maxSize; |
| | | 20 | | private int _maxSizeQuota; |
| | | 21 | | private int _totalSize; |
| | | 22 | | private bool _callerReturnsBuffer; |
| | | 23 | | private bool _bufferReturned; |
| | | 24 | | private bool _initialized; |
| | | 25 | | |
| | | 26 | | // requires an explicit call to Init() by the caller |
| | 24 | 27 | | public BufferedOutputStream() |
| | | 28 | | { |
| | 24 | 29 | | _chunks = new byte[4][]; |
| | 24 | 30 | | } |
| | | 31 | | |
| | | 32 | | public BufferedOutputStream(int initialSize, int maxSize, BufferManager bufferManager) |
| | 0 | 33 | | : this() |
| | | 34 | | { |
| | 0 | 35 | | Reinitialize(initialSize, maxSize, bufferManager); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public override bool CanRead |
| | | 39 | | { |
| | | 40 | | get |
| | | 41 | | { |
| | 0 | 42 | | return false; |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public override bool CanSeek |
| | | 47 | | { |
| | | 48 | | get |
| | | 49 | | { |
| | 0 | 50 | | return false; |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public override bool CanWrite |
| | | 55 | | { |
| | | 56 | | get |
| | | 57 | | { |
| | 0 | 58 | | return true; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public override long Length |
| | | 63 | | { |
| | | 64 | | get |
| | | 65 | | { |
| | 0 | 66 | | return _totalSize; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public override long Position |
| | | 71 | | { |
| | | 72 | | get |
| | | 73 | | { |
| | 0 | 74 | | throw Fx.Exception.AsError(new NotSupportedException(SR.SeekNotSupported)); |
| | | 75 | | } |
| | | 76 | | set |
| | | 77 | | { |
| | 0 | 78 | | throw Fx.Exception.AsError(new NotSupportedException(SR.SeekNotSupported)); |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public void Reinitialize(int initialSize, int maxSizeQuota, BufferManager bufferManager) |
| | | 83 | | { |
| | 0 | 84 | | Reinitialize(initialSize, maxSizeQuota, maxSizeQuota, bufferManager); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | public void Reinitialize(int initialSize, int maxSizeQuota, int effectiveMaxSize, BufferManager bufferManager) |
| | | 88 | | { |
| | | 89 | | Fx.Assert(!_initialized, "Clear must be called before re-initializing stream"); |
| | 24 | 90 | | _maxSizeQuota = maxSizeQuota; |
| | 24 | 91 | | _maxSize = effectiveMaxSize; |
| | 24 | 92 | | _bufferManager = bufferManager; |
| | 24 | 93 | | _currentChunk = bufferManager.TakeBuffer(initialSize); |
| | 24 | 94 | | _currentChunkSize = 0; |
| | 24 | 95 | | _totalSize = 0; |
| | 24 | 96 | | _chunkCount = 1; |
| | 24 | 97 | | _chunks[0] = _currentChunk; |
| | 24 | 98 | | _initialized = true; |
| | 24 | 99 | | } |
| | | 100 | | |
| | | 101 | | private void AllocNextChunk(int minimumChunkSize) |
| | | 102 | | { |
| | | 103 | | int newChunkSize; |
| | 0 | 104 | | if (_currentChunk.Length > (int.MaxValue / 2)) |
| | | 105 | | { |
| | 0 | 106 | | newChunkSize = int.MaxValue; |
| | | 107 | | } |
| | | 108 | | else |
| | | 109 | | { |
| | 0 | 110 | | newChunkSize = _currentChunk.Length * 2; |
| | | 111 | | } |
| | 0 | 112 | | if (minimumChunkSize > newChunkSize) |
| | | 113 | | { |
| | 0 | 114 | | newChunkSize = minimumChunkSize; |
| | | 115 | | } |
| | 0 | 116 | | byte[] newChunk = _bufferManager.TakeBuffer(newChunkSize); |
| | 0 | 117 | | if (_chunkCount == _chunks.Length) |
| | | 118 | | { |
| | 0 | 119 | | byte[][] newChunks = new byte[_chunks.Length * 2][]; |
| | 0 | 120 | | Array.Copy(_chunks, newChunks, _chunks.Length); |
| | 0 | 121 | | _chunks = newChunks; |
| | | 122 | | } |
| | 0 | 123 | | _chunks[_chunkCount++] = newChunk; |
| | 0 | 124 | | _currentChunk = newChunk; |
| | 0 | 125 | | _currentChunkSize = 0; |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | | 129 | | { |
| | 0 | 130 | | return Task.FromException<int>(Fx.Exception.AsError(new NotSupportedException(SR.ReadNotSupported))); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | //public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object sta |
| | | 134 | | //{ |
| | | 135 | | // throw Fx.Exception.AsError(new NotSupportedException(SR.ReadNotSupported)); |
| | | 136 | | //} |
| | | 137 | | |
| | | 138 | | //public override int EndRead(IAsyncResult result) |
| | | 139 | | //{ |
| | | 140 | | // throw Fx.Exception.AsError(new NotSupportedException(SR.ReadNotSupported)); |
| | | 141 | | //} |
| | | 142 | | |
| | | 143 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | | 144 | | { |
| | 0 | 145 | | Write(buffer, offset, count); |
| | 0 | 146 | | return Task.CompletedTask; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | //public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object st |
| | | 150 | | //{ |
| | | 151 | | // Write(buffer, offset, size); |
| | | 152 | | // return new CompletedAsyncResult(callback, state); |
| | | 153 | | //} |
| | | 154 | | |
| | | 155 | | //public override void EndWrite(IAsyncResult result) |
| | | 156 | | //{ |
| | | 157 | | // CompletedAsyncResult.End(result); |
| | | 158 | | //} |
| | | 159 | | |
| | | 160 | | public void Clear() |
| | | 161 | | { |
| | 24 | 162 | | if (!_callerReturnsBuffer) |
| | | 163 | | { |
| | 0 | 164 | | for (int i = 0; i < _chunkCount; i++) |
| | | 165 | | { |
| | 0 | 166 | | _bufferManager.ReturnBuffer(_chunks[i]); |
| | 0 | 167 | | _chunks[i] = null; |
| | | 168 | | } |
| | | 169 | | } |
| | | 170 | | |
| | 24 | 171 | | _callerReturnsBuffer = false; |
| | 24 | 172 | | _initialized = false; |
| | 24 | 173 | | _bufferReturned = false; |
| | 24 | 174 | | _chunkCount = 0; |
| | 24 | 175 | | _currentChunk = null; |
| | 24 | 176 | | } |
| | | 177 | | |
| | | 178 | | //public override void Close() |
| | | 179 | | //{ |
| | | 180 | | //} |
| | | 181 | | |
| | | 182 | | public override void Flush() |
| | | 183 | | { |
| | 48 | 184 | | } |
| | | 185 | | |
| | | 186 | | public override int Read(byte[] buffer, int offset, int size) |
| | | 187 | | { |
| | 0 | 188 | | throw Fx.Exception.AsError(new NotSupportedException(SR.ReadNotSupported)); |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | public override int ReadByte() |
| | | 192 | | { |
| | 0 | 193 | | throw Fx.Exception.AsError(new NotSupportedException(SR.ReadNotSupported)); |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | public override long Seek(long offset, SeekOrigin origin) |
| | | 197 | | { |
| | 0 | 198 | | throw Fx.Exception.AsError(new NotSupportedException(SR.SeekNotSupported)); |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | public override void SetLength(long value) |
| | | 202 | | { |
| | 0 | 203 | | throw Fx.Exception.AsError(new NotSupportedException(SR.SeekNotSupported)); |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | public MemoryStream ToMemoryStream() |
| | | 207 | | { |
| | 0 | 208 | | byte[] buffer = ToArray(out int bufferSize); |
| | 0 | 209 | | return new MemoryStream(buffer, 0, bufferSize); |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | public byte[] ToArray(out int bufferSize) |
| | | 213 | | { |
| | | 214 | | Fx.Assert(_initialized, "No data to return from uninitialized stream"); |
| | | 215 | | Fx.Assert(!_bufferReturned, "ToArray cannot be called more than once"); |
| | | 216 | | |
| | | 217 | | byte[] buffer; |
| | 24 | 218 | | if (_chunkCount == 1) |
| | | 219 | | { |
| | 24 | 220 | | buffer = _currentChunk; |
| | 24 | 221 | | bufferSize = _currentChunkSize; |
| | 24 | 222 | | _callerReturnsBuffer = true; |
| | | 223 | | } |
| | | 224 | | else |
| | | 225 | | { |
| | 0 | 226 | | buffer = _bufferManager.TakeBuffer(_totalSize); |
| | 0 | 227 | | int offset = 0; |
| | 0 | 228 | | int count = _chunkCount - 1; |
| | 0 | 229 | | for (int i = 0; i < count; i++) |
| | | 230 | | { |
| | 0 | 231 | | byte[] chunk = _chunks[i]; |
| | 0 | 232 | | Buffer.BlockCopy(chunk, 0, buffer, offset, chunk.Length); |
| | 0 | 233 | | offset += chunk.Length; |
| | | 234 | | } |
| | 0 | 235 | | Buffer.BlockCopy(_currentChunk, 0, buffer, offset, _currentChunkSize); |
| | 0 | 236 | | bufferSize = _totalSize; |
| | | 237 | | } |
| | | 238 | | |
| | 24 | 239 | | _bufferReturned = true; |
| | 24 | 240 | | return buffer; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | public void Skip(int size) |
| | | 244 | | { |
| | 24 | 245 | | WriteCore(null, 0, size); |
| | 24 | 246 | | } |
| | | 247 | | |
| | | 248 | | public override void Write(byte[] buffer, int offset, int size) |
| | | 249 | | { |
| | 24 | 250 | | WriteCore(buffer, offset, size); |
| | 24 | 251 | | } |
| | | 252 | | |
| | | 253 | | protected virtual Exception CreateQuotaExceededException(int maxSizeQuota) |
| | | 254 | | { |
| | 0 | 255 | | return new InvalidOperationException(SR.Format(SR.BufferedOutputStreamQuotaExceeded, maxSizeQuota)); |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | private void WriteCore(byte[] buffer, int offset, int size) |
| | | 259 | | { |
| | | 260 | | Fx.Assert(_initialized, "Cannot write to uninitialized stream"); |
| | | 261 | | Fx.Assert(!_bufferReturned, "Cannot write to stream once ToArray has been called."); |
| | | 262 | | |
| | 48 | 263 | | if (size < 0) |
| | | 264 | | { |
| | 0 | 265 | | throw Fx.Exception.ArgumentOutOfRange(nameof(size), size, SRCommon.ValueMustBeNonNegative); |
| | | 266 | | } |
| | | 267 | | |
| | 48 | 268 | | if ((int.MaxValue - size) < _totalSize) |
| | | 269 | | { |
| | 0 | 270 | | throw Fx.Exception.AsError(CreateQuotaExceededException(_maxSizeQuota)); |
| | | 271 | | } |
| | | 272 | | |
| | 48 | 273 | | int newTotalSize = _totalSize + size; |
| | 48 | 274 | | if (newTotalSize > _maxSize) |
| | | 275 | | { |
| | 0 | 276 | | throw Fx.Exception.AsError(CreateQuotaExceededException(_maxSizeQuota)); |
| | | 277 | | } |
| | | 278 | | |
| | 48 | 279 | | int remainingSizeInChunk = _currentChunk.Length - _currentChunkSize; |
| | 48 | 280 | | if (size > remainingSizeInChunk) |
| | | 281 | | { |
| | 0 | 282 | | if (remainingSizeInChunk > 0) |
| | | 283 | | { |
| | 0 | 284 | | if (buffer != null) |
| | | 285 | | { |
| | 0 | 286 | | Buffer.BlockCopy(buffer, offset, _currentChunk, _currentChunkSize, remainingSizeInChunk); |
| | | 287 | | } |
| | 0 | 288 | | _currentChunkSize = _currentChunk.Length; |
| | 0 | 289 | | offset += remainingSizeInChunk; |
| | 0 | 290 | | size -= remainingSizeInChunk; |
| | | 291 | | } |
| | 0 | 292 | | AllocNextChunk(size); |
| | | 293 | | } |
| | | 294 | | |
| | 48 | 295 | | if (buffer != null) |
| | | 296 | | { |
| | 24 | 297 | | Buffer.BlockCopy(buffer, offset, _currentChunk, _currentChunkSize, size); |
| | | 298 | | } |
| | 48 | 299 | | _totalSize = newTotalSize; |
| | 48 | 300 | | _currentChunkSize += size; |
| | 48 | 301 | | } |
| | | 302 | | |
| | | 303 | | public override void WriteByte(byte value) |
| | | 304 | | { |
| | | 305 | | Fx.Assert(_initialized, "Cannot write to uninitialized stream"); |
| | | 306 | | Fx.Assert(!_bufferReturned, "Cannot write to stream once ToArray has been called."); |
| | | 307 | | |
| | 0 | 308 | | if (_totalSize == _maxSize) |
| | | 309 | | { |
| | 0 | 310 | | throw Fx.Exception.AsError(CreateQuotaExceededException(_maxSize)); |
| | | 311 | | } |
| | 0 | 312 | | if (_currentChunkSize == _currentChunk.Length) |
| | | 313 | | { |
| | 0 | 314 | | AllocNextChunk(1); |
| | | 315 | | } |
| | 0 | 316 | | _currentChunk[_currentChunkSize++] = value; |
| | 0 | 317 | | } |
| | | 318 | | } |
| | | 319 | | } |