0

The application I am working on has a terribly slow loading web page. If I run the following code it works fine but only because of the call to sleep. If I don't sleep then the InputStream is just a bunch of spaces, probably due to the application it is calling from. Is there any non-hack way around this?

public class PublishTool extends Thread {

  private URL publishUrl;

  private String filerLocation;

  public PublishTool() {
  }

  public PublishTool(String publishUrl, String filerLocation) throws NibException {

    try {
      this.publishUrl = new URL(publishUrl);
    } catch (MalformedURLException e) {
      throw new NibException("Publish Url :" + publishUrl + " is not valid. ");
    }

    this.filerLocation = filerLocation;

  }

  public void run() {

    File filerFile = new File(filerLocation);
    BufferedWriter writer = null;


    try {
      URLConnection conn = publishUrl.openConnection();
      BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(conn.getInputStream())));

      writer = new BufferedWriter(new FileWriter(filerLocation));

      Thread.sleep(1000l);

      while (reader.ready()) {
        writer.write(reader.readLine() + "\n");
      }

    } catch (MalformedURLException e) {
      throw new IllegalStateException("Malformed URL for : " + publishUrl + " " + filerLocation, e);
    } catch (IOException e) {
      throw new IllegalStateException("IO Exception for  : " + publishUrl + " " + filerLocation, e);
    } catch (InterruptedException e) {
      throw new IllegalStateException("Thread was interrupted early... publishing might have failed.");
    } catch (NibException e) {
      throw new IllegalStateException("Publishing File Copy failed : " + filerLocation + ".bak" + " to " + filerLocation);
    } finally {
      try {
        writer.flush();
        writer.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
5
  • What is the value of "publishUrl"? Commented Apr 15, 2009 at 23:24
  • This code is all sorts of broken. Commented Apr 15, 2009 at 23:36
  • errors i've spotted: .close not in finally block(s); readline for binary data will break things, conn not closed at all, buffered reader/writer normally only need to be used in one direction. may post answer tomorrorw when i'm sober. Commented Apr 15, 2009 at 23:42
  • @Andreas: Also, url is never used, inputStream can't be set to an InputStreamReader, and reader isn't attached to any input stream. Commented Apr 16, 2009 at 0:01
  • Sorry.. I was doing this in a rush yesterday.. The system I am writing this for is all kinds of busted so I was trying to get rid of some off the confusing cruff. Will try to clean it up a bit Commented Apr 17, 2009 at 1:11

2 Answers 2

4

Don't use reader.ready(). Just call readLine() and let readLine() block until the data's ready. The end of the data will generally be signalled with a null line.

In case its helpful, a couple of code examples on my web site: reading from a URL.

Sign up to request clarification or add additional context in comments.

3 Comments

So something like: while (!reader.readLine().equalsIgnoreCase("")) { writer.write(reader.readLine() + "\n"); } I just ran my test case and this seems to work.
Essentially, yes. The end of data will be signalled with null, though.
Cool man.. thanks for the link... I'd vote you up if I could!
1

Firstly, it would be helpful if you posted your actual code.

My guess the problem is calling Reader.ready. Similar to InputStream.available, that returns true if there is already buffered input. If it needs to wait for, say, a socket the it will return false. Generally you don't need ready. Use readLine, and break out of the loop if it returns null (for end of stream).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.