Streamreader buffer causes basestream. position to not reflect the current read location

Source: Internet
Author: User
Today, I want to write a small tool to extract some text from a file, Code As follows:
Public   Static   Void Main ( String [] ARGs)

{

Streamreader SR =   New Streamreader (ARGs [ 0 ], Encoding. Default, False , 1 );

Sr. basestream. Seek ( Zero X 00005298 , Seekorigin. Begin );


String Line =   Null ;


Do

{

Line=Sr. Readline ();

Console. writeline (line );

}

While (Sr. basestream. Position <   0x0000b0da );



Sr. Close ();

}

However, when reading 0x0000ae98, it will end. Modify the Code as follows to output Sr. basestream. position;

Public   Static   Void Main ( String [] ARGs)

{

Streamreader SR =   New Streamreader (ARGs [ 0 ], Encoding. Default );

Sr. basestream. Seek ( Zero X 00005298 , Seekorigin. Begin );


String Line =   Null ;


Do

{

Line=Sr. Readline ();

Console. writeline (line+Sr. basestream. position );

}

While (Sr. basestream. Position <   0x0000b0da );



Sr. Close ();

}


It is found that the position is increased by 2048 as a stride, so it is clear that it is caused by the buffer adopted by streamreader. By searching in the. NET Framework SDK, we can use constructors to specify the buffer size of streamreader, but the minimum value is 128, that is, the buffer of streamreader cannot be disabled.

There are two possible solutions: first, use filestream. Read to read the data in the entire range to a buffer. Then, you can

1) Build a memorystream based on this buffer and then read it using streamreader.

Or 2) Call encoding. Default. getstring () to obtain the string and construct a stringreader to read the string.

The code for the two methods is as follows:

1) Use memorystream and streamreader

Public   Static   Void Main ( String [] ARGs)

{

Filestream FS =   New Filestream (ARGs [ 0 ], Filemode. Open );


FS. Seek ( Zero X 00005298 , Seekorigin. Begin );

Byte [] Buffer =   New   Byte [ 0x0000b0da   -   Zero X 00005298 ];

FS. Read (buffer, 0 , Buffer. Length );


FS. Close ();


Streamreader SR =   New Streamreader ( New Memorystream (buffer), encoding. Default );


String Line = Sr. Readline ();


While (Line ! =   Null )

{

Console. writeline (line );

Line=Sr. Readline ();

}


Sr. Close ();

}


2) encoding. getstring and stringreader

Public   Static   Void Main ( String [] ARGs)

{

Filestream FS =   New Filestream (ARGs [ 0 ], Filemode. Open );


FS. Seek ( Zero X 00005298 , Seekorigin. Begin );

Byte [] Buffer =   New   Byte [ 0x0000b0da   -   Zero X 00005298 ];

FS. Read (buffer, 0 , Buffer. Length );


FS. Close ();


Stringreader SR =   New Stringreader (encoding. Default. getstring (buffer ));


String Line = Sr. Readline ();


While (Line ! =   Null )

{

Console. writeline (line );

Line=Sr. Readline ();

}


Sr. Close ();

}


After I tested the two methods, I came to the conclusion that stringreader is a little faster and very small. If the data volume is large, different results may be returned.

Internally, stringreader uses the char traversal in the string to find the travel separator, and then calls substring to obtain the substring, streamreader creates a character array buffer (char []) for the buffer size, and then calls decoder each time. getchars obtains the string array from byte [], traverses the char array to find the separator, creates a stringbuilder, and calls its append (char [],...) method returns a substring.

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.