I uploaded a JAR file that was 21 KB in size on my computer to a website. I found the direct link to that JAR file and I create an InputStream to that file:
URL url = new URL("addresstofile");
InputStream stream = url.openStream();
It would be expected that the amount of bytes available in the stream would be ~21,000. However, the amount available is 7,048. In an attempt to debug, I saved those 7,048 bytes in a byte array and then I write those bytes to a temporary file with the extension “.jar”
I extract the classes in the JAR file (that is 7 KB; the original is 21 KB). Most of the classes are there except I noticed that nested classes are not present. I am not sure as to why that is – is it just a coincidence that the InputStream just cuts out there, or are there some special exceptions to nested classes?
Why is InputStream acting so oddly?
Thanks!
@Neil: This works! Thanks a lot!
In the modified version of the code you present, the problem is the comparison. Don’t cast ‘b’ to an int before making the comparison with -1. If you do that then if a byte in the stream happens to be 255, you won’t be able to differentiate between this byte and the end of the stream.
So make the comparison with -1 on the int returned from read(). Then if it’s not -1, cast the value to a byte and put it in the array (or whatever).