Styler2go
Styler2go

Reputation: 603

Timeout an InputStream

Is there a way to set a Timeout to an InputStream?

My current code looks like that:

public class DownloadFile implements Runnable {
private EventHandler eh;
private String source;
private String destination;

public void run(){
    try {
        Log.d("Download", "Download... " + source);
        URL url = new URL(source);

        URLConnection connection = url.openConnection();
        connection.connect();

        int fileLength = connection.getContentLength();

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(destination + "t");

Upvotes: 0

Views: 2459

Answers (2)

Sumit Singh
Sumit Singh

Reputation: 15906

I think For you requirement you can use

HttpURLConnection.setReadTimeout()

Mab be these links will helps you.

  1. Is it possible to read from a Java InputStream with a timeout?
  2. Class HttpURLConnection
  3. Can I set a timeout for a InputStream's read() function?

Upvotes: 3

Sebastian Breit
Sebastian Breit

Reputation: 6159

You could start an AsyncTask or just a separate thread and sleep it for the timeout time and then check if the transaction is ready. Or even better, you could postDelayed a message to a Handler, and then check if your task is ready. You could use a timer too, but I think the Handler solution is the most elegant.

Take a look at this: http://developer.android.com/reference/android/os/Handler.html

Upvotes: 0

Related Questions