Code :
import java.io.*;
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
String phrase = "Hello World #This_Is_Comment# #This is not comment#";
byte[] bytes = phrase.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
BufferedInputStream bin = new BufferedInputStream(in);
int character;
boolean com = false;
while ((character = bin.read()) != -1) {
switch(character) {
case '#' :
if (com) com = false;
else {
com = true;
bin.mark(1000);
}
break;
case ' ' :
if (com) {
com = false;
System.out.print('#');
bin.reset();
}
else {
System.out.print((char)character);
}
break;
default : if (!com) System.out.print((char)character);
}
}
in.close(); bin.close();
}
}
What does this code do?
This code reads the String and the bufferStream removes/hides comments. Here comments are denoted by #this_is_comment# and comments with ' ' (space) is not considered comment ex : #This is not comment#.
Question:
Whenever ' ' (space) is encountered and com (boolean value, when true does not read the stream) is true it reverts the stream to the marked position, my doubt is that if it reverts, wouldn't the # be encountered again and com will be set to false hence considering it comment.
case '#' :
if (com) com = false;
else {
com = true;
bin.mark(1000);
}
but this is not the case the output is correct.
If possible please edit the question to make it more understandable.
The behavior is expected. If you read the implementation of the methods .mark()
and .read()
of BufferedInputStream
, you can see that:
The method .mark()
sets markPos
to pos
:
public synchronized void mark(int readlimit) {
marklimit = readlimit;
markpos = pos;
}
The question is who is pos
? Just go to its definition and will find this in JavaDoc (only reporting the relevant part):
/**
* The current position in the buffer. This is the index of the next
* character to be read from the <code>buf</code> array.
* <p>
* ... more JavaDoc
*/
protected int pos;
Hence, when you call .reset()
you're doing this:
public synchronized void reset() throws IOException {
getBufIfOpen(); // Cause exception if closed
if (markpos < 0)
throw new IOException("Resetting to invalid mark");
pos = markpos;
}
Basically, you're restoring the last "next character" of your stream.
According to the official JavaDoc of BufferedInputStream
, if you are in a String s = "Hello World"
and you call .mark()
while reading character W
, when you do .reset()
you will restart from the next character after W
which is o
.
This is why your code doesn't fall in the comment part again.