Stream.Read

using System;
using System.IO;

public class Block
{
    public static void Main()
    {
        Stream s = new MemoryStream();
        for (int i=0; i<100; i++)
            s.WriteByte((byte)i);
        s.Position = 0;

        // Now read s into a byte buffer.
        byte[] bytes = new byte[s.Length];
        int numBytesToRead = (int) s.Length;
        int numBytesRead = 0;
        while (numBytesToRead > 0)
        {
            // Read may return anything from 0 to numBytesToRead.
            int n = s.Read(bytes, numBytesRead, numBytesToRead);
            // The end of the file is reached.
            if (n==0)
                break;
            numBytesRead += n;
            numBytesToRead -= n;
        }
        s.Close();
        // numBytesToRead should be 0 now, and numBytesRead should
        // equal 100.
        Console.WriteLine("number of bytes read: "+numBytesRead);
    }
}

转载于:https://www.cnblogs.com/lcl_1015/articles/1657294.html

在处理流数据时,`stream.read` 方法是用于从流中读取数据的常用方式之一。不同的编程语言和库提供了各自的实现方式,但其核心思想相似:从流中按字节或字符读取数据,并根据需要处理缓冲、阻塞与非阻塞行为。 在 Python 中,`stream.read` 可以用于文件流、网络流等场景。以下是一个简单的文件流读取示例: ```python # 打开一个文件并读取内容 with open('example.txt', 'rb') as file_stream: data = file_stream.read() # 读取全部内容 print(data) ``` 如果需要逐块读取大文件,可以指定每次读取的字节数: ```python # 分块读取文件内容 with open('large_file.txt', 'rb') as file_stream: chunk_size = 1024 # 每次读取1KB while True: chunk = file_stream.read(chunk_size) if not chunk: break print(chunk) # 处理当前块 ``` 在 C++ 中,Boost.Interprocess 提供了 `bufferstream`,可以用于在内存中进行流式读写操作。以下是一个使用 `boost::interprocess::bufferstream` 读取数据的示例: ```cpp #include <iostream> #include <boost/interprocess/streams/bufferstream.hpp> int main() { // 创建一个缓冲区,大小为100字节 char buffer[100]; // 使用bufferstream将缓冲区包装成流对象 boost::interprocess::bufferstream stream(buffer, sizeof(buffer)); // 写入数据到缓冲区 stream << "Hello, bufferstream!"; // 读取缓冲区中的数据 std::string data; stream >> data; // 输出读取到的数据 std::cout << "Data read from buffer: " << data << std::endl; return 0; } ``` 在 MicroPython 中,流的读写操作由 `mp_stream_rw` 函数实现。该函数负责处理流的读写逻辑,包括检查流是否支持读写操作、调用读写函数、处理非阻塞错误等。这种机制确保了流操作的高效性和可靠性,尤其适用于嵌入式系统中的数据处理[^2]。 对于 .NET 平台,`System.IO.Stream` 类提供了 `Read` 方法来从流中读取数据。以下是一个使用 `MemoryStream` 的示例: ```csharp using System; using System.IO; class Program { static void Main() { byte[] data = System.Text.Encoding.UTF8.GetBytes("Hello, Stream!"); using (MemoryStream memoryStream = new MemoryStream(data)) { byte[] buffer = new byte[1024]; int bytesRead = memoryStream.Read(buffer, 0, buffer.Length); string result = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead); Console.WriteLine(result); } } } ``` .NET 中的 `BufferedStream` 类还可以为其他流提供缓冲功能,从而提高读写性能。该类通过减少对底层流的频繁访问来优化数据传输效率[^1]。 在处理流数据时,需要注意以下几点: - **缓冲机制**:合理使用缓冲可以显著提升性能,例如 .NET 中的 `BufferedStream` 或 Python 中的 `io.BufferedReader`。 - **非阻塞与超时处理**:在网络流或设备流中,应考虑非阻塞模式或设置适当的超时时间,以避免程序长时间挂起。 - **资源释放**:读取完成后,务必释放流所占用的资源,例如调用 `Dispose` 方法或使用 `using` 语句块[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值