Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / BufferedOutputStream.cs / 1 / BufferedOutputStream.cs
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.IO;
using System.ServiceModel;
using System.Diagnostics;
using System.Globalization;
class BufferedOutputStream : Stream
{
byte[] currentChunk;
int currentChunkSize;
byte[][] chunks;
int chunkCount;
int totalSize;
int maxSize;
int maxSizeQuota;
BufferManager bufferManager;
string quotaExceededString;
public BufferedOutputStream(string quotaExceededString)
{
this.chunks = new byte[4][];
this.quotaExceededString = quotaExceededString;
}
public BufferedOutputStream(string quotaExceededString, int maxSize)
: this(quotaExceededString)
{
Init(0, maxSize, BufferManager.CreateBufferManager(0, int.MaxValue));
}
public BufferedOutputStream(string quotaExceededString, int initialSize, int maxSize, BufferManager bufferManager)
: this(quotaExceededString)
{
Init(initialSize, maxSize, bufferManager);
}
public void Init(int initialSize, int maxSizeQuota, BufferManager bufferManager)
{
Init(initialSize, maxSizeQuota, maxSizeQuota, bufferManager);
}
public void Init(int initialSize, int maxSizeQuota, int effectiveMaxSize, BufferManager bufferManager)
{
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;
}
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
{
#pragma warning suppress 56503 // [....], required by the Stream.Position contract
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SeekNotSupported)));
}
set
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SeekNotSupported)));
}
}
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 DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.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 void Clear()
{
for (int i = 0; i < this.chunkCount; i++)
{
this.chunks[i] = null;
}
this.chunkCount = 0;
this.currentChunk = null;
}
public override void Close()
{
}
public override int EndRead(IAsyncResult result)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.ReadNotSupported)));
}
public override void EndWrite(IAsyncResult result)
{
CompletedAsyncResult.End(result);
}
public override void Flush()
{
}
public override int Read(byte[] buffer, int offset, int size)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.ReadNotSupported)));
}
public override int ReadByte()
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.ReadNotSupported)));
}
public override long Seek(long offset, SeekOrigin origin)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SeekNotSupported)));
}
public override void SetLength(long value)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.SeekNotSupported)));
}
public MemoryStream ToMemoryStream()
{
int bufferSize;
byte[] buffer = ToArray(out bufferSize);
return new MemoryStream(buffer, 0, bufferSize);
}
public byte[] ToArray(out int bufferSize)
{
byte[] buffer;
if (this.chunkCount == 1)
{
buffer = this.currentChunk;
bufferSize = this.currentChunkSize;
}
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;
}
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);
}
void WriteCore(byte[] buffer, int offset, int size)
{
if (size < 0)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("size", size, SR.GetString(
SR.ValueMustBeNonNegative)));
}
if ((int.MaxValue - size) < this.totalSize)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new QuotaExceededException(SR.GetString(this.quotaExceededString, this.maxSizeQuota.ToString(NumberFormatInfo.CurrentInfo))));
}
int newTotalSize = this.totalSize + size;
if (newTotalSize > this.maxSize)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new QuotaExceededException(SR.GetString(this.quotaExceededString, this.maxSizeQuota.ToString(NumberFormatInfo.CurrentInfo))));
}
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)
{
if (this.totalSize == this.maxSize)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new QuotaExceededException(SR.GetString(this.quotaExceededString, this.maxSize)));
}
if (this.currentChunkSize == this.currentChunk.Length)
{
AllocNextChunk(1);
}
this.currentChunk[this.currentChunkSize++] = value;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Formatter.cs
- ElementNotEnabledException.cs
- BindableTemplateBuilder.cs
- MembershipPasswordException.cs
- RuntimeHandles.cs
- TextOutput.cs
- GenerateHelper.cs
- PolicyStatement.cs
- MimePart.cs
- TCEAdapterGenerator.cs
- COM2IPerPropertyBrowsingHandler.cs
- CounterCreationDataCollection.cs
- _FtpDataStream.cs
- MeasureItemEvent.cs
- GeometryHitTestParameters.cs
- Mouse.cs
- TypeToStringValueConverter.cs
- DateTimeOffset.cs
- TextTreeTextElementNode.cs
- WaitingCursor.cs
- StateBag.cs
- ModuleBuilder.cs
- TTSEvent.cs
- SymDocumentType.cs
- ContextMenuStrip.cs
- SlipBehavior.cs
- RegistryKey.cs
- SmtpLoginAuthenticationModule.cs
- WinInet.cs
- StringAnimationUsingKeyFrames.cs
- CodeTypeDeclarationCollection.cs
- Pointer.cs
- IisHelper.cs
- RelationshipFixer.cs
- TextWriterTraceListener.cs
- Page.cs
- XmlLanguage.cs
- EntityWrapperFactory.cs
- Opcode.cs
- shaperfactoryquerycachekey.cs
- CryptoHelper.cs
- Avt.cs
- Propagator.JoinPropagator.cs
- TextBoxAutomationPeer.cs
- PluralizationService.cs
- IdnElement.cs
- RenderTargetBitmap.cs
- DynamicRouteExpression.cs
- ControlPager.cs
- BackgroundWorker.cs
- DataGridViewButtonCell.cs
- PartialList.cs
- RC2CryptoServiceProvider.cs
- DataSourceHelper.cs
- HttpProfileBase.cs
- DataBoundControlHelper.cs
- DataGridRelationshipRow.cs
- ExeConfigurationFileMap.cs
- SplitterEvent.cs
- ProfileSettingsCollection.cs
- VirtualPath.cs
- ToolStripStatusLabel.cs
- DeviceContext2.cs
- SoapInteropTypes.cs
- SwitchCase.cs
- Command.cs
- DbConnectionOptions.cs
- OleDbErrorCollection.cs
- _FixedSizeReader.cs
- IndependentAnimationStorage.cs
- SapiRecognizer.cs
- HMACRIPEMD160.cs
- NotImplementedException.cs
- MethodImplAttribute.cs
- EditingContext.cs
- SecurityHeaderLayout.cs
- QilList.cs
- SchemaNames.cs
- VectorAnimationBase.cs
- DataGridState.cs
- CacheModeValueSerializer.cs
- HttpClientChannel.cs
- SerialStream.cs
- XhtmlBasicCommandAdapter.cs
- MsmqProcessProtocolHandler.cs
- XmlExpressionDumper.cs
- TableHeaderCell.cs
- SafeLibraryHandle.cs
- ColumnProvider.cs
- BaseServiceProvider.cs
- SignatureToken.cs
- BaseTemplateBuildProvider.cs
- ItemsControl.cs
- ApplicationFileCodeDomTreeGenerator.cs
- Propagator.ExtentPlaceholderCreator.cs
- ExtentKey.cs
- ProtocolViolationException.cs
- ObjectDataSourceDisposingEventArgs.cs
- ItemPager.cs
- DocumentSchemaValidator.cs