0

Aim : To read a Url which containing information in Json.

Question: I got a code of reading Url Which is given Below. I have a complete Understanding what code is doing but I do not have any idea why the size of char array is 1024 not 2048 or something else . How to decide what character size array is good at the time of reading Url ?

private static String readUrl(String urlString) throws Exception {
    BufferedReader reader = null;
    try {
        URL url = new URL(urlString);
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        StringBuffer buffer = new StringBuffer();
        int read;
        char[] chars = new char[1024]; ???   

        while ((read = reader.read(chars)) != -1)
            buffer.append(chars, 0, read); 

        return buffer.toString();
    } finally {
        if (reader != null)
            reader.close();
    }
}     
Raedwald
  • 46,613
  • 43
  • 151
  • 237
mystertyboy
  • 479
  • 6
  • 13
  • Please format your code correctly ;P – HarryCBurn Dec 09 '14 at 07:24
  • There is no reason this is 1024 but it cant be 2048 usually, read this http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers – Lrrr Dec 09 '14 at 07:26
  • 1
    @AliAmiri This question is not about the length of the URL string. It is about reading the content. The shown code uses the `char[]` in a loop for reading that content. – Seelenvirtuose Dec 09 '14 at 07:56
  • @Seelenvirtuose how much it reads is depend on how long URL is. – Lrrr Dec 09 '14 at 08:02
  • 1
    @AliAmiri No, it doesn't. – Seelenvirtuose Dec 09 '14 at 08:03
  • yes @Seelenvirtuose I am talking about the length of char array. Is there any any document or anything by which I can correct my doubt about it. – mystertyboy Dec 09 '14 at 08:16
  • @AliAmiri provided link is useful thanks for it. by reading that i can create a thumb rule not to use char array more than 1024 because browsers url read is something around <2048 .Is it the only reason of using 1024 or less than 2048 ? if I use 512 so its performance will increase or decrease ? I think decrease now program will run reading url twice. am I Right ? – mystertyboy Dec 09 '14 at 08:22

1 Answers1

0

As the BufferedReader already has an internal buffer of 4096 characters, implementation-dependent, and as the socket already has a considerably larger receive buffer, it really doesn't make much difference what value you choose. The returns on buffering diminish geometrically with size.

user207421
  • 305,947
  • 44
  • 307
  • 483