inputstream.available() is 0 always
.available()
can not be used in inter-process communication (serial included), since it only checks if there is data available (in input buffers) in current process.
In serial communication, when you send a messaga and then immediately call available()
you will mostly get 0 as serial port did not yet reply with any data.
The solution is to use blocking read()
in a separate thread (with interrupt()
to end it):
Thread interrupt not ending blocking call on input stream read
Why InputStream.inAvailable() is always returning 0?
I should get back an xml as response but bytes in input stream buffer
Maybe so, but not instantaneously, which is what your code assumes. There are few if any correct uses of available(), and this isn't one of them. Just block in the read.
InputDataStream.available() is always 0 Java Socket Client
Presumably it's because no data has been received yet. available()
tries to return the amount of data available right now without blocking, so if you call available()
straight after making the connection, I'd expect to receive 0 most of the time. If you wait a while, you may well find available()
returns a different value.
However, personally I don't typically use available()
anyway. I create a buffer of some appropriate size for the situation, and just read into that:
byte[] data = new byte[16 * 1024];
int bytesRead = stream.read(data);
That will block until some data is available, but it may well return read than 16K of data. If you want to keep reading until you reach the end of the stream, you need to loop round.
Basically it depends on what you're trying to do, but available()
is rarely useful in my experience.
What does InputStream.available() do in Java?
Blocking doesn't relate to threading or synchronisation here. Instead it relates to blocking IO (see this for more info). If you issue a request to read, and the channel has none available, a blocking call will wait (or block) until data is available (or the channel is closed, throws an exception etc.)
So why use available()
? So you can determine how many bytes to read, or determine whether you're going to block.
Note that Java has non-blocking IO capabilities as well. See here for more details
InputStream.available() doesn't work
Read the javadoc:
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
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.
In short, InputStream.available()
is not half as useful as you think it is.
If you need to detect the end of the stream, read()
from it and it detect if the result is -1
. Do not use available()
.
How can I check if an InputStream is empty without reading from it?
I think you are looking for inputstream.available()
. It does not tell you whether its empty but it can give you an indication as to whether data is there to be read or not.
inputstream available() always return 0 in android studio while uploading azure blob
Please make sure using the correct uri of your photoURi
with the prefix file://
, and try the code below.
long size = 0L;
File file = new File(photoURI);
if(file.exists()) {
FileInputStream fis = new FileInputStream(file);
size = fis.available();
fis.close();
}
Related Topics
Findviewbyid Returns Null in a Dialog
How to Find the Currently Running Applications Programmatically in Android
How to Use Vectordrawable in Buttons and Textviews Using Android:Drawableright
How to Execute Linux Commands on a Remote MAChine Using Java
Differences Between System.Out.Println() and Return in Java
Java Embedded Databases Comparison
Cannot Issue Data Manipulation Statements with Executequery()
Java 8: Lambda-Streams, Filter by Method with Exception
Different Dependencies for Different Build Profiles
Parsing a String to Date Format in Java Defaults Date to 1 and Month to January
Date Object Simpledateformat Not Parsing Timestamp String Correctly in Java (Android) Environment
Firebaseapp with Name [Default] Doesn't Exist
How to Get the Current Screen Orientation
Can't Make Jdbc Connection to MySQL (Using Java, Intellij, and Linux)
Post to Jersey Rest Service Getting Error 415 Unsupported Media Type
How to Convert a JSON String to a Map<String, String> with Jackson JSON