Javen-Studio 咖啡小屋

http://javenstudio.org - C++ Java 分布式 搜索引擎
Naven's Research Laboratory - Thinking of Life, Imagination of Future

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  24 随笔 :: 57 文章 :: 170 评论 :: 4 Trackbacks

I/O流的设计

C++通用框架的设计 作者:naven

1           I/O流介绍

I/O流,即输入/输出流(Input/Output Stream),是软件框架中的核心系统。对程序设计语言设计者来说,设计一个令人满意的I/O系统,是件极艰巨的任务,也是不可缺少的基础设施。C++的标准模板库已经提供一套很不错的I/O流库了,但是我还是喜欢Java.Net框架提供的那样的使用简单且扩展性强的I/O系统,而且还需要自行做一些改进已融入到C++通用框架体系中,由于I/O系统是框架的基础设施,所以设计它是很关键的任务。

 

本框架的I/O系统设计宗旨与JavaI/O系统设计初衷略有不同(Java I/O的设计原是为了防止classes膨胀,可是事与愿违),主要目标一是要让使用更简单,二是让扩展更简单。本I/O系统参考Java I/O设计,有InputStream/OutputStreamReader/Writer两部分的类,但是含义略有不同,本I/O系统的InputStream/OutputStream定义为流设备,可将系统中任何设备或者内存或者内存中的对象当作输入/输出流设备,它们实现I/O系统最底层的输入/输出的基本操作。而Reader/Writer定义为流设备的读写器,它实现对InputStream/OutputStream的流设备的读写功能,可以组装和扩展读写功能,提供丰富的读写操作符。下面介绍一下I/O流主要的类:

 

主要有如下一些类

 

class AbstractFile                                  表示I/O设备(如FileSocket等)的Abstract基类

class File                                                 文件设备,表示系统的文件、目录等

 

class InputStream                                           表示输入流设备的Abstract基类

class FileInputStream                           表示文件输入流设备

class SocketInputStream                      表示Socket输入流设备

class StringBufferInputStream            表示String对象输入流设备

 

class OutputStream                                        表示输出流设备的Abstract基类

class FileOutputStream                        表示文件输出流设备

class SocketOutputStream                   表示Socket输出流设备

class StringBufferOutputStream         表示String对象输出流设备

class SystemFileOutputStream           表示系统文件(STDOUT等)输出流设备

class ConsoleOutputStream                表示系统控制台输出流设备

class STDERROutputStream               表示系统STDERR输出流设备

class STDOUTOutputStream              表示系统STDOUT输出流设备

 

class Reader                                                     表示读操作器的Abstract基类

class BufferedReader                            表示缓冲的读操作器

class FileReader                                     表示文件的读操作器

class InputStreamReader                     把输入流设备当作的读操作器

class SocketReader                               表示Socket的读操作器

class StringReader                                表示String对象的读操作器

 

class Writer                                                      表示写操作器的Abstract基类

class BufferedWriter                             表示缓冲的写操作器

class FileWriter                             表示文件的写操作器

class OutputStreamWriter                    把输出流设备当作的写操作器

class SocketWriter                                表示Socket的写操作器

class StringWriter                                  表示String对象的写操作器

 

 

2           Hello World!

下面的程序示例如何用上面的类进行读写操作:

 

void main() 
{
    
// 定义一个文件的读操作器,它将自动创建文件输入流设备
    FileReader fr("c:\\temp\\test.txt"); 
    
    
// 通过读操作器构造一个缓冲的读操作器
    BufferedReader rd(fr); 
    String s; 

    
// 一行行读取文件内容,并输出
    
// 注意:每一行自动带“\r\n”
while( rd.readLine(s) > 0 ) {
    
// 截掉后面的 \r\n 字符
       s.rtrimChars(“\r\n”);
        printf(
"%s\r\n", s.c_str()); 
    }


}

 

各个读写操作器还可以组合使用,跟Java的用法类似。

3           AbstactFile

它描述了一个可进行输入输出操作的设备的通用公共的Abstract抽象基类,如文件、目录、Socket等。它定义了一些公共的成员和方法,如果错误号等,还定义了一些子类必须实现的接口,即纯虚方法,所以它是一个Abstract类。它的定义看起来如下所示:

 

class AbstractFile
{
public

    
/**
     * Tests whether the application can read the file denoted by this
     * abstract pathname.
     *
     
*/

    
virtual BOOL canRead() = 0

    
/**
     * Tests whether the application can modify to the file denoted by this
     * abstract pathname.
     *
     
*/

    
virtual BOOL canWrite() = 0

    
/**
     * Tests whether the application can modify and read the file denoted by this
     * abstract pathname.
     *
     
*/

    
virtual BOOL canReadWrite() = 0

}
;

 

4           File

File类是描述操作系统的文件和目录的类,可读取文件的一些属性信息,如创建时间、大小等,也可以通过此类修改这些信息。备注:目录的一些功能还未完全实现。下面的程序展示了通过它来获取文件的属性信息:

 

    File file("C:\\Temp\\main.txt"); 

    printf(
"\n-----File--------\n" 
        
"path = %s name = %s parent = %s\n" 
        
"isAbsolute = %d absolutePath = %s\n" 
        
"exists = %d canRead = %d canWrite = %d canReadWrite = %d\n" 
        
"isDirectory = %d isFile = %d\n" 
        
"lastModified = %d length= %d\n" 
        
"lastModified time = %s\n"
        file.getPath().c_str(), 
// 路径
        file.getName().c_str(), // 文件名
        file.getParent().c_str(), // 父级目录
        file.isAbsolute(),      // 是否绝对路径
        file.getAbsolutePath().c_str(), // 绝对路径
        file.exists(), file.canRead(), file.canWrite(), file.canReadWrite(), 
        file.isDirectory(), file.isFile(), 
// 是否文件或目录
        file.lastModified(), // 最近修改时间
        file.length(), // 文件长度
        tff.format(file.lastModified()).c_str()); // 最近修改时间

 

5           InputStream

它描述了一个输入流设备的通用公共的Abstract抽象基类,每一种数据源都有相应的InputStream子类,如文件输入流、Socket输入流、字符串输入流等。它跟AbstractFile类似,定义公共的方法,也定义了一些子类需要实现的接口。InputStream流设备实现的接口均是最基础的操作,如读取一个byte字节的数据,或者读取指定长度的数据,它的定义看起来如下所示:

 

class InputStream 
{
protected
    
/**
     * Offset for the file handle from origin.
     
*/

    
long _position; 

    
/*
     * Is this stream closed? 
     
*/

    BOOL _closed;

public
    
    
/**
     * Report position in input stream.
     
*/
 
    
virtual long tellp() return _position; } 

    
/**
     * Returns the number of bytes that can be read (or skipped over) 
     * from this input stream without blocking by the next caller of 
     * a method for this input stream. 
     
*/
 
    
virtual int available() = 0
    
    
/**
     * Reads the next byte of data from the input stream. 
     
*/

    
virtual int read() = 0
    
    
/**
     * Reads some number of bytes from the input stream and stores 
     * them into the buffer array b.
     
*/

    
virtual int read(void *b, int len) = 0
    
    
/**
     * Reads up to len bytes of data from the input stream into 
     * an array of bytes.
     
*/

    
virtual int read(void *b, int off, int len) = 0
    
    
/**
     * Skips over and discards n bytes of data from this input stream. 
     
*/

    
virtual long skip(long n) = 0

    
/**
     * Closes this input stream and releases any system resources 
     * associated with the stream. 
     
*/

    
virtual void close() = 0
    
}
;

 

6           OutputStream

InputStream输入流相对应,它描述了一个输出流设备的通用公共的Abstract抽象基类,每一种数据源都有相应的OuputStream子类,如文件输出流、Socket输出流、字符串输出流等。它的接口定义看起来如下所示:

 

class OutputStream 
{
protected
    
/**
     * Offset for the file handle from origin.
     
*/

    
long _position; 

    
/*
     * Is this stream closed? 
     
*/

    BOOL _closed;

public
    
    
/**
     * Report position in output stream.
     
*/
 
    
virtual long tellp() return _position; } 
    
    
/**
     * Flushes this output stream and forces any buffered 
     * output bytes to be written out.
     
*/

    
virtual void flush() = 0
    
    
/**
     * Writes len bytes from the specified byte array to 
     * this output stream. 
     
*/
 
    
virtual OutputStream& write(const void *b, int len) = 0
    
    
/**
     * Writes len bytes from the specified byte array to 
     * this output stream. 
     
*/
 
    
virtual OutputStream& write(const void *b, int off, int len) = 0

    
/**
     * Writes the specified byte to this output stream. 
     
*/
 
    
virtual OutputStream& write(int b) = 0
    
    
/**
     * Closes this output stream and releases any system 
     * resources associated with this stream. 
     
*/
 
    
virtual void close() = 0
    
}
;

 

7           Reader

它描述了一个对流设备的读操作器的通用公共的Abstract抽象基类,每一种输入流设备都有相应的读操作器,如文件读操作器、Socket读操作器、字符串读操作器等。它的定义与Java有所区别,Java里的Reader主要用于“相容于Unicode并基于字符”的数据源的读取操作,而本系统的Reader是用于所有输入流设备进行的读操作。另外某些Reader可以通过另一个Reader来进行创建,以实现比较复杂的读取操作,如带缓冲的或一行行的读取。

 

它的接口定义看起来如下所示:

 

class Reader 
{
public

    
/**
     * Check this stream if is closed. 
     
*/

    BOOL isClosed(); 

    
/**
     * Report position in input stream.
     
*/
 
    
virtual long tellp() = 0

    
/**
     * Read a single character. 
     
*/

    
virtual int read() = 0

    
/**
     * Read characters into a portion of an array. 
     
*/

    
virtual int read(char *cbuf, int len) = 0

    
/**
     * Read characters into a portion of an array.  
     
*/

    
virtual int read(char *cbuf, int off, int len) = 0

    
/**
     * Reads up to <code>len</code> bytes of data from this input stream
     * into an array of bytes. 
     
*/

    
int read(String &b, int len); 

    
/**
     * Reads up to <code>len</code> bytes of data from this input stream
     * into an array of bytes. 
     
*/

    
int read(String &b, int off, int len); 

    
/**
     * Skip characters.
     
*/

    
virtual long skip(long n) = 0

    
/**
     * Tell whether this stream is ready to be read.
     
*/

    
virtual BOOL ready(); 

    
/**
     * Tell whether this stream supports the mark() operation. 
     
*/

    
virtual BOOL markSupported(); 

    
/**
     * Mark the present position in the stream. 
     
*/

    
virtual BOOL mark(int readAheadLimit); 

    
/**
     * Reset the stream. 
     
*/

    
virtual void reset(); 

    
/**
     * Close the stream. 
     
*/

    
virtual void close(); 

}
;

 

8           Writer

Reader对应,它描述了一个对流设备的写操作器的通用公共的Abstract抽象基类,每一种输出流设备都有相应的写操作器,如文件写操作器、Socket写操作器、字符串写操作器等。同样,它也是用于所有输出流设备进行的写操作。某些Writer也可以通过另一个Writer来进行创建,以实现比较复杂的写操作,如带缓冲的写操作。

 

考虑到C++的特性,本Writer还实现了所有的Operator<<操作符,所以可以进行与C++标准库类似的格式化输出,如下所示:

 

    FileWriter fw("c:\\temp\\test.txt"); 

    fw 
<< "This is the NOTICE log message" 
       
<< 123456 << "    " 
       
<< String("fdsafdas  ");

 

它的接口定义看起来如下所示:

 

class Writer 
{
public
    
/**
     * Check this stream if is closed. 
     
*/

    BOOL isClosed(); 

    
/**
     * Report position in output stream.
     
*/
 
    
virtual long tellp() = 0

    
/**
     * Write a single character.
     
*/

    
virtual Writer& write(int c) = 0

    
/**
     * Write an array of characters.
     
*/

    
virtual Writer& write(const char *cbuf) = 0

    
/**
     * Write a portion of an array of characters.
     
*/

    
virtual Writer& write(const char *cbuf, int len) = 0

    
/**
     * Write a portion of an array of characters.
     
*/

    
virtual Writer& write(const char *cbuf, int off, int len) = 0

    
/**
     * Write a string.
     
*/

    
virtual Writer& write(String &str) = 0

    
/**
     * Write a portion of a string.
     
*/

    
virtual Writer& write(String &str, int len) = 0

    
/**
     * Write a portion of a string.
     
*/

    
virtual Writer& write(String &str, int off, int len) = 0

    
/**
     * Flush the stream. 
     
*/

    
virtual void flush(); 

    
/**
     * Close the stream. 
     
*/

    
virtual void close(); 

}
;

 

9           InputStreamReader类和OuputStreamWriter

这是两个特殊读写操作器类,它的设计是运用Bridge模式,可以将一个InputStream流设备或OuputStream流设备转换成一个Reader或一个Writer。所以读取文件的操作,通过它们也可以如下实现:

 

void main() 
{
    FileInputStream fis(
"c:\\temp\\test.txt"); 
    
    InputStreamReader isr(fis); 
    
    BufferedReader rd(isr); 
    String s; 

    
while( rd.readLine(s) > 0 ) {
        printf(
"%s\r\n", s.c_str()); 
    }


}

 

 

 

C++通用框架的设计 作者:naven 日期:2005-11-9

posted on 2005-11-10 01:02 Javen-Studio 阅读(4104) 评论(7)  编辑 收藏 引用

评论

# I/O流的设计(Java的InputStream/OuputStream和Reader/Writer的C 实现)[TrackBack] 2005-11-10 01:16 Naven
I/O流,即输入/输出流(Input/Output Stream),是软件框架中的核心系统。对程序设计语言设计者来说,设计一个令人满意的I/O系统,是件极艰巨的任务,也是不可缺少的基础设施。C 的标准模板库已经提供一套很不错的I/O流库了,但是我还是喜欢Java和.Net框架提供的那样的使用简单且扩展性强的I/O系统,而且还需要自行做一些改进已融入到C 通用框架体系中,由于I/O系统是框架的基础设施,所以设计它是很关键的任务。


阅读请点 http://www.cppblog.com/javenstudio/articles/1028.html...  查看原文  回复  更多评论
  

# re: I/O流的设计(Java的InputStream/OuputStream和Reader/Writer的C++实现) 2005-11-11 09:02 Dash
挺不错的~正好提醒了我一直想去自己模仿Java OR C++的I/O库 写一个的想法,谢谢~  回复  更多评论
  

# re: I/O流的设计(Java的InputStream/OuputStream和Reader/Writer的C++实现) 2006-07-11 14:04 yazi
正是我需要的
谢谢斑竹  回复  更多评论
  

# re: I/O流的设计(Java的InputStream/OuputStream和Reader/Writer的C++实现) 2007-01-04 16:29 reader
有source code 提供吗?谢谢。  回复  更多评论
  

# re: I/O流的设计(Java的InputStream/OuputStream和Reader/Writer的C++实现) 2007-01-05 09:58 Javen-Studio
首页有下载,全部src暂未提供  回复  更多评论
  

# re: I/O流的设计(Java的InputStream/OuputStream和Reader/Writer的C++实现) 2010-01-12 15:01 wind
不错,其中的SocketInputStream很符合我的想法,哈哈
感觉Java的Socket也应该这么设计啊,支持你,呵呵  回复  更多评论
  

# re: I/O流的设计(Java的InputStream/OuputStream和Reader/Writer的C++实现) 2010-01-13 09:21 naven
heihei, i think so  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理