machao657 / javaparser

Automatically exported from code.google.com/p/javaparser
0 stars 0 forks source link

Annoying Issue with JavaParser.parse(InputStream in) #37

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When using the JavaParser.parse() method to get a single CompilationUnit it 
works fine.  The problem arises when I attempt to have three separate 
CompilationUnits all from the same InputStream (that do not share a object 
reference). 

Only the first CompilationUnit has any data, the others are just empty 
CompilationUnits (not null though), it took me awhile to figure out, but 
what I think what is happening is that the parse method is not resetting 
back to the start of the file again at the end of its parsing, this meant 
that subsequent uses or JavaParser.parse() saw that they where already at 
the end of the file and didn't actually parse anything.

I am using a ugly solution where I close and re-construct my 
FileInputStream after every call to JavaParser.parse().  It would be nice 
if the parse method just returns to the start of the file after finishing 
its work, or even if there was a JavaParser.reParse() method or something.

Cheers, Bryce
P.S. Awesome project by the way. 

Original issue reported on code.google.com by bnemhau...@gmail.com on 27 Apr 2010 at 3:21

GoogleCodeExporter commented 9 years ago
@Bryce

After all that, I believe your request is invalid because 
InputStream.reset/mark are firstly optional and not always possible. 

The usual paradigm is that an InputSTream is completely consumed and possibly 
empty and always never reset when use has completed. If you want to start from 
the start, recrete or get the InputStream again. I would suggest that you 
either recreate the FileInputStream or perhaps dump the bytes of the 
InputStream into a BAOS and then create the IS to pass to JP from the BAOS.

I cant think of a single OSS framework or library that "resets" a InputStream 
it is given.. it just doesnt happen. Think of your own code that is given an 
InputStream, do you ever call reset() when you are finished as part of the 
resource cleanup. Of course not, one simply calls close and thats it.

Original comment by miroslav...@gmail.com on 4 Jun 2011 at 4:57