I am very new to android and I'm just scratching surface of it. When I was going through some Tutorials I came across this class to Download the File. I was able to understand Other code but I am unableto understand what does
private static final int MEGABYTE = 1024 * 1024;
Does? Is this really necessary? what if It is not declared First.
Below is the Code
public class FileDownloader { private static final int MEGABYTE = 1024 * 1024; public static void downloadFile(String fileUrl, File directory){ try { URL url = new URL(fileUrl); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); //urlConnection.setRequestMethod("GET"); //urlConnection.setDoOutput(true); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream(directory); int totalSize = urlConnection.getContentLength(); byte[] buffer = new byte[MEGABYTE]; int bufferLength = 0; while((bufferLength = inputStream.read(buffer))>0 ){ fileOutputStream.write(buffer, 0, bufferLength); } fileOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
It must be very stupid Question but I was really curious to know How and why?
Thanks in advance.
MEGABYTE = 1024 * 1024
mean number of bytes in a MB
known as megabyte
8 bits = 1 byte
1024 bytes = 1 kb
1024 kb = 1 Mb
and 1 Mb contains 1048576
. bytes which is equal to 1024 * 1024
inputStream.read
will try to read this amount bytes from the steam and put it inside the byte array
data_read_as_byte return
> 0 number of bytes read
< 0 -1
// return 0 when passed array size is 0
The private static final
declaration means that that value is declared as a constant . There is nothing special with it, it is just a good coding practice to place the hard-coded values as constants. It is for the sake of code readability and maintainability.
Your code is basically creating a buffer of 1 MB for downloading files.
That means when downloading a file, first 1 MB is downloaded into your RAM, then that part is copied to file output stream (Disk), and this process will repeat until everything is downloaded. Note that if everything was downloaded to RAM, your device may run out of memory and app would throw OutOfMemoryError
. Also note that you can choose any value instead of 1 MB for your buffer.
private static final int MEGABYTE = 1024 * 1024;
Does ?
Declared a variable that is final
, private
and static
What it is necessarily doing is that you can reference MEGABYTE
variable inside any method of FileDownloader
class( because it is private
), be it static
or not
.
If it would have declared inside any method
, then it's scope
would be within that method only.
If it would have been declared without static
keyword, then it would not be accessible in static
method. You can try it yourself by removing the static
keyword.
Is this really necessary? what if It is not declared First.
In the given class, there is only one static method
and this variable is declared as private
, so it doesn't make a lot of sense to declare it in such a way. Declaring this variable inside your method should also be fine
Instantiating MEGABYTES
less than INTEGER.MAX_VALUE
should be fine
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.