|
001 packagepackage is used to name the directory or folder a class is in scg.ch14.io;
002
003 importimport means to make the classes and/or packages available in this program java.io.FilterReader;
004 importimport means to make the classes and/or packages available in this program java.io.IOException;
005 importimport means to make the classes and/or packages available in this program java.io.Reader;
006
007 /**
008 * This classclass is a group of fields and methods used for making objects wraps a {open braces start code blocks and must be matched with a close brace@link Reader}close braces end code blocks and must match an earlier open brace and reads it whilewhile is a looping structure for executing code repeatedly eliminating
009 * end-of-line comments. End-of-line comments are defined to be the same
010 * as Java end-of-line comments, beginning with a doubledouble is the type for numbers that can contain decimal fractions slash and
011 * extending to the end of the current line. The whole end-of-line
012 * comment is treated as consisting of the end-of-line character (\n or
013 * \r) found after the end of the comment.<br />
014 * <br />
015 * This classclass is a group of fields and methods used for making objects is modeled after the SourceReader classclass is a group of fields and methods used for making objects presented in
016 * Harold, _Java I/O, 2E_, published by O'Reilly. The Unicode reading
017 * methods were modified forfor is a looping structure for repeatedly executing a block of code the task at hand. Also, the variable c was
018 * universally replaced with ch (thisthis means the current object (the implicit parameter) author (blad) is so pedantic).
019 */
020 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects SansCommentFilterReader
021 extendsextends means to customize or extend the functionality of a class FilterReader {open braces start code blocks and must be matched with a close brace
022 /** /** state flag; have we already seen the end of the stream? */
023 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false endOfStream =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
024
025 /** the extra, read-ahead character (-1 ifif executes the next statement only if the condition in parenthesis evaluates to true not useful) */
026 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer readAheadCh;
027
028 /**
029 * Construct a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link SansCommentFilterReader}close braces end code blocks and must match an earlier open brace wrapped around the
030 * given {open braces start code blocks and must be matched with a close brace@link Reader}close braces end code blocks and must match an earlier open brace.
031 *
032 * @paramnull in the {open braces start code blocks and must be matched with a close brace@link Reader}close braces end code blocks and must match an earlier open brace where thisthis means the current object (the implicit parameter) reader gets characters.
033 */
034 publicpublic is used to indicate unrestricted access (any other class can have access) SansCommentFilterReader(Reader in) {open braces start code blocks and must be matched with a close brace
035 super(in);
036 thisthis means the current object (the implicit parameter).endOfStream =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
037 thisthis means the current object (the implicit parameter).readAheadCh =this assignment operator makes the left side equal to the right side -1;
038 }close braces end code blocks and must match an earlier open brace
039
040 /**
041 * Read one character whilewhile is a looping structure for executing code repeatedly skipping end-of-line comments. An
042 * end-of-line comment is offset with a pair of slashes, Java-style:
043 * // this is comment
044 *
045 * @returnnull returns the character read
046 */
047 @Override
048 publicpublic is used to indicate unrestricted access (any other class can have access) intint is the type for whole numbers and it is short for integer read() throwsthrows means that the method does not handle the given types of exceptions that may occur IOException {open braces start code blocks and must be matched with a close brace
049 intint is the type for whole numbers and it is short for integer ch;// the character to return
050
051 // was there already a character read ahead when dealing with a
052 // slash? If so, return that character
053 ifif executes the next statement only if the condition in parenthesis evaluates to true (readAheadCh !=this is the not equals operator which evaluates to true if both sides are different -1) {open braces start code blocks and must be matched with a close brace
054 ch =this assignment operator makes the left side equal to the right side readAheadCh;
055 readAheadCh =this assignment operator makes the left side equal to the right side -1;
056 returnreturn means to provide the result of the method and/or cease execution of the method immediately ch;
057 }close braces end code blocks and must match an earlier open brace
058
059 ch =this assignment operator makes the left side equal to the right side in.read();
060
061 // it can't start a comment so return it
062 ifif executes the next statement only if the condition in parenthesis evaluates to true (ch !=this is the not equals operator which evaluates to true if both sides are different '/') {open braces start code blocks and must be matched with a close brace
063 returnreturn means to provide the result of the method and/or cease execution of the method immediately ch;
064 }close braces end code blocks and must match an earlier open brace
065
066 // is it the start of a comment?
067 readAheadCh =this assignment operator makes the left side equal to the right side in.read();
068
069 // not start of comment; keep readAheadCh and return ch
070 ifif executes the next statement only if the condition in parenthesis evaluates to true (readAheadCh !=this is the not equals operator which evaluates to true if both sides are different '/') {open braces start code blocks and must be matched with a close brace
071 returnreturn means to provide the result of the method and/or cease execution of the method immediately ch;
072 }close braces end code blocks and must match an earlier open brace
073
074 readAheadCh =this assignment operator makes the left side equal to the right side -1;// readAheadCh part of comment
075 whilewhile is a looping structure for executing code repeatedly ((ch !=this is the not equals operator which evaluates to true if both sides are different '\n') &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) (ch !=this is the not equals operator which evaluates to true if both sides are different '\r')) {open braces start code blocks and must be matched with a close brace// read to end-of-line
076 ch =this assignment operator makes the left side equal to the right side in.read();
077 }close braces end code blocks and must match an earlier open brace
078
079 returnreturn means to provide the result of the method and/or cease execution of the method immediately ch;// return end-of-line character that terminated loop
080 }close braces end code blocks and must match an earlier open brace
081
082 /**
083 * Read length characters into text starting at offset. Written to use
084 * {open braces start code blocks and must be matched with a close brace@link #read()}close braces end code blocks and must match an earlier open brace so only that method need know anything about
085 * comments. Do Not Repeat Yourself.
086 *
087 * @paramnull text character buffer where characters are to be placed
088 * @paramnull offset the offset into text where characters are to be
089 * saved
090 * @paramnull length number of characters desired
091 *
092 * @returnnull number of characters read or -1 ifif executes the next statement only if the condition in parenthesis evaluates to true read is past end of
093 * input stream
094 */
095 @Override
096 publicpublic is used to indicate unrestricted access (any other class can have access) intint is the type for whole numbers and it is short for integer read(charchar is the type for a single letter or symbol and it is short for character[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays text, intint is the type for whole numbers and it is short for integer offset, intint is the type for whole numbers and it is short for integer length)
097 throwsthrows means that the method does not handle the given types of exceptions that may occur IOException {open braces start code blocks and must be matched with a close brace
098 ifif executes the next statement only if the condition in parenthesis evaluates to true (endOfStream) {open braces start code blocks and must be matched with a close brace
099 returnreturn means to provide the result of the method and/or cease execution of the method immediately -1;// end already reached
100 }close braces end code blocks and must match an earlier open brace
101
102 intint is the type for whole numbers and it is short for integer charCount =this assignment operator makes the left side equal to the right side 0;
103 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side offset; i < (offset +adds two numbers together or concatenates Strings together length); i++this is the increment operator, which increases the variable by 1) {open braces start code blocks and must be matched with a close brace
104 intint is the type for whole numbers and it is short for integer temp =this assignment operator makes the left side equal to the right side thisthis means the current object (the implicit parameter).read();
105 ifif executes the next statement only if the condition in parenthesis evaluates to true (temp ==this is the comparison operator which evaluates to true if both sides are the same -1) {open braces start code blocks and must be matched with a close brace
106 endOfStream =this assignment operator makes the left side equal to the right side truetrue is the boolean value that is the opposite of false;
107 breakbreak terminates the loop immediately;
108 }close braces end code blocks and must match an earlier open brace
109 text[brackets are typically used to declare, initialize and index (indicate which element of) arraysi]brackets are typically used to declare, initialize and index (indicate which element of) arrays =this assignment operator makes the left side equal to the right side (charchar is the type for a single letter or symbol and it is short for character) temp;
110 charCount++this is the increment operator, which increases the variable by 1;
111 }close braces end code blocks and must match an earlier open brace
112 returnreturn means to provide the result of the method and/or cease execution of the method immediately charCount;
113 }close braces end code blocks and must match an earlier open brace
114
115 /**
116 * Skip over the given number of characters. Characters are counted
117 * after removal of comments
118 *
119 * @paramnull n number of characters to skip over
120 *
121 * @returnnull the number of characters actually skipped over; -1 ifif executes the next statement only if the condition in parenthesis evaluates to true skip
122 * is past end of input stream
123 */
124 @Override
125 publicpublic is used to indicate unrestricted access (any other class can have access) longlong is the type for whole numbers (they have a larger range than int) skip(longlong is the type for whole numbers (they have a larger range than int) n) throwsthrows means that the method does not handle the given types of exceptions that may occur IOException {open braces start code blocks and must be matched with a close brace
126 charchar is the type for a single letter or symbol and it is short for character[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays chArray =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor charchar is the type for a single letter or symbol and it is short for character[brackets are typically used to declare, initialize and index (indicate which element of) arrays(intint is the type for whole numbers and it is short for integer) n]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
127 intint is the type for whole numbers and it is short for integer charCountSkipped =this assignment operator makes the left side equal to the right side thisthis means the current object (the implicit parameter).read(chArray);
128 returnreturn means to provide the result of the method and/or cease execution of the method immediately charCountSkipped;
129 }close braces end code blocks and must match an earlier open brace
130 }close braces end code blocks and must match an earlier open brace
131
132 //Uploaded on Mon Mar 29 21:40:05 EDT 2010
|