ruben
ruben

Reputation: 1750

InputStream.available() is returning 0 or No file size

Please consider the following j2me code segment:

1. FileConnection fc = (FileConnection) Connector.open("file:///root1/photos/2.png");
2. InputStream is = fc.openInputStream();
3. System.out.println(is.available());
4. byte[] fileBytes = new byte[is.available()];
5. int sizef = is.read(fileBytes);
6. System.out.println("filesize:"+sizef);

In this case line 3 & 6 both outputs 0 as file size. But when I put is.read(anyByteArray) this line after line 2 it shows the proper file size. Why is this happening? I think I don't understand these class very well. Any pointer for better understanding?

Thanks for your help.

Upvotes: 1

Views: 914

Answers (1)

johusman
johusman

Reputation: 3472

Don't know about j2me, but the Java6 javadoc for InputStream.available() says this:

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

Upvotes: 1

Related Questions