I'm writing an application which has a method that will download a text file from my server. This text file will contain ~1,000 proxy IP's. The download will happen every 10 minutes. I need to find the most efficient way of doing this.
Currently I have a method in a class called Connection which will return the bytes of whatever I want to retrieve. So if I make a connection to the server for the text file using such method, I will get it returned in bytes. My other method will create a very long string from these bytes. After, I split the long string into an array using System.LineSeparator. Here is the code:
public static void fetchProxies(String url) {
Connection c = new Connection();
List<Proxy> tempProxy = new ArrayList<Proxy>();
ByteArrayOutputStream baos =
c.requestBytes(url);
String line = new String(baos.toByteArray());
String[] split = line.split(System.lineSeparator());
//more code to come but the above works fine.
}
This currently works but I know that it isn't the most efficient way. I
My Problem
Instead of turning the bytes into a very long string, what is the most efficient way of turning the bytes into my IP's so I can add each individual IP into an arraylist and then return the arraylist full of IP's?
The most efficient and logical way would be to create a BufferedReader
wrapping an InputStreamReader
wrapping the InputStream
of the URL connection. You would the use readLine()
on the BufferedReader
until it returns null, and append each line read to the list of IP addresses:
List<String> ipList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), theAppropriateEncoding))) {
String line;
while ((line = reader.readLine()) != null) {
ipList.add(line);
}
}
Note that this probably won't change much in the performance of the method, though, because most of the time is being spend in waiting fof bytes coming from the remote host, which is considerably slower than building and splitting a String in memory.