Bufferedreader source code analysis-Readline Method

Source: Internet
Author: User

The Readline method is a very common method in bufferedreader.

With this function, we can read data from a row in an input stream.

To distinguish rows, use "\ r", "\ n", or "\ r \ n". below is the source code of Readline in bufferedreader.

Detailed analysis is written in comments.

String Readline (Boolean ignorelf) throws ioexception {stringbuffer S = NULL; int startchar; synchronized (LOCK) {// ensure that the underlying inputstream is not disabled ensureopen (); // ignorelf is always false in bufferedreader. // skiplf is set to true after reading the "\ r" character, in this way, the "\ n" Boolean omitlf = ignorelf | skiplf; // the next two loops are ignored. The first bufferloop is mainly filled with characters from the underlying array, the underlying array is equivalent to a buffer zone. The buffer size can be specified by the second parameter of the constructor bufferedreader (Reader in, int // sz). The default value is 8192 bytes. // Charloop is used to traverse the underlying array. Each time you read the data in the underlying array, the data is written to a stringbuffer, // when "\ r" or "\ n" is read, the result is returned after the last batch of data is written and the entire loop bufferloop: For (;) is exited (;;) {// nextchar indicates the position of the next character to be read, nchars = number of characters read from inputstream + nextcharif (nextchar> = nchars) Fill (); // read the end of inputstream if (nextchar> = nchars) {/* EOF */If (s! = NULL & S. length ()> 0) return S. tostring (); elsereturn NULL;} Boolean EOL = false; char C = 0; int I;/* skip a leftover '\ n ', if necessary */If (omitlf & (cb [nextchar] = '\ n') nextchar ++; skiplf = false; omitlf = false; // exit this loop in two cases: 1. Read "\ r" or "\ n" 2. The underlying array CB is read charloop: for (I = nextchar; I <nchars; I ++) {c = CB [I]; If (C = '\ n') | (C =' \ R ')) {EOL = true; break charloop ;}// read the starting position of the underlying array startchar = Nextchar; // The position currently read. nextchar = I; // whether the row (end of line) if (EoL) {string STR; If (S = NULL) {// simply use s here. Why do you want to create a STR variable STR = new string (CB, startchar, I-startchar);} else {S. append (CB, startchar, I-startchar); STR = S. tostring () ;}// the position to be read next time nextchar ++; // if you read "\ n" next time, it will be ignored, instead of terminating read if (C = '\ R') {skiplf = true;} return STR;} // write read data if (S = NULL) S = new stringbuffer (defaultexpectedlinelength); S. append (CB, startchar, I-startchar );}}}


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.