Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / Net / System / Net / Mail / BufferedReadStream.cs / 1 / BufferedReadStream.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Net
{
using System;
using System.IO;
internal class BufferedReadStream : DelegatedStream
{
byte[] storedBuffer;
int storedLength;
int storedOffset;
bool readMore;
internal BufferedReadStream(Stream stream) : this(stream, false)
{
}
internal BufferedReadStream(Stream stream, bool readMore) : base(stream)
{
this.readMore = readMore;
}
public override bool CanWrite
{
get
{
return false;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
ReadAsyncResult result = new ReadAsyncResult(this, callback, state);
result.Read(buffer, offset, count);
return result;
}
public override int EndRead(IAsyncResult asyncResult)
{
int read = ReadAsyncResult.End(asyncResult);
return read;
}
public override int Read(byte[] buffer, int offset, int count)
{
int read = 0;
if (this.storedOffset < this.storedLength)
{
read = Math.Min(count, this.storedLength - this.storedOffset);
Buffer.BlockCopy(this.storedBuffer, this.storedOffset, buffer, offset, read);
this.storedOffset += read;
if (read == count || !this.readMore)
{
return read;
}
offset += read;
count -= read;
}
return read + base.Read(buffer, offset, count);
}
public override int ReadByte()
{
if (this.storedOffset < this.storedLength)
{
return (int)this.storedBuffer[this.storedOffset++];
}
else
{
return base.ReadByte();
}
}
internal void Push(byte[] buffer, int offset, int count)
{
if (count == 0)
return;
if (this.storedOffset == this.storedLength)
{
if (this.storedBuffer == null || this.storedBuffer.Length < count)
{
this.storedBuffer = new byte[count];
}
this.storedOffset = 0;
this.storedLength = count;
}
else
{
// if there's room to just insert before existing data
if (count <= this.storedOffset)
{
this.storedOffset -= count;
}
// if there's room in the buffer but need to shift things over
else if (count <= this.storedBuffer.Length - this.storedLength + this.storedOffset)
{
Buffer.BlockCopy(this.storedBuffer, this.storedOffset, this.storedBuffer, count, this.storedLength - this.storedOffset);
this.storedLength += count - this.storedOffset;
this.storedOffset = 0;
}
else
{
byte[] newBuffer = new byte[count + this.storedLength - this.storedOffset];
Buffer.BlockCopy(this.storedBuffer, this.storedOffset, newBuffer, count, this.storedLength - this.storedOffset);
this.storedLength += count - this.storedOffset;
this.storedOffset = 0;
this.storedBuffer = newBuffer;
}
}
Buffer.BlockCopy(buffer, offset, this.storedBuffer, this.storedOffset, count);
}
class ReadAsyncResult : LazyAsyncResult
{
BufferedReadStream parent;
int read;
static AsyncCallback onRead = new AsyncCallback(OnRead);
internal ReadAsyncResult(BufferedReadStream parent, AsyncCallback callback, object state) : base(null,state,callback)
{
this.parent = parent;
}
internal void Read(byte[] buffer, int offset, int count){
if (parent.storedOffset < parent.storedLength)
{
this.read = Math.Min(count, parent.storedLength - parent.storedOffset);
Buffer.BlockCopy(parent.storedBuffer, parent.storedOffset, buffer, offset, this.read);
parent.storedOffset += this.read;
if (this.read == count || !this.parent.readMore)
{
this.InvokeCallback();
return;
}
count -= this.read;
offset += this.read;
}
IAsyncResult result = parent.BaseStream.BeginRead(buffer, offset, count, onRead, this);
if (result.CompletedSynchronously)
{
//
this.read += parent.BaseStream.EndRead(result);
InvokeCallback();
}
}
internal static int End(IAsyncResult result)
{
ReadAsyncResult thisPtr = (ReadAsyncResult)result;
thisPtr.InternalWaitForCompletion();
return thisPtr.read;
}
static void OnRead(IAsyncResult result)
{
if (!result.CompletedSynchronously)
{
ReadAsyncResult thisPtr = (ReadAsyncResult)result.AsyncState;
try
{
thisPtr.read += thisPtr.parent.BaseStream.EndRead(result);
thisPtr.InvokeCallback();
}
catch (Exception e)
{
if (thisPtr.IsCompleted)
throw;
thisPtr.InvokeCallback(e);
}
catch {
if (thisPtr.IsCompleted)
throw;
thisPtr.InvokeCallback(new Exception(SR.GetString(SR.net_nonClsCompliantException)));
}
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Net
{
using System;
using System.IO;
internal class BufferedReadStream : DelegatedStream
{
byte[] storedBuffer;
int storedLength;
int storedOffset;
bool readMore;
internal BufferedReadStream(Stream stream) : this(stream, false)
{
}
internal BufferedReadStream(Stream stream, bool readMore) : base(stream)
{
this.readMore = readMore;
}
public override bool CanWrite
{
get
{
return false;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
ReadAsyncResult result = new ReadAsyncResult(this, callback, state);
result.Read(buffer, offset, count);
return result;
}
public override int EndRead(IAsyncResult asyncResult)
{
int read = ReadAsyncResult.End(asyncResult);
return read;
}
public override int Read(byte[] buffer, int offset, int count)
{
int read = 0;
if (this.storedOffset < this.storedLength)
{
read = Math.Min(count, this.storedLength - this.storedOffset);
Buffer.BlockCopy(this.storedBuffer, this.storedOffset, buffer, offset, read);
this.storedOffset += read;
if (read == count || !this.readMore)
{
return read;
}
offset += read;
count -= read;
}
return read + base.Read(buffer, offset, count);
}
public override int ReadByte()
{
if (this.storedOffset < this.storedLength)
{
return (int)this.storedBuffer[this.storedOffset++];
}
else
{
return base.ReadByte();
}
}
internal void Push(byte[] buffer, int offset, int count)
{
if (count == 0)
return;
if (this.storedOffset == this.storedLength)
{
if (this.storedBuffer == null || this.storedBuffer.Length < count)
{
this.storedBuffer = new byte[count];
}
this.storedOffset = 0;
this.storedLength = count;
}
else
{
// if there's room to just insert before existing data
if (count <= this.storedOffset)
{
this.storedOffset -= count;
}
// if there's room in the buffer but need to shift things over
else if (count <= this.storedBuffer.Length - this.storedLength + this.storedOffset)
{
Buffer.BlockCopy(this.storedBuffer, this.storedOffset, this.storedBuffer, count, this.storedLength - this.storedOffset);
this.storedLength += count - this.storedOffset;
this.storedOffset = 0;
}
else
{
byte[] newBuffer = new byte[count + this.storedLength - this.storedOffset];
Buffer.BlockCopy(this.storedBuffer, this.storedOffset, newBuffer, count, this.storedLength - this.storedOffset);
this.storedLength += count - this.storedOffset;
this.storedOffset = 0;
this.storedBuffer = newBuffer;
}
}
Buffer.BlockCopy(buffer, offset, this.storedBuffer, this.storedOffset, count);
}
class ReadAsyncResult : LazyAsyncResult
{
BufferedReadStream parent;
int read;
static AsyncCallback onRead = new AsyncCallback(OnRead);
internal ReadAsyncResult(BufferedReadStream parent, AsyncCallback callback, object state) : base(null,state,callback)
{
this.parent = parent;
}
internal void Read(byte[] buffer, int offset, int count){
if (parent.storedOffset < parent.storedLength)
{
this.read = Math.Min(count, parent.storedLength - parent.storedOffset);
Buffer.BlockCopy(parent.storedBuffer, parent.storedOffset, buffer, offset, this.read);
parent.storedOffset += this.read;
if (this.read == count || !this.parent.readMore)
{
this.InvokeCallback();
return;
}
count -= this.read;
offset += this.read;
}
IAsyncResult result = parent.BaseStream.BeginRead(buffer, offset, count, onRead, this);
if (result.CompletedSynchronously)
{
//
this.read += parent.BaseStream.EndRead(result);
InvokeCallback();
}
}
internal static int End(IAsyncResult result)
{
ReadAsyncResult thisPtr = (ReadAsyncResult)result;
thisPtr.InternalWaitForCompletion();
return thisPtr.read;
}
static void OnRead(IAsyncResult result)
{
if (!result.CompletedSynchronously)
{
ReadAsyncResult thisPtr = (ReadAsyncResult)result.AsyncState;
try
{
thisPtr.read += thisPtr.parent.BaseStream.EndRead(result);
thisPtr.InvokeCallback();
}
catch (Exception e)
{
if (thisPtr.IsCompleted)
throw;
thisPtr.InvokeCallback(e);
}
catch {
if (thisPtr.IsCompleted)
throw;
thisPtr.InvokeCallback(new Exception(SR.GetString(SR.net_nonClsCompliantException)));
}
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AttachedPropertyBrowsableAttribute.cs
- ScrollEventArgs.cs
- XmlComplianceUtil.cs
- XmlCountingReader.cs
- BindingGroup.cs
- DataGridSortCommandEventArgs.cs
- IssuanceLicense.cs
- WindowAutomationPeer.cs
- ExpressionBuilder.cs
- ADMembershipUser.cs
- ExpressionLink.cs
- WindowsSecurityTokenAuthenticator.cs
- StylusPointPropertyInfo.cs
- PageFunction.cs
- FileDetails.cs
- SpotLight.cs
- DesignerSerializerAttribute.cs
- TabControlEvent.cs
- entityreference_tresulttype.cs
- BoundsDrawingContextWalker.cs
- SafeArchiveContext.cs
- RankException.cs
- TextParentUndoUnit.cs
- HtmlAnchor.cs
- TargetParameterCountException.cs
- SortExpressionBuilder.cs
- SafePEFileHandle.cs
- TimeSpanOrInfiniteConverter.cs
- ContractHandle.cs
- Configuration.cs
- GridViewRowEventArgs.cs
- NetTcpSectionData.cs
- DataPagerFieldItem.cs
- IndexedWhereQueryOperator.cs
- GlyphRunDrawing.cs
- DataGridViewRowPrePaintEventArgs.cs
- QueryExpr.cs
- Validator.cs
- WebEventTraceProvider.cs
- WeakHashtable.cs
- SkinBuilder.cs
- BindingExpression.cs
- MD5HashHelper.cs
- C14NUtil.cs
- DBCommand.cs
- AudioBase.cs
- DrawingServices.cs
- HttpHeaderCollection.cs
- WebPartZoneAutoFormat.cs
- PointConverter.cs
- ViewBox.cs
- CompilationSection.cs
- BitmapCodecInfo.cs
- precedingquery.cs
- ComponentManagerBroker.cs
- SqlRowUpdatingEvent.cs
- WsiProfilesElementCollection.cs
- TextSpan.cs
- CustomErrorsSection.cs
- Parameter.cs
- HtmlInputButton.cs
- AsyncStreamReader.cs
- SettingsPropertyValueCollection.cs
- AuthStoreRoleProvider.cs
- FormViewPageEventArgs.cs
- MatrixAnimationUsingKeyFrames.cs
- Win32PrintDialog.cs
- XmlDataDocument.cs
- Int32AnimationBase.cs
- IPEndPointCollection.cs
- XmlSchemaGroupRef.cs
- OutputScope.cs
- Message.cs
- NameNode.cs
- ConnectionPoint.cs
- ListBoxItemWrapperAutomationPeer.cs
- PersonalizableAttribute.cs
- ProfessionalColorTable.cs
- MultiTrigger.cs
- DataGridColumnHeadersPresenter.cs
- MonthChangedEventArgs.cs
- AsymmetricKeyExchangeFormatter.cs
- SingleSelectRootGridEntry.cs
- WebPartDisplayModeEventArgs.cs
- RepeatBehaviorConverter.cs
- CustomTrackingQuery.cs
- FloatUtil.cs
- Material.cs
- TypefaceMap.cs
- CodeTypeReferenceCollection.cs
- Grid.cs
- SimpleExpression.cs
- CodeDirectoryCompiler.cs
- RemoteWebConfigurationHost.cs
- ColorAnimationBase.cs
- OdbcErrorCollection.cs
- FixedPageStructure.cs
- DataGridClipboardCellContent.cs
- BackgroundWorker.cs
- DocumentGridContextMenu.cs