OpenRTMFP/Cumulus Primer(12)IO管理之IO流(续)

OpenRTMFP/Cumulus Primer(12)IO管理之IO流(续)

  • Author: 柳大·Poechant(钟超)
  • Email: zhongchao.ustc#gmail.com (#->@)
  • Blog: Blog.CSDN.net/Poechant
  • Date: April 24th, 2012

0 导语

本文所介绍的 CumulusServer 中用到的 IO 流,是基于 CumulusServer 中的 MemoryIOS 的,可以先阅读《OpenRTMFP/Cumulus Primer(11)IO 管理之IO流》一文,然后再阅读本文。

1 输入流

class MemoryInputStream: public MemoryIOS, public std::istream
{
public:
    MemoryInputStream(const char* pBuffer,Poco::UInt32 bufferSize);
        /// Creates a MemoryInputStream for the given memory area,
        /// ready for reading.
    MemoryInputStream(MemoryInputStream&);
    ~MemoryInputStream();
        /// Destroys the MemoryInputStream.
    char*           current();
};

构造函数、拷贝构造函数和析构函数也都没什么可说的,初始化 MemoryIOS 以及 istream。istream 是 iostream 中的 basic_istream  别名(typedef)。

MemoryInputStream::MemoryInputStream(const char* pBuffer, UInt32 bufferSize): 
    MemoryIOS(const_cast<char*>(pBuffer), bufferSize), istream(rdbuf()) {
}

MemoryInputStream::MemoryInputStream(MemoryInputStream& other):
    MemoryIOS(other), istream(rdbuf()) {
}

MemoryInputStream::~MemoryInputStream() {
}

唯一的一个成员函数是 current,封装了 MemoryIOS 的 MemoryStreamBuf 成员的 gCurrent 函数:

inline char* MemoryInputStream::current() {
    return rdbuf()->gCurrent();
}

2 输出流

class MemoryOutputStream: public MemoryIOS, public std::ostream
    /// An input stream for reading from a memory area.
{
public:
    MemoryOutputStream(char* pBuffer,Poco::UInt32 bufferSize);
        /// Creates a MemoryOutputStream for the given memory area,
        /// ready for writing.
    MemoryOutputStream(MemoryOutputStream&);
    ~MemoryOutputStream();
        /// Destroys the MemoryInputStream.

    Poco::UInt32    written();
    void            written(Poco::UInt32 size);
    char*           current();
};

2.1 构造函数、拷贝构造函数和析构函数

如下,不赘述了。

MemoryOutputStream::MemoryOutputStream(char* pBuffer, UInt32 bufferSize): 
    MemoryIOS(pBuffer, bufferSize), ostream(rdbuf()) {
}
MemoryOutputStream::MemoryOutputStream(MemoryOutputStream& other):
    MemoryIOS(other), ostream(rdbuf()) {
}

MemoryOutputStream::~MemoryOutputStream(){
}

2.2 读取和设定已写字节数

读取:

inline Poco::UInt32 MemoryOutputStream::written() {
    return rdbuf()->written();
}

设定:

inline void MemoryOutputStream::written(Poco::UInt32 size) {
    rdbuf()->written(size);
}

2.3 当前位置

与 MemoryInputStream 中的封装类似:

inline char* MemoryOutputStream::current() {
    return rdbuf()->pCurrent();
}

-

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant

-

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值