|
1 | | -package ai.elimu.content_provider.util; |
| 1 | +package ai.elimu.content_provider.util |
2 | 2 |
|
3 | | -import android.util.Log; |
| 3 | +import android.util.Log |
| 4 | +import org.apache.commons.io.IOUtils |
| 5 | +import java.io.BufferedReader |
| 6 | +import java.io.IOException |
| 7 | +import java.io.InputStream |
| 8 | +import java.io.InputStreamReader |
| 9 | +import java.net.HttpURLConnection |
| 10 | +import java.net.URL |
4 | 11 |
|
5 | | -import org.apache.commons.io.IOUtils; |
| 12 | +object MultimediaDownloader { |
| 13 | + fun downloadFileBytes(urlValue: String): ByteArray? { |
| 14 | + Log.i(MultimediaDownloader::class.java.name, "downloadFileBytes") |
6 | 15 |
|
7 | | -import java.io.BufferedReader; |
8 | | -import java.io.IOException; |
9 | | -import java.io.InputStream; |
10 | | -import java.io.InputStreamReader; |
11 | | -import java.net.HttpURLConnection; |
12 | | -import java.net.URL; |
| 16 | + Log.i(MultimediaDownloader::class.java.name, "Downloading from $urlValue") |
13 | 17 |
|
14 | | -public class MultimediaDownloader { |
15 | | - |
16 | | - public static byte[] downloadFileBytes(String urlValue) { |
17 | | - Log.i(MultimediaDownloader.class.getName(), "downloadFileBytes"); |
18 | | - |
19 | | - Log.i(MultimediaDownloader.class.getName(), "Downloading from " + urlValue); |
20 | | - |
21 | | - byte[] bytes = null; |
| 18 | + var bytes: ByteArray? = null |
22 | 19 |
|
23 | 20 | try { |
24 | | - URL url = new URL(urlValue); |
25 | | - |
26 | | - HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); |
27 | | - httpURLConnection.setRequestMethod("GET"); |
28 | | - httpURLConnection.connect(); |
29 | | - |
30 | | - int responseCode = httpURLConnection.getResponseCode(); |
31 | | - Log.i(MultimediaDownloader.class.getName(), "responseCode: " + responseCode); |
32 | | - InputStream inputStream = null; |
| 21 | + val url = URL(urlValue) |
| 22 | + |
| 23 | + val httpURLConnection = url.openConnection() as HttpURLConnection |
| 24 | + httpURLConnection.requestMethod = "GET" |
| 25 | + httpURLConnection.connect() |
| 26 | + |
| 27 | + val responseCode = httpURLConnection.responseCode |
| 28 | + Log.i( |
| 29 | + MultimediaDownloader::class.java.name, |
| 30 | + "responseCode: $responseCode" |
| 31 | + ) |
| 32 | + var inputStream: InputStream? = null |
33 | 33 | if (responseCode == 200) { |
34 | | - inputStream = httpURLConnection.getInputStream(); |
35 | | - bytes = IOUtils.toByteArray(inputStream); |
| 34 | + inputStream = httpURLConnection.inputStream |
| 35 | + bytes = IOUtils.toByteArray(inputStream) |
36 | 36 | } else { |
37 | | - inputStream = httpURLConnection.getErrorStream(); |
38 | | - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); |
39 | | - String response = ""; |
40 | | - String line; |
41 | | - while ((line = bufferedReader.readLine()) != null) { |
42 | | - response += line; |
| 37 | + inputStream = httpURLConnection.errorStream |
| 38 | + val bufferedReader = BufferedReader(InputStreamReader(inputStream)) |
| 39 | + var response = "" |
| 40 | + var line: String |
| 41 | + while ((bufferedReader.readLine().also { line = it }) != null) { |
| 42 | + response += line |
43 | 43 | } |
44 | | - Log.e(MultimediaDownloader.class.getName(), "responseCode: " + responseCode + ", response: " + response); |
| 44 | + Log.e( |
| 45 | + MultimediaDownloader::class.java.name, |
| 46 | + "responseCode: $responseCode, response: $response" |
| 47 | + ) |
45 | 48 | } |
46 | | - } catch (IOException e) { |
47 | | - Log.e(MultimediaDownloader.class.getName(), null, e); |
| 49 | + } catch (e: IOException) { |
| 50 | + Log.e(MultimediaDownloader::class.java.name, null, e) |
48 | 51 | } |
49 | 52 |
|
50 | | - return bytes; |
| 53 | + return bytes |
51 | 54 | } |
52 | 55 | } |
0 commit comments