0

What I need to do is basically reading a file which is continously growing, but in every interval I just want to read the new that was appended to the file.

I found a lot of ways to do this with a file on my filesystem, but all these require that I download the file before I can do the magic using the RandomAccessFile-Object.

Is there a way to do this with an online-file like http://myurl.com/123.txt? So that I just stream the data added in the last 2 minutes?

Thanks a lot! noise

tommueller
  • 2,358
  • 2
  • 32
  • 44

1 Answers1

1

If the server supports it, you can use the Range header to request bytes from a given offset. Do this repeatedly to poll the file for new bytes.

Pseudocode:

long offset = 0;
while (true) {
    request.setHeader("Range: bytes=" + offset + "-");
    request.send();
    request.readResponse();
    offset += theNumberOfBytesRead;
    Thread.sleep(someRespectfulAmountOfTime);
}

See this question for details about how the server is likely to respond if it does or doesn't have more bytes available.

To get "data added in the last 2 minutes", you have to keep track of what offset was two minutes ago.

Community
  • 1
  • 1
mpartel
  • 4,474
  • 1
  • 24
  • 31