Results 1 to 10 of 10
  1. #1
    sumarlidason is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Question BufferedReader.readLine() slow reading long line.

    Good Afternoon,

    I am interfacing with a web site using their XML API to retrieve data. Unfortunately - everything is coming down the pipe on a single line, and the code I'm using is text book - I think.
    Java Code:
    BufferedReader d = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    String output = d.readLine();
    In some instances readLine() takes 22 seconds to execute, there must be a better way.

    Any ideas would be appreciated! :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: BufferedReader.readLine() slow reading long line.

    everything is coming down the pipe on a single line
    Is 22 seconds a reasonable time for the number of bytes being read?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sumarlidason is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: BufferedReader.readLine() slow reading long line.

    Yes, that is a good point. :)

    I should have included that my goal was to show progress to the end user instead of one of those never ending progress bars.

    Also,
    urlConn.getContentLength() returned unknown length, so If i can't get the content size all of this is moot anyway. Is there another way to retrieve the size of the content?
    Last edited by sumarlidason; 06-21-2012 at 06:35 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: BufferedReader.readLine() slow reading long line.

    Have the sender send the length.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    sumarlidason is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: BufferedReader.readLine() slow reading long line.

    Lets say I could make that happen; I'd like a way to read ?2048? bytes from the stream at a time, then I could provide a pretty accurate progress bar for the end user.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: BufferedReader.readLine() slow reading long line.

    I'd like a way to read ?2048? bytes
    Look at the read() methods.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    sumarlidason is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: BufferedReader.readLine() slow reading long line.

    Quote Originally Posted by Norm View Post
    Look at the read() methods.
    At first glance I thought that was my answer, but the implementation is a little different, the parameter off,
    off - Offset at which to start storing characters
    looks to be used as an offset into the char[] you supply, not an offset into the BufferedReader. The other read() only reads a single byte, and this doesn't seem like a good idea for several MB of data.

    Thank you for helping me.

  8. #8
    sumarlidason is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: BufferedReader.readLine() slow reading long line.

    Or - the buffered reader handles that internally like it does with read(). I'll give that a shot.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: BufferedReader.readLine() slow reading long line.

    Also look at the Reader class that BufferedReader extends.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    sumarlidason is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: BufferedReader.readLine() slow reading long line.

    It works, but not as I expected. BufferedReader::read() stops way before 2048. According to the docs it stops for only these reasons,
    The specified number of characters have been read,
    The read method of the underlying stream returns -1, indicating end-of-file, or
    The ready method of the underlying stream returns false, indicating that further input requests would block.
    1) condition not satisfied
    2) condition not satisfied
    3) doubtful with my 127 byte test message

    Alas, I think I can work with it, thanks!

Similar Threads

  1. Replies: 1
    Last Post: 02-27-2012, 01:37 PM
  2. Manifest file: line too long error
    By nn12 in forum New To Java
    Replies: 10
    Last Post: 02-01-2011, 03:01 PM
  3. Replies: 1
    Last Post: 10-08-2009, 04:45 PM
  4. Reading text from a URL using BufferedReader
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:17 AM
  5. BufferedReader: readLine method problems
    By bbq in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2007, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •