Read out InputStream in intervals

Hi,

I want to read out a GPS receiver in defined intervals and get only the recent data. So I tried to hold the connection all the time and open an InputStream from time to time, get the data and close the InputStream again. But I always get an Exception when I try to open an InputStream for the second time. Then I tried to hold also the InputStream all the time and skip the data I don't need, but I always get the old data.

Has anybody an idea to solve this problem without opening and closing connections permanently?

[543 byte] By [j2willa] at [2007-11-26 17:04:12]
# 1

> I want to read out a GPS receiver in defined

> intervals and get only the recent data. So I tried to

> hold the connection all the time and open an

> InputStream from time to time, get the data and close

> the InputStream again.

No, that will close the underlying socket.

> Then I tried to hold also the InputStream all

> the time and skip the data I don't need, but I always

> get the old data.

You must have a programming error.

ejpa at 2007-7-8 23:31:59 > top of Java-index,Core,Core APIs...
# 2

Thanks for your quick answer!

I can only skip the next dataset and not more, because after the first skipping, available() returns always 0.

In the meantime I solved the problem with a timertask, which always opens and closes the connection. But I don't think that this way is a really good solution.

j2willa at 2007-7-8 23:31:59 > top of Java-index,Core,Core APIs...
# 3
available() is an even worse solution ... good for nothing as far as I know. Well I did see a valid use for it once but I forget what it was ...
ejpa at 2007-7-8 23:31:59 > top of Java-index,Core,Core APIs...
# 4
How would you skip all old bytes of the stream, when available() dont' works?
j2willa at 2007-7-8 23:31:59 > top of Java-index,Core,Core APIs...
# 5
available() does work. It just doesn't work the way anybody ever expects it too - despite the Javadoc describing its behaviour precisely. As ejp said, given what it actually does (rather than what people expect it to do) it rarely has any practical use.
dannyyatesa at 2007-7-8 23:31:59 > top of Java-index,Core,Core APIs...
# 6
Ok, available() works, but it's the wrong way to solve my problem. So what can I do to get the last dataset instead of permanently open a new inputstream?
j2willa at 2007-7-8 23:31:59 > top of Java-index,Core,Core APIs...
# 7

Your problem statement is very unclear. For example, what type of connection are you talking about? ejp assumed it was sockets, but I would guess it's a serial port?

Either way, if you close an InputStream, you will close the underlying connection.

I don't understand why you don't just open your connection, wrap it in an appropriate type of InputStream or Reader, and just leave it open and read it whenever you want to. That way, you'll only ever get new data that you haven't already read.

dannyyatesa at 2007-7-8 23:31:59 > top of Java-index,Core,Core APIs...
# 8

I'm talking of a serial port bluetooth-connection and I want to get gps data over this connection on a mobile phone. But two positions per minute are absolutely enough, I don't need more detailed data. That's why I want to skip the data between the reading periods. Considering the limited resources on a mobile I thought it would make sense not to read in permanently. And I didn't find a possibility to get the recent data after such a waiting cycle. I hope the problem is now clearer.

j2willa at 2007-7-8 23:31:59 > top of Java-index,Core,Core APIs...
# 9

I doubt there's going to be any way other than reading and discarding the updates you don't need.

Or, disconnect completely, and just re-connect to get a single sample when you need it. This might be resource heavy - or it might not. You'd need to measure it. It especially might make sense in a Bluetooth environment as a way of conserving battery power.

dannyyatesa at 2007-7-8 23:31:59 > top of Java-index,Core,Core APIs...
# 10
Ok, I supposed already that there exists no other way than the both possibilities you gave. I guess the second is the better one. Thanks!
j2willa at 2007-7-8 23:31:59 > top of Java-index,Core,Core APIs...