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