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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.Runtime
{
using System;
using System.Globalization;
using System.IO;
class BufferedOutputStream : Stream
{
[Fx.Tag.Cache(typeof(byte), Fx.Tag.CacheAttrition.None, Scope = Fx.Tag.Strings.ExternallyManaged,
SizeLimit = Fx.Tag.Strings.ExternallyManaged)]
InternalBufferManager bufferManager;
[Fx.Tag.Queue(typeof(byte), SizeLimit = "BufferedOutputStream(maxSize)",
StaleElementsRemovedImmediately = true, EnqueueThrowsIfFull = true)]
byte[][] chunks;
int chunkCount;
byte[] currentChunk;
int currentChunkSize;
int maxSize;
int maxSizeQuota;
int totalSize;
bool callerReturnsBuffer;
bool bufferReturned;
bool initialized;
// requires an explicit call to Init() by the caller
public BufferedOutputStream()
{
this.chunks = new byte[4][];
}
public BufferedOutputStream(int initialSize, int maxSize, InternalBufferManager bufferManager)
: this()
{
Reinitialize(initialSize, maxSize, bufferManager);
}
public BufferedOutputStream(int maxSize)
: this(0, maxSize, InternalBufferManager.Create(0, int.MaxValue))
{
}
public override bool CanRead
{
get
{
return false;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override bool CanWrite
{
get
{
return true;
}
}
public override long Length
{
get
{
return this.totalSize;
}
}
public override long Position
{
get
{
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.SeekNotSupported));
}
set
{
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.SeekNotSupported));
}
}
public void Reinitialize(int initialSize, int maxSizeQuota, InternalBufferManager bufferManager)
{
Reinitialize(initialSize, maxSizeQuota, maxSizeQuota, bufferManager);
}
public void Reinitialize(int initialSize, int maxSizeQuota, int effectiveMaxSize, InternalBufferManager bufferManager)
{
Fx.Assert(!this.initialized, "Clear must be called before re-initializing stream");
this.maxSizeQuota = maxSizeQuota;
this.maxSize = effectiveMaxSize;
this.bufferManager = bufferManager;
this.currentChunk = bufferManager.TakeBuffer(initialSize);
this.currentChunkSize = 0;
this.totalSize = 0;
this.chunkCount = 1;
this.chunks[0] = this.currentChunk;
this.initialized = true;
}
void AllocNextChunk(int minimumChunkSize)
{
int newChunkSize;
if (this.currentChunk.Length > (int.MaxValue / 2))
{
newChunkSize = int.MaxValue;
}
else
{
newChunkSize = this.currentChunk.Length * 2;
}
if (minimumChunkSize > newChunkSize)
{
newChunkSize = minimumChunkSize;
}
byte[] newChunk = this.bufferManager.TakeBuffer(newChunkSize);
if (this.chunkCount == this.chunks.Length)
{
byte[][] newChunks = new byte[this.chunks.Length * 2][];
Array.Copy(this.chunks, newChunks, this.chunks.Length);
this.chunks = newChunks;
}
this.chunks[this.chunkCount++] = newChunk;
this.currentChunk = newChunk;
this.currentChunkSize = 0;
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
{
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.ReadNotSupported));
}
public override int EndRead(IAsyncResult result)
{
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.ReadNotSupported));
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
{
Write(buffer, offset, size);
return new CompletedAsyncResult(callback, state);
}
public override void EndWrite(IAsyncResult result)
{
CompletedAsyncResult.End(result);
}
public void Clear()
{
if (!this.callerReturnsBuffer)
{
for (int i = 0; i < this.chunkCount; i++)
{
this.bufferManager.ReturnBuffer(this.chunks[i]);
this.chunks[i] = null;
}
}
this.callerReturnsBuffer = false;
this.initialized = false;
this.bufferReturned = false;
this.chunkCount = 0;
this.currentChunk = null;
}
public override void Close()
{
}
public override void Flush()
{
}
public override int Read(byte[] buffer, int offset, int size)
{
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.ReadNotSupported));
}
public override int ReadByte()
{
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.ReadNotSupported));
}
public override long Seek(long offset, SeekOrigin origin)
{
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.SeekNotSupported));
}
public override void SetLength(long value)
{
throw Fx.Exception.AsError(new NotSupportedException(InternalSR.SeekNotSupported));
}
public MemoryStream ToMemoryStream()
{
int bufferSize;
byte[] buffer = ToArray(out bufferSize);
return new MemoryStream(buffer, 0, bufferSize);
}
public byte[] ToArray(out int bufferSize)
{
Fx.Assert(this.initialized, "No data to return from uninitialized stream");
Fx.Assert(!this.bufferReturned, "ToArray cannot be called more than once");
byte[] buffer;
if (this.chunkCount == 1)
{
buffer = this.currentChunk;
bufferSize = this.currentChunkSize;
this.callerReturnsBuffer = true;
}
else
{
buffer = this.bufferManager.TakeBuffer(this.totalSize);
int offset = 0;
int count = this.chunkCount - 1;
for (int i = 0; i < count; i++)
{
byte[] chunk = this.chunks[i];
Buffer.BlockCopy(chunk, 0, buffer, offset, chunk.Length);
offset += chunk.Length;
}
Buffer.BlockCopy(this.currentChunk, 0, buffer, offset, this.currentChunkSize);
bufferSize = this.totalSize;
}
this.bufferReturned = true;
return buffer;
}
public void Skip(int size)
{
WriteCore(null, 0, size);
}
public override void Write(byte[] buffer, int offset, int size)
{
WriteCore(buffer, offset, size);
}
protected virtual Exception CreateQuotaExceededException(int maxSizeQuota)
{
return new InvalidOperationException(InternalSR.BufferedOutputStreamQuotaExceeded(maxSizeQuota));
}
void WriteCore(byte[] buffer, int offset, int size)
{
Fx.Assert(this.initialized, "Cannot write to uninitialized stream");
Fx.Assert(!this.bufferReturned, "Cannot write to stream once ToArray has been called.");
if (size < 0)
{
throw Fx.Exception.ArgumentOutOfRange("size", size, InternalSR.ValueMustBeNonNegative);
}
if ((int.MaxValue - size) < this.totalSize)
{
throw Fx.Exception.AsError(CreateQuotaExceededException(this.maxSizeQuota));
}
int newTotalSize = this.totalSize + size;
if (newTotalSize > this.maxSize)
{
throw Fx.Exception.AsError(CreateQuotaExceededException(this.maxSizeQuota));
}
int remainingSizeInChunk = this.currentChunk.Length - this.currentChunkSize;
if (size > remainingSizeInChunk)
{
if (remainingSizeInChunk > 0)
{
if (buffer != null)
{
Buffer.BlockCopy(buffer, offset, this.currentChunk, this.currentChunkSize, remainingSizeInChunk);
}
this.currentChunkSize = this.currentChunk.Length;
offset += remainingSizeInChunk;
size -= remainingSizeInChunk;
}
AllocNextChunk(size);
}
if (buffer != null)
{
Buffer.BlockCopy(buffer, offset, this.currentChunk, this.currentChunkSize, size);
}
this.totalSize = newTotalSize;
this.currentChunkSize += size;
}
public override void WriteByte(byte value)
{
Fx.Assert(this.initialized, "Cannot write to uninitialized stream");
Fx.Assert(!this.bufferReturned, "Cannot write to stream once ToArray has been called.");
if (this.totalSize == this.maxSize)
{
throw Fx.Exception.AsError(CreateQuotaExceededException(this.maxSize));
}
if (this.currentChunkSize == this.currentChunk.Length)
{
AllocNextChunk(1);
}
this.currentChunk[this.currentChunkSize++] = value;
}
}
}
|