public sealed class BufferedStream : Stream
|
BufferedStream can be composed around certain types of streams. It provides implementations for reading and writing bytes to an underlying data source or repository. Use BinaryReader and BinaryWriter for reading and writing other data types.BufferedStream is designed to prevent the buffer from slowing down input and output when the buffer is not needed. If you always read and write for sizes greater than the internal buffer size, then BufferedStream might not even allocate the internal buffer.BufferedStream also buffers reads and writes in a shared buffer. It is assumed that you will almost always be doing a series of reads or writes, but rarely alternate between the two of them.
ctor #1 | Overloaded:.ctor(Stream stream) Initializes a new instance of the BufferedStream class with a default buffer size of 4096 bytes. |
ctor #2 | Overloaded:.ctor(Stream stream, int bufferSize) Initializes a new instance of the BufferedStream class with the specified buffer size. |
CanRead | Read-only Overridden: Gets a value indicating whether the current stream supports reading. |
CanSeek | Read-only Overridden: Gets a value indicating whether the current stream supports seeking. |
CanWrite | Read-only Overridden: Gets a value indicating whether the current stream supports writing. |
Length | Read-only Overridden: Gets the stream length in bytes. |
Position | Read-write Overridden: Gets the position within the current stream. |
BeginRead (inherited from System.IO.Stream) |
See base class member description: System.IO.Stream.BeginRead Begins an asynchronous read operation. |
BeginWrite (inherited from System.IO.Stream) |
See base class member description: System.IO.Stream.BeginWrite Begins an asynchronous write operation. |
Close | Overridden: Closes the stream and releases any resources (especially system resources such as sockets and file handles) associated with the current buffered stream. |
CreateObjRef (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
EndRead (inherited from System.IO.Stream) |
See base class member description: System.IO.Stream.EndRead Waits for the pending asynchronous read to complete. |
EndWrite (inherited from System.IO.Stream) |
See base class member description: System.IO.Stream.EndWrite Ends an asynchronous write operation. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
Flush | Overridden: Clears all buffers for this stream and causes any buffered data to be written to the underlying device. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
InitializeLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. |
Read | Overridden: Copies bytes from the current buffered stream to an array. |
ReadByte | Overridden: Reads a byte from the underlying stream and returns the byte cast to an int, or returns -1 if reading from the end of the stream. |
Seek | Overridden: Sets the position within the current buffered stream. |
SetLength | Overridden: Sets the length of the buffered stream. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Write | Overridden: Copies bytes to the buffered stream and advances the current position within the buffered stream by the number of bytes written. |
WriteByte | Overridden: Writes a byte to the current position in the buffered stream. |
CreateWaitHandle (inherited from System.IO.Stream) |
See base class member description: System.IO.Stream.CreateWaitHandle Allocates a WaitHandle object. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public BufferedStream( |
stream
Exception Type | Condition |
---|---|
ArgumentNullException | stream is null. |
stream
bufferSize
Exception Type | Condition |
---|---|
ArgumentNullException | stream is null. |
ArgumentOutOfRangeException | bufferSize is negative. |
public override bool CanRead {get;}
|
If the stream is closed, this property returns false.
public override bool CanSeek {get;}
|
If the stream is closed, this property returns false.
public override bool CanWrite {get;}
|
If the stream is closed, this property returns false.
public override long Length {get;}
|
Exception Type | Condition |
---|---|
IOException | The underlying stream is null or closed. |
NotSupportedException | The stream does not support seeking. |
ObjectDisposedException | Methods were called after the stream was closed. |
public override long Position {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The value passed to BufferedStream.Seek is negative. |
IOException | An I/O error occurs, such as the stream being closed. |
NotSupportedException | The stream does not support seeking. |
ObjectDisposedException | Methods were called after the stream was closed. |
The set accessor copies any data previously written to the buffer to the underlying stream, and then invokes BufferedStream.Seek.
Seeking to any location beyond the length of the stream is supported.
public virtual IAsyncResult BeginRead( |
buffer
offset
count
callback
state
Exception Type | Condition |
---|---|
IOException | Attempted an asynchronous read past the end of the file, or a disk error occurs. |
ArgumentException | One or more or the arguments is invalid. |
ObjectDisposedException | Methods were called after the stream was closed. |
NotSupportedException | The current Stream implementation does not support the read operation. |
The current position in the stream is updated when the asynchronous read or write is issued, not when the I/O operation completes.
Multiple simultaneous asynchronous requests render the request completion order uncertain.
Use the Stream.CanRead property to determine whether the current instance supports reading.
If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from BeginRead. Errors that occur during an asynchronous read request, such as a disk failure during the I/O request, occur on the threadpool thread and become visible upon a call to EndRead. Exceptions thrown by the threadpool thread will not be visible when calling Stream.EndWrite.
public virtual IAsyncResult BeginWrite( |
buffer
offset
count
callback
state
Exception Type | Condition |
---|---|
IOException | Attempted an asynchronous write past the end of the file, or a disk error occurs. |
ArgumentException | One or more or the arguments is invalid. |
ObjectDisposedException | Methods were called after the stream was closed. |
NotSupportedException | The current Stream implementation does not support the write operation. |
If a stream is writable, writing at the end of the stream expands the stream.
The current position in the stream is updated when you issue the asynchronous read or write, not when the I/O operation completes. Multiple simultaneous asynchronous requests render the request completion order uncertain.
Use the Stream.CanWrite property to determine whether the current instance supports writing.
If a stream is closed or you pass an invalid argument, exceptions are thrown immediately from BeginWrite. Errors that occur during an asynchronous write request, such as a disk failure during the I/O request, occur on the threadpool thread and become visible upon a call to EndWrite. Exceptions thrown by the threadpool thread will not be visible when calling EndWrite.
public override void Close(); |
Exception Type | Condition |
---|---|
IOException | An error occurred while trying to close the stream. |
Flushing the stream will not flush its underlying encoder unless you explicitly call BufferedStream.Flush or Close. Setting StreamWriter.AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.
Attempting to manipulate a stream after it has been closed might throw an ObjectDisposedException.
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
protected virtual WaitHandle CreateWaitHandle(); |
Use this method if you implement the asynchronous methods and require a way of blocking in Stream.EndRead or Stream.EndWrite until the asynchronous operation is complete.
public virtual int EndRead( |
asyncResult
Exception Type | Condition |
---|---|
ArgumentNullException | asyncResult is null. |
ArgumentException | asyncResult did not originate from a Stream.BeginRead method on the current stream. |
EndRead can be called once on every IAsyncResult from Stream.BeginRead.
This method blocks until the I/O operation has completed.
public virtual void EndWrite( |
asyncResult
Exception Type | Condition |
---|---|
ArgumentNullException | asyncResult is null. |
ArgumentException | asyncResult did not originate from a Stream.BeginWrite method on the current stream. |
This method blocks until the I/O operation has completed. Errors that occur during an asynchronous write request, such as a disk failure during the I/O request, occur on the threadpool thread and become visible upon a call to EndWrite. Exceptions thrown by the threadpool thread will not be visible when calling EndWrite.
~BufferedStream(); |
public override void Flush(); |
Exception Type | Condition |
---|---|
IOException | The data source or repository is not open. |
All the read and write methods of BufferedStream automatically maintain the buffer, so there is no need to invoke Flush when switching back and forth between reading and writing.
public virtual int GetHashCode(); |
public object GetLifetimeService(); |
public Type GetType(); |
public virtual object InitializeLifetimeService(); |
public class MyClass : MarshalByRefObject { public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } }
protected object MemberwiseClone(); |
array
offset
count
Exception Type | Condition |
---|---|
ArgumentException | Length of array minus offset is less than count. |
ArgumentNullException | array is null. |
ArgumentOutOfRangeException | offset or count is negative. |
IOException | The stream is not open or is null. |
NotSupportedException | The stream does not support reading. |
ObjectDisposedException | Methods were called after the stream was closed. |
Use BinaryReader for reading primitive data types.
public override int ReadByte(); |
Exception Type | Condition |
---|---|
IOException | An I/O error occurs, such as the stream being closed. |
NotSupportedException | The stream does not support reading. |
ObjectDisposedException | Methods were called after the stream was closed. |
public override long Seek(long offset, Seek( |
offset
origin
Exception Type | Condition |
---|---|
IOException | The stream is not open or is null. |
NotSupportedException | The stream does not support seeking. |
ObjectDisposedException | Methods were called after the stream was closed. |
Seeking to any location beyond the length of the stream is supported.
public override void SetLength( |
value
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | value is negative. |
IOException | The stream is not open or is null. |
NotSupportedException | The stream does not support both writing and seeking. |
ObjectDisposedException | Methods were called after the stream was closed. |
SetLength flushes any buffered writes if necessary.
A stream must support both writing and seeking for SetLength to work.
public virtual string ToString(); |
array
offset
count
Exception Type | Condition |
---|---|
ArgumentException | Length of array minus offset is less than count. |
ArgumentNullException | array is null. |
ArgumentOutOfRangeException | offset or count is negative. |
IOException | The stream is closed or null. |
NotSupportedException | The stream does not support writing. |
ObjectDisposedException | Methods were called after the stream was closed. |
public override void WriteByte( |
value
Exception Type | Condition |
---|---|
NotSupportedException | The stream does not support writing. |
ArgumentNullException | value is null. |
ObjectDisposedException | Methods were called after the stream was closed. |