• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Javen-Studio 咖啡小屋

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

              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              24 隨筆 :: 57 文章 :: 170 評(píng)論 :: 4 Trackbacks

            I/O流的設(shè)計(jì)

            C++通用框架的設(shè)計(jì) 作者:naven

            1           I/O流介紹

            I/O流,即輸入/輸出流(Input/Output Stream),是軟件框架中的核心系統(tǒng)。對(duì)程序設(shè)計(jì)語(yǔ)言設(shè)計(jì)者來(lái)說(shuō),設(shè)計(jì)一個(gè)令人滿(mǎn)意的I/O系統(tǒng),是件極艱巨的任務(wù),也是不可缺少的基礎(chǔ)設(shè)施。C++的標(biāo)準(zhǔn)模板庫(kù)已經(jīng)提供一套很不錯(cuò)的I/O流庫(kù)了,但是我還是喜歡Java.Net框架提供的那樣的使用簡(jiǎn)單且擴(kuò)展性強(qiáng)的I/O系統(tǒng),而且還需要自行做一些改進(jìn)已融入到C++通用框架體系中,由于I/O系統(tǒng)是框架的基礎(chǔ)設(shè)施,所以設(shè)計(jì)它是很關(guān)鍵的任務(wù)。

             

            本框架的I/O系統(tǒng)設(shè)計(jì)宗旨與JavaI/O系統(tǒng)設(shè)計(jì)初衷略有不同(Java I/O的設(shè)計(jì)原是為了防止classes膨脹,可是事與愿違),主要目標(biāo)一是要讓使用更簡(jiǎn)單,二是讓擴(kuò)展更簡(jiǎn)單。本I/O系統(tǒng)參考Java I/O設(shè)計(jì),有InputStream/OutputStreamReader/Writer兩部分的類(lèi),但是含義略有不同,本I/O系統(tǒng)的InputStream/OutputStream定義為流設(shè)備,可將系統(tǒng)中任何設(shè)備或者內(nèi)存或者內(nèi)存中的對(duì)象當(dāng)作輸入/輸出流設(shè)備,它們實(shí)現(xiàn)I/O系統(tǒng)最底層的輸入/輸出的基本操作。而Reader/Writer定義為流設(shè)備的讀寫(xiě)器,它實(shí)現(xiàn)對(duì)InputStream/OutputStream的流設(shè)備的讀寫(xiě)功能,可以組裝和擴(kuò)展讀寫(xiě)功能,提供豐富的讀寫(xiě)操作符。下面介紹一下I/O流主要的類(lèi):

             

            主要有如下一些類(lèi)

             

            class AbstractFile                                  表示I/O設(shè)備(如FileSocket等)的Abstract基類(lèi)

            class File                                                 文件設(shè)備,表示系統(tǒng)的文件、目錄等

             

            class InputStream                                           表示輸入流設(shè)備的Abstract基類(lèi)

            class FileInputStream                           表示文件輸入流設(shè)備

            class SocketInputStream                      表示Socket輸入流設(shè)備

            class StringBufferInputStream            表示String對(duì)象輸入流設(shè)備

             

            class OutputStream                                        表示輸出流設(shè)備的Abstract基類(lèi)

            class FileOutputStream                        表示文件輸出流設(shè)備

            class SocketOutputStream                   表示Socket輸出流設(shè)備

            class StringBufferOutputStream         表示String對(duì)象輸出流設(shè)備

            class SystemFileOutputStream           表示系統(tǒng)文件(STDOUT等)輸出流設(shè)備

            class ConsoleOutputStream                表示系統(tǒng)控制臺(tái)輸出流設(shè)備

            class STDERROutputStream               表示系統(tǒng)STDERR輸出流設(shè)備

            class STDOUTOutputStream              表示系統(tǒng)STDOUT輸出流設(shè)備

             

            class Reader                                                     表示讀操作器的Abstract基類(lèi)

            class BufferedReader                            表示緩沖的讀操作器

            class FileReader                                     表示文件的讀操作器

            class InputStreamReader                     把輸入流設(shè)備當(dāng)作的讀操作器

            class SocketReader                               表示Socket的讀操作器

            class StringReader                                表示String對(duì)象的讀操作器

             

            class Writer                                                      表示寫(xiě)操作器的Abstract基類(lèi)

            class BufferedWriter                             表示緩沖的寫(xiě)操作器

            class FileWriter                             表示文件的寫(xiě)操作器

            class OutputStreamWriter                    把輸出流設(shè)備當(dāng)作的寫(xiě)操作器

            class SocketWriter                                表示Socket的寫(xiě)操作器

            class StringWriter                                  表示String對(duì)象的寫(xiě)操作器

             

             

            2           Hello World!

            下面的程序示例如何用上面的類(lèi)進(jìn)行讀寫(xiě)操作:

             

            void main() 
            {
                
            // 定義一個(gè)文件的讀操作器,它將自動(dòng)創(chuàng)建文件輸入流設(shè)備
                FileReader fr("c:\\temp\\test.txt"); 
                
                
            // 通過(guò)讀操作器構(gòu)造一個(gè)緩沖的讀操作器
                BufferedReader rd(fr); 
                String s; 

                
            // 一行行讀取文件內(nèi)容,并輸出
                
            // 注意:每一行自動(dòng)帶“\r\n”
            while( rd.readLine(s) > 0 ) {
                
            // 截掉后面的 \r\n 字符
                   s.rtrimChars(“\r\n”);
                    printf(
            "%s\r\n", s.c_str()); 
                }


            }

             

            各個(gè)讀寫(xiě)操作器還可以組合使用,跟Java的用法類(lèi)似。

            3           AbstactFile類(lèi)

            它描述了一個(gè)可進(jìn)行輸入輸出操作的設(shè)備的通用公共的Abstract抽象基類(lèi),如文件、目錄、Socket等。它定義了一些公共的成員和方法,如果錯(cuò)誤號(hào)等,還定義了一些子類(lèi)必須實(shí)現(xiàn)的接口,即純虛方法,所以它是一個(gè)Abstract類(lèi)。它的定義看起來(lái)如下所示:

             

            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類(lèi)

            File類(lèi)是描述操作系統(tǒng)的文件和目錄的類(lèi),可讀取文件的一些屬性信息,如創(chuàng)建時(shí)間、大小等,也可以通過(guò)此類(lèi)修改這些信息。備注:目錄的一些功能還未完全實(shí)現(xiàn)。下面的程序展示了通過(guò)它來(lái)獲取文件的屬性信息:

             

                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(), // 父級(jí)目錄
                    file.isAbsolute(),      // 是否絕對(duì)路徑
                    file.getAbsolutePath().c_str(), // 絕對(duì)路徑
                    file.exists(), file.canRead(), file.canWrite(), file.canReadWrite(), 
                    file.isDirectory(), file.isFile(), 
            // 是否文件或目錄
                    file.lastModified(), // 最近修改時(shí)間
                    file.length(), // 文件長(zhǎng)度
                    tff.format(file.lastModified()).c_str()); // 最近修改時(shí)間

             

            5           InputStream類(lèi)

            它描述了一個(gè)輸入流設(shè)備的通用公共的Abstract抽象基類(lèi),每一種數(shù)據(jù)源都有相應(yīng)的InputStream子類(lèi),如文件輸入流、Socket輸入流、字符串輸入流等。它跟AbstractFile類(lèi)似,定義公共的方法,也定義了一些子類(lèi)需要實(shí)現(xiàn)的接口。InputStream流設(shè)備實(shí)現(xiàn)的接口均是最基礎(chǔ)的操作,如讀取一個(gè)byte字節(jié)的數(shù)據(jù),或者讀取指定長(zhǎng)度的數(shù)據(jù),它的定義看起來(lái)如下所示:

             

            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類(lèi)

            InputStream輸入流相對(duì)應(yīng),它描述了一個(gè)輸出流設(shè)備的通用公共的Abstract抽象基類(lèi),每一種數(shù)據(jù)源都有相應(yīng)的OuputStream子類(lèi),如文件輸出流、Socket輸出流、字符串輸出流等。它的接口定義看起來(lái)如下所示:

             

            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類(lèi)

            它描述了一個(gè)對(duì)流設(shè)備的讀操作器的通用公共的Abstract抽象基類(lèi),每一種輸入流設(shè)備都有相應(yīng)的讀操作器,如文件讀操作器、Socket讀操作器、字符串讀操作器等。它的定義與Java有所區(qū)別,Java里的Reader主要用于“相容于Unicode并基于字符”的數(shù)據(jù)源的讀取操作,而本系統(tǒng)的Reader是用于所有輸入流設(shè)備進(jìn)行的讀操作。另外某些Reader可以通過(guò)另一個(gè)Reader來(lái)進(jìn)行創(chuàng)建,以實(shí)現(xiàn)比較復(fù)雜的讀取操作,如帶緩沖的或一行行的讀取。

             

            它的接口定義看起來(lái)如下所示:

             

            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類(lèi)

            Reader對(duì)應(yīng),它描述了一個(gè)對(duì)流設(shè)備的寫(xiě)操作器的通用公共的Abstract抽象基類(lèi),每一種輸出流設(shè)備都有相應(yīng)的寫(xiě)操作器,如文件寫(xiě)操作器、Socket寫(xiě)操作器、字符串寫(xiě)操作器等。同樣,它也是用于所有輸出流設(shè)備進(jìn)行的寫(xiě)操作。某些Writer也可以通過(guò)另一個(gè)Writer來(lái)進(jìn)行創(chuàng)建,以實(shí)現(xiàn)比較復(fù)雜的寫(xiě)操作,如帶緩沖的寫(xiě)操作。

             

            考慮到C++的特性,本Writer還實(shí)現(xiàn)了所有的Operator<<操作符,所以可以進(jìn)行與C++標(biāo)準(zhǔn)庫(kù)類(lèi)似的格式化輸出,如下所示:

             

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

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

             

            它的接口定義看起來(lái)如下所示:

             

            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類(lèi)和OuputStreamWriter類(lèi)

            這是兩個(gè)特殊讀寫(xiě)操作器類(lèi),它的設(shè)計(jì)是運(yùn)用Bridge模式,可以將一個(gè)InputStream流設(shè)備或OuputStream流設(shè)備轉(zhuǎn)換成一個(gè)Reader或一個(gè)Writer。所以讀取文件的操作,通過(guò)它們也可以如下實(shí)現(xiàn):

             

            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++通用框架的設(shè)計(jì) 作者:naven 日期:2005-11-9

            posted on 2005-11-10 01:02 Javen-Studio 閱讀(4202) 評(píng)論(7)  編輯 收藏 引用

            評(píng)論

            # I/O流的設(shè)計(jì)(Java的InputStream/OuputStream和Reader/Writer的C 實(shí)現(xiàn))[TrackBack] 2005-11-10 01:16 Naven
            I/O流,即輸入/輸出流(Input/Output Stream),是軟件框架中的核心系統(tǒng)。對(duì)程序設(shè)計(jì)語(yǔ)言設(shè)計(jì)者來(lái)說(shuō),設(shè)計(jì)一個(gè)令人滿(mǎn)意的I/O系統(tǒng),是件極艱巨的任務(wù),也是不可缺少的基礎(chǔ)設(shè)施。C 的標(biāo)準(zhǔn)模板庫(kù)已經(jīng)提供一套很不錯(cuò)的I/O流庫(kù)了,但是我還是喜歡Java和.Net框架提供的那樣的使用簡(jiǎn)單且擴(kuò)展性強(qiáng)的I/O系統(tǒng),而且還需要自行做一些改進(jìn)已融入到C 通用框架體系中,由于I/O系統(tǒng)是框架的基礎(chǔ)設(shè)施,所以設(shè)計(jì)它是很關(guān)鍵的任務(wù)。


            閱讀請(qǐng)點(diǎn) http://www.shnenglu.com/javenstudio/articles/1028.html...  查看原文  回復(fù)  更多評(píng)論
              

            # re: I/O流的設(shè)計(jì)(Java的InputStream/OuputStream和Reader/Writer的C++實(shí)現(xiàn)) 2005-11-11 09:02 Dash
            挺不錯(cuò)的~正好提醒了我一直想去自己模仿Java OR C++的I/O庫(kù) 寫(xiě)一個(gè)的想法,謝謝~  回復(fù)  更多評(píng)論
              

            # re: I/O流的設(shè)計(jì)(Java的InputStream/OuputStream和Reader/Writer的C++實(shí)現(xiàn)) 2006-07-11 14:04 yazi
            正是我需要的
            謝謝斑竹  回復(fù)  更多評(píng)論
              

            # re: I/O流的設(shè)計(jì)(Java的InputStream/OuputStream和Reader/Writer的C++實(shí)現(xiàn)) 2007-01-04 16:29 reader
            有source code 提供嗎?謝謝。  回復(fù)  更多評(píng)論
              

            # re: I/O流的設(shè)計(jì)(Java的InputStream/OuputStream和Reader/Writer的C++實(shí)現(xiàn)) 2007-01-05 09:58 Javen-Studio
            首頁(yè)有下載,全部src暫未提供  回復(fù)  更多評(píng)論
              

            # re: I/O流的設(shè)計(jì)(Java的InputStream/OuputStream和Reader/Writer的C++實(shí)現(xiàn)) 2010-01-12 15:01 wind
            不錯(cuò),其中的SocketInputStream很符合我的想法,哈哈
            感覺(jué)Java的Socket也應(yīng)該這么設(shè)計(jì)啊,支持你,呵呵  回復(fù)  更多評(píng)論
              

            # re: I/O流的設(shè)計(jì)(Java的InputStream/OuputStream和Reader/Writer的C++實(shí)現(xiàn)) 2010-01-13 09:21 naven
            heihei, i think so  回復(fù)  更多評(píng)論
              


            只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            国产午夜精品理论片久久影视| 国产精品久久久福利| 久久国产精品波多野结衣AV| 久久亚洲AV成人无码电影| 久久只有这里有精品4| 精品国产一区二区三区久久蜜臀| 97久久久精品综合88久久| 日韩乱码人妻无码中文字幕久久| 国内精品久久国产| 午夜福利91久久福利| 久久伊人色| 亚洲精品无码专区久久同性男| 久久精品国产一区二区三区| 国产精品嫩草影院久久| 久久99精品九九九久久婷婷| 国产午夜福利精品久久| 国产亚洲成人久久| 蜜臀久久99精品久久久久久| 老司机午夜网站国内精品久久久久久久久 | 大伊人青草狠狠久久| 国内精品久久久久久99| 97久久国产亚洲精品超碰热| 9久久9久久精品| 狠狠精品干练久久久无码中文字幕| 99久久人人爽亚洲精品美女| 国产农村妇女毛片精品久久| 久久天天躁狠狠躁夜夜2020| 亚洲伊人久久成综合人影院 | 偷偷做久久久久网站| 精品国产99久久久久久麻豆| 亚洲AV无码一区东京热久久| 久久亚洲精品成人AV| 久久美女网站免费| 久久久久久极精品久久久| 亚洲精品久久久www| 亚洲精品蜜桃久久久久久| 高清免费久久午夜精品| 久久久久噜噜噜亚洲熟女综合 | 亚洲精品乱码久久久久久蜜桃图片| 久久亚洲春色中文字幕久久久| 久久精品国产秦先生|