Effectively read a file from a URL in byte [] in Java

I am trying to find a more efficient method of reading a file from a remote URL and storing it in an array of bytes. Here is what I have now:

private byte[] fetchRemoteFile(String location) throws Exception {
  URL url = new URL(location);
  InputStream is = null;
  byte[] bytes = null;
  try {
    is = url.openStream ();
    bytes = IOUtils.toByteArray(is);
  } catch (IOException e) {
    //handle errors
  }
  finally {
    if (is != null) is.close();
  }
  return bytes;
}

As you can see, I am currently passing the URL to a method where it uses an InputStream object to read in bytes of the file. This method uses Apache Commons IOUtils. However, this method call tends to take a relatively long time. When you get hundreds, thousands or hundreds of thousands of files one by one, it gets pretty slow. Is there a way to improve this method so that it works more efficiently? I considered multithreading, but I would like to keep this as a last resort.

+4
1

.

:

" "

:

  • ,

  • , ?

, , VM args -XmsYYYYM -XmxYYYYM , , , , . .

+2

All Articles