An issue worth attention when reading the stream in the appendix provided by the park friends of C #.

Source: Internet
Author: User
Tags ftp site

Review solicitation Stickers: http://www.cnblogs.com/BeginnerClassroom/archive/2010/07/30/1788649.html

Appendix collection Stickers: http://www.cnblogs.com/BeginnerClassroom/archive/2010/08/04/1792175.html

You are welcome to expand a part of the content of this book, which will be attached to the book in the form of an appendix.

Requirements:

    1. Closely around one or two centers;
    2. Clear logic and smooth writing;
    3. Consider the basics of beginners.
    4. It is recommended that the writing time be less than one week.

After I write everything, I put it there first. After a while, I read it and modify it again. It is basically smooth after so many times.

 

(PS: it will be signed, but there is no draft fee, because there is not much, not enough points. Of course, if you make a fortune, I will give it to you .)

Title Author Status
Research on modifying the font size of RichTextBox Li yilai Completed
Difference between delegation and Interface Tang fan Writing
XML format comment Capricornus Writing
Explicit interface implementation and comparison with abstract classes Gu lei Writing
. Net version change table Zhang Zhiming Writing
Character encoding Zhao Shijing Writing
Notes when reading a stream Huang zhibin Writing
Application of Regular Expressions in emeditor Liu yongfa Writing
Drawing Cache   To be selected
Asynchronous read/write operations   To be selected
Control Development and custom control Minghao_hu Writing
   
     

 

Notes when reading a stream

(This article is provided by Huang zhibin)

Stream class is the abstract base class of all streams. Through it and its subclassProgramYou can perform operations such as "read", "write", and "query" without having to know the details of the operating system and basic devices. We hope the example in this article can help you understand the usage of the stream.

 
Next we will study the reading members of the stream class and its derived classes.

Stream. Read ()

Stream. readbytes ()

Binaryreader. Read ()

Binaryreader. readbytes ()

Textreader. Read ()

Textreader. readblock ()

 
The stream. Read method is used to read the byte sequence from the stream and increase the current position of the stream to the corresponding byte number. In msdn, there is a saying: "Even if the stream is not reached, the number of bytes obtained by this method can still be less than the number of bytes requested ." Now let's write a program to verify this.
 
Using system; using system. io; using skyiv. util; namespace skyiv. ben. streamtest {sealed class program {static void display (string MSG, int N) {console. writeline ("{128 }:{ 1024: N0}", MSG, n);} static void main () {var BS = new byte [131,072 *]; //
VaR FTP = new ftpclient ("ftp://ftp.hp.com", "anonymous", "ben@skyiv.com"); stream = FTP. getdownloadstream ("pub/softpaq/allfiles.txt"); binaryreader = new binaryreader (Stream); textreader = new streamreader (Stream); int count1 = stream. read (BS, 0, BS. length); int count2 = stream. readbytes (BS. length ). length; int count3 = binaryreader. read (BS, 0, BS. length); int count4 = binaryreader. readbytes (BS. length ). length; int count5 = textreader. read (BUF, 0, Buf. length); int count6 = textreader. readblock (BUF, 0, Buf. length); display ("CT", BS. length); display ("stream. read ", count1); display (" stream. readbytes ", count2); display (" binaryreader. read ", count3); display (" binaryreader. readbytes ", count4); display (" textreader. read ", count5); display (" textreader. readblock ", count6 );}}}

======

 The result of running the program three times is as follows: Keep CT:  131  ,  072  Stream. Read:  4  ,  356  Stream. readbytes:  131  ,  072  Binaryreader. Read:  2  , 904  Binaryreader. readbytes:  131  ,  072  Textreader. Read:  123  ,  812  Textreader. readblock:  131  ,  072  Secondary CT:  131  ,  072 Stream. Read:  4  ,  356  Stream. readbytes:  131  ,  072  Binaryreader. Read:  4  ,  356  Binaryreader. readbytes:  131  ,  072 Textreader. Read:  2  ,  904  Textreader. readblock:  131  ,  072  Secondary CT:  131  ,  072  Stream. Read:  4  ,  356 Stream. readbytes:  131  ,  072  Binaryreader. Read:  2  ,  904  Binaryreader. readbytes:  131  ,  072  Textreader. Read:  5  ,  808 Textreader. readblock:  131  ,  072   

Visible, stream. read (), binaryreader. read () and textreader. when the read () method does not reach the end of the stream, the number of bytes can still be less than the number of bytes requested. This problem is solved in the network stream (such as FTP), device stream (such as serial port input) and so on, and stream. readbytes (), binaryreader. readbytes (), and textreader. the readblock () method does not have this problem.

Now, we can use reflector to view the source program of binaryreader. Read () method.Code.

 

Public Virtual int read (byte [] buffer, int index, int count) {If (buffer = NULL) {Throw new argumentnullexception ("buffer", environment. getresourcestring ("argumentnull_buffer");} If (index <0) {Throw new argumentoutofrangeexception ("Index", environment. getresourcestring ("argumentoutofrange_neednonnegnum");} If (count <0) {Throw new argumentoutofrangeexception ("count", environment. getresourcestring ("argumentoutofrange_neednonnegnum");} If (buffer. length-index) <count) {Throw new argumentexception (environment. getresourcestring ("argument_invalidofflen");} If (this. m_stream = NULL) {_ error. filenotopen ();} return this. m_stream.read (buffer, index, count );}

======

The m_stream type of the last row is stream. It can be seen that binaryreader. the read () method simply calls stream after some necessary checks. read () method, so they have the same problem.

The source code of binaryreader. readbytes is as follows:

 

Public Virtual byte [] readbytes (INT count) {If (count <0) {Throw new argumentoutofrangeexception ("count", environment. getresourcestring ("argumentoutofrange_neednonnegnum");} If (this. m_stream = NULL) {_ error. filenotopen ();} byte [] buffer = new byte [count]; int offset = 0; do {int num2 = This. m_stream.read (buffer, offset, count); If (num2 = 0) {break;} Offset + = num2; count-= num2;} W Hile (count> 0); If (offset! = Buffer. length) {byte [] DST = new byte [offset]; buffer. internalblockcopy (buffer, 0, DST, 0, offset); buffer = DST;} return buffer ;}

======

From the code above, we can see that the binaryreader. readbytes method cyclically calls the stream. Read method until it reaches the end of the stream or has read the request bytes. That is to say, if the Stream does not reach the end, this method will certainly return the requested byte.

The stream. readbytes () method is actually an extension method I wrote. The source code is as follows:

Using system; using system. io; namespace skyiv. util {static class extensionmethods {public static byte [] readbytes (this stream, int count) {If (count <0) throw new argumentoutofrangeexception ("count ", "?????????? "); Var BS = new byte [count]; var offset = 0; For (INT n =-1; n! = 0 & COUNT> 0; count-= N, offset + = N) n = stream. Read (BS, offset, count); If (offset! = BS. Length) array. Resize (ref BS, offset); Return BS ;}}}

======

The ftpclient class used in the test program is compiled by me. For details, refer to my other article "How to directly process compressed files on the FTP server" [①], the source code is as follows:

Using system; using system. io; using system. net; namespace skyiv. util {sealed class ftpclient {URI; string username; string password; Public ftpclient (string Uri, string username, string password) {This. uri = new uri (URI); this. username = username; this. password = password;} public stream getdownloadstream (string sourcefile) {URI downloaduri = new uri (Uri, sourcefile); If (downloaduri. scheme! = Uri. urischemeftp) throw new argumentexception ("URI is not an FTP site"); ftpwebrequest ftprequest = (ftpwebrequest) webrequest. create (downloaduri); ftprequest. credentials = new networkcredential (username, password); ftprequest. method = webrequestmethods. FTP. downloadfile; Return (ftpwebresponse) ftprequest. getresponse ()). getresponsestream ();}}}

======

[1] address: http://www.cnblogs.com/skyivben/archive/2005/09/17/238920.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.