0

I have two static blocks in my class that load data from two URLs. I want to use a single static block, and read the data efficiently. Any thoughts on how this can be done?

static {    

    URL urlA = null;
    String data = "";       
    try {
        url = new URL(urlA);            
        BufferedReader in = new BufferedReader(new InputStreamReader(urlA.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            data = data + inputLine;
        }
    } catch (MalformedURLException mue) {
        e.printStackTrace();
    } catch (IOException ioe) {
        e.printStackTrace();
    }
    //Do stuff with the data
}

static {    

    URL urlB = null;
    String data = "";       
    try {
        url = new URL(urlB);            
        BufferedReader in = new BufferedReader(new InputStreamReader(urlB.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            data = data + inputLine;
        }
    } catch (MalformedURLException mue) {
        e.printStackTrace();
    } catch (IOException ioe) {
        e.printStackTrace();
    }
    //Do stuff with the data
}
5
  • 1
    Uhm, it is not really recommended to use static blocks for that... Commented Jul 25, 2013 at 6:41
  • 2
    Go ahead, you have our permission. You could simple write a single, static, utility method and pass in the different URLs Commented Jul 25, 2013 at 6:41
  • What kind of improvements do you want? Commented Jul 25, 2013 at 6:42
  • I need to use static blocks in this case since I want the data to be loaded as soon as the class is loaded. Ha MadProgrammer :) Commented Jul 25, 2013 at 6:42
  • justhalf - Just want to combine everything in one static block, which would mean creating multiple String variables, starting two different while loops. I am thinking if this can be done more efficiently? Commented Jul 25, 2013 at 6:43

3 Answers 3

1

Improvements:

  1. Do not use a static initializer block for this. These are executed when the class is loaded. You can't know when that will happen, and if something goes wrong the class fails to load, and the class loading error confuses the original error. Use a static utility method instead and call it "lazily" when the data is needed.
  2. Use StringBuilder instead of String to concatenate the output. String being immutable means that every time you append a line the program must copy what has been read so far into a new string, causing a major performance hit:

    StringBuilder input = new StringBuilder();
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        input.append(inputLine);
    }
    data = input.toString();
    
  3. Handle the exceptions properly. What happens if the program is not connected to a network? You will get an IOException, the program will print a stack trace somewhere, and then what? No data has been loaded from anywhere so there's nothing to process.

  4. The character encoding. The program assumes the remote system uses the character encoding that happens to be the default encoding on the local system. If you're in complete control of where the program will be run this doesn't have to be a problem, but it's safer not to make such assumptions and use some fixed well known encoding instead, like UTF-8. Set the encoding in the InputStreamReader constructor.
Sign up to request clarification or add additional context in comments.

4 Comments

I have a logger. I will be using it. This is just a dummy implementation I typed up here to get y'all's input.
Using a logger is besides the point, what's important is if it's meaningful to continue the execution after loading the data has failed. Since you're loading the data you must need it for something, right?
Yup, but thats besides the point. Not relevant to the question I have asked here ;)
If you want answers relevant to your problem you should ask a question that's relevant to your problem. Asking "How to improve this code that has nothing to do with what I'm actually doing" doesn't seem very productive.
0

You can have something like this:

static {    

        String dataFromUrl1 = loadFromUrl("http://myurl1");
        String dataFromUrl2 = loadFromUrl("http://myurl2");
    }


private static String loadFromUrl(String urlStr){
    String data = "";       
    try {
        URL url = new URL(urlStr);            
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            data = data + inputLine;
        }
    } catch (MalformedURLException mue) {
        mue.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return data;

}

Here you have a static function which gets data from a given url and returns it. you can call this static function as many times you want from the static block.

5 Comments

Yes if the return type is a string. What is url1, and url2 both return objects of different types? In my case, they actually do.
what are the expected return types then?
Objects custom to my application.
Be specific in your questions. Couldn't you mention in your question that you want get different objects from different urls? My suggestion is to write static function which converts a url data to the desired object, and access the same from static block. But this kind of data loading in the static block is not considered as a good practice.
Nope. I didn't have to. Even if I did, your implementation is very generic.
0

You can do this:

public static String readUrl(String urlString) {    

    String data = "";       
    try {
        url = new URL(urlString);            
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            data = data + inputLine;
        }
    } catch (MalformedURLException mue) {
        e.printStackTrace();
    } catch (IOException ioe) {
        e.printStackTrace();
    }
    return data;
}

public static String data1;
public static String data2;

static {    

data1 = readUrl("http://google.com");    
data2 = readUrl("http://oracle.com");

}

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.