vote up 0 vote down
star

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();
      }
    }
  }
flag
What is the value of "publishUrl"? – Eddie Apr 15 at 23:24
This code is all sorts of broken. – Pesto Apr 15 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. – Andreas Apr 15 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. – Pesto Apr 16 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 – rhigdon Apr 17 at 1:11
add comment

2 Answers:

vote up 2 vote down
check

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.

link|flag
So something like: while (!reader.readLine().equalsIgnoreCase("")) { writer.write(reader.readLine() + "\n"); } I just ran my test case and this seems to work. – rhigdon Apr 17 at 1:26
Essentially, yes. The end of data will be signalled with null, though. – Neil Coffey Apr 17 at 2:49
Cool man.. thanks for the link... I'd vote you up if I could! – rhigdon Apr 17 at 17:04
add comment
vote up 1 vote down

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).

link|flag
add comment

Your Answer:

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.