我怎样才能连接的对象,需要写流的对象,想要一个可读流?对象、可读

2023-09-06 08:22:07 作者:人醒梦荒凉

有一个数据集知道如何通过序列中WriteXML到流()。

我有一个对象,想要一个可读的流。我怎样才能将数据集连接到这个其他的对象?

该管道的比喻:假设一个1/2的铜管有男出口和阀也有一个公接头连接两个我需要一个适配器 - 女两端。

我想象的AdapterStream类是多线程感知。流在内部与一个固定的缓冲区来实现,比如8K字节。在一个线程我叫中WriteXML()使用AdaterStream的一个实例。中WriteXML的实现重复调用write()都流直到把它完成。但是AdapterStream块时缓冲区已满,并等待,直到缓冲区通过调用排水阅读()。在一个单独的线程,应用程序调用read()上AdapterStream直到它返回零。

如果缓冲区是空的,这并不意味着阅读()可以返回零。当一个EOF被写入侧信号通知它只能返回零。 (也许与关闭()或WriteEof()方法)。

面向对象这么久了,还没找到对象

您会需要一个ManualResetEvent的要使用的写()和读()之间的信号。

这不会是太难写。但是,是否这样的事情存在吗?

喜欢的东西的BlockingStream斯蒂芬·托布在MSDN杂志规定,除没有无限的缓冲区分配。

解决方案

我没有发现这样的数据流,结束了写作之一。

但! 还有就是要解决这个问题,使用排在匿名管道支持更简单,一般的方式.NET 3.5: I-need-to-return-a-stream-but-component-X-wants-to-write-to-one

A DataSet knows how to serialize to a stream via WriteXml().

I have an object that wants a readable stream. How can I connect the DataSet to this other object?

The plumbing analogy: suppose a 1/2" copper pipe has a "male" outlet, and a valve also has a "male" fitting. To connect the two I need an adapter - female on both ends.

I am imagining an AdapterStream class that is multi-thread-aware. The stream internally is implemented with a fixed buffer, say 8kbytes. On one thread I call WriteXml() using an instance of that AdaterStream. The implementation of WriteXml repeatedly calls Write() to the stream until it's done. But AdapterStream blocks when the buffer is full, and waits until the buffer is drained via a call to Read(). In a separate thread, the app calls Read() on that AdapterStream until it returns zero.

If the buffer is empty that doesn't mean Read() can return zero. It can only return zero when an EOF is signaled by the writing side. (maybe with a Close() or WriteEof() method).

You'd need an ManualResetEvent to be used to signal between the Write() and Read().

It wouldn't be too hard to write. But, does such a thing exist?

Something like the BlockingStream Stephen Taub provided in MSDN Magazine, except without the unlimited buffer allocation.

解决方案

I found no such stream, ending up writing one.

BUT! There is a simpler, general way to solve this, using the anonymous pipes support that came in .NET 3.5: I-need-to-return-a-stream-but-component-X-wants-to-write-to-one .