Signature | Description |
---|---|
int Read(byte[] buffer, int offset, int count) | Reads a block of bytes from the stream and writes the data to a buffer. |
Task | Asynchronously reads a block of bytes from the stream. |
void Write(byte[] buffer, int offset, int count) | Writes a block of bytes to the stream. |
Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | Asynchronously writes a block of bytes to the stream. |
long Seek(long offset, SeekOrigin origin) | Sets the position within the current stream. |
void Flush() | Clears all buffers for the current stream and causes any buffered data to be written. |
Task FlushAsync(CancellationToken cancellationToken) | Asynchronously clears all buffers for the current stream. |
void Close() | Closes the current stream and releases any resources. |
Overview
The System.IO.Stream
abstract class provides a generic view of a sequence of bytes, such as a file, an I/O device, an inter-process communication pipe, or a TCP/IP socket.
All stream classes inherit from this type, enabling consistent operations for reading, writing, and seeking.
Inheritance
Object
→ MarshalByRefObject
→ System.IO.Stream
Methods ▼
Properties ▼
Property | Description |
---|---|
bool CanRead | Gets a value indicating whether the current stream supports reading. |
bool CanWrite | Gets a value indicating whether the current stream supports writing. |
bool CanSeek | Gets a value indicating whether the current stream supports seeking. |
long Length | Gets the length of the stream in bytes. |
long Position | Gets or sets the current position within the stream. |
Events ▼
The Stream
class does not expose any events directly. Derived classes may provide events such as Disposed
.
Remarks
When creating a custom stream, you must override the abstract members Read
, Write
, Seek
, SetLength
, and the properties CanRead
, CanWrite
, CanSeek
, Length
, and Position
. Implementations should also override Dispose(bool)
to release unmanaged resources.
Example
using System;
using System.IO;
using System.Text;
class UpperCaseStream : Stream
{
private readonly Stream _baseStream;
public UpperCaseStream(Stream baseStream) => _baseStream = baseStream;
public override bool CanRead => _baseStream.CanRead;
public override bool CanSeek => _baseStream.CanSeek;
public override bool CanWrite => _baseStream.CanWrite;
public override long Length => _baseStream.Length;
public override long Position
{
get => _baseStream.Position;
set => _baseStream.Position = value;
}
public override void Flush() => _baseStream.Flush();
public override int Read(byte[] buffer, int offset, int count)
{
int bytesRead = _baseStream.Read(buffer, offset, count);
for (int i = offset; i < offset + bytesRead; i++)
buffer[i] = (byte)char.ToUpper((char)buffer[i]);
return bytesRead;
}
public override void Write(byte[] buffer, int offset, int count)
{
byte[] upper = new byte[count];
for (int i = 0; i < count; i++)
upper[i] = (byte)char.ToUpper((char)buffer[offset + i]);
_baseStream.Write(upper, 0, count);
}
public override long Seek(long offset, SeekOrigin origin) =>
_baseStream.Seek(offset, origin);
public override void SetLength(long value) => _baseStream.SetLength(value);
}
class Program
{
static void Main()
{
using var fs = new FileStream("example.txt", FileMode.Create, FileAccess.Write);
using var ucs = new UpperCaseStream(fs);
byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
ucs.Write(data, 0, data.Length);
}
}