0

I'm trying to load in a url that is a zip file.. Right now I read in the file to get the size. Then create an array of ints with that size, and then read in the file again and store it in my array

  1. Is there a way to get the size of the url so I do not have to read it in twice?
  2. Is there a way to change the size of the array as I read it in so I do not have to get the size ahead of time.

It seems silly to read in the file 3 times

Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • Use, for example, [`java.io.ByteArrayInputStream`](http://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayInputStream.html) instead of using an array directly. – Jesper Oct 13 '15 at 14:36
  • It is very silly to read it 3 times. Use a buffer. http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java – jgitter Oct 13 '15 at 14:41
  • Are you trying to save the file to your computer or would you like to cache it in the code? – mezzodrinker Oct 13 '15 at 14:42
  • you should probably create an array of `byte` instead of `int` – zapl Oct 13 '15 at 14:44

1 Answers1

2

It depends on how your server/service has been implemented. Try reading the "HTTP Header" Content-Length first e.g

URL url = new URL("http(s)://yourfileURL");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();

or

urlConnection.getHeaderField("Content-Length");

See Content-Length

Community
  • 1
  • 1
Optional
  • 4,387
  • 4
  • 27
  • 45