urbanairship-core/src/main/java/com/urbanairship/util/FileUtils.java
@NonNull
    @WorkerThread
    public static DownloadResult downloadFile(@NonNull URL url, @NonNull File file) throws IOException {
        Logger.verbose("Downloading file from: " + url + " to: " + file.getAbsolutePath());

        InputStream inputStream = null;
        FileOutputStream outputStream = null;
        URLConnection conn = null;

        try {
            conn = url.openConnection();
            conn.setConnectTimeout(NETWORK_TIMEOUT_MS);
            conn.setUseCaches(true);
            inputStream = conn.getInputStream();

            int statusCode = 0;

            if (conn instanceof HttpURLConnection) {
                statusCode = ((HttpURLConnection) conn).getResponseCode();

                if (!UAHttpStatusUtil.inSuccessRange(((HttpURLConnection) conn).getResponseCode())) {
                    return new DownloadResult(false, statusCode);
                }
            }


            if (inputStream != null) {
                outputStream = new FileOutputStream(file);

                byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead;

                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }

                outputStream.close();
                inputStream.close();

                return new DownloadResult(true, statusCode);
            }

            return new DownloadResult(false, statusCode);
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }

            if (inputStream != null) {
                inputStream.close();
            }

            if (conn != null && conn instanceof HttpURLConnection) {
                ((HttpURLConnection) conn).disconnect();
            }
        }
    }
/**
     * Downloads a file to disk.
     *
     * @param url The URL image.
     * @param file The file path where the image will be downloaded.
     * @return The download result.
     * @throws IOException
     */
Downloads a file to disk.
@param url The URL image.
@param file The file path where the image will be downloaded.
@return The download result.
Downloads a file to disk . @param url The URL image . @param file The file path where the image will be downloaded . @return The download result .
@NonNull
    @WorkerThread
    public static DownloadResult downloadFile(@NonNull URL url, @NonNull File file) throws IOException {
        Logger.verbose("Downloading file from: " + url + " to: " + file.getAbsolutePath());
        InputStream inputStream = null;
        FileOutputStream outputStream = null;
        URLConnection conn = null;
        try {
            conn = url.openConnection();
            conn.setConnectTimeout(NETWORK_TIMEOUT_MS);
            conn.setUseCaches(true);
            inputStream = conn.getInputStream();
            int statusCode = 0;
            if (conn instanceof HttpURLConnection) {
                statusCode = ((HttpURLConnection) conn).getResponseCode();
                if (!UAHttpStatusUtil.inSuccessRange(((HttpURLConnection) conn).getResponseCode())) {
                    return new DownloadResult(false, statusCode);
                }
            }
            if (inputStream != null) {
                outputStream = new FileOutputStream(file);
                byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                outputStream.close();
                inputStream.close();
                return new DownloadResult(true, statusCode);
            }
            return new DownloadResult(false, statusCode);
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (conn != null && conn instanceof HttpURLConnection) {
                ((HttpURLConnection) conn).disconnect();
            }
        }
    }
@ NonNull @ WorkerThread public static DownloadResult downloadFile ( @ NonNull URL url , @ NonNull File file ) throws IOException { Logger . verbose ( "Downloading file from: " + url + " to: " + file . getAbsolutePath ( ) ) ; InputStream inputStream = null ; FileOutputStream outputStream = null ; URLConnection conn = null ; try { conn = url . openConnection ( ) ; conn . setConnectTimeout ( NETWORK_TIMEOUT_MS ) ; conn . setUseCaches ( true ) ; inputStream = conn . getInputStream ( ) ; int statusCode = 0 ; if ( conn instanceof HttpURLConnection ) { statusCode = ( ( HttpURLConnection ) conn ) . getResponseCode ( ) ; if ( ! UAHttpStatusUtil . inSuccessRange ( ( ( HttpURLConnection ) conn ) . getResponseCode ( ) ) ) { return new DownloadResult ( false , statusCode ) ; } } if ( inputStream != null ) { outputStream = new FileOutputStream ( file ) ; byte [ ] buffer = new byte [ BUFFER_SIZE ] ; int bytesRead ; while ( ( bytesRead = inputStream . read ( buffer ) ) != - 1 ) { outputStream . write ( buffer , 0 , bytesRead ) ; } outputStream . close ( ) ; inputStream . close ( ) ; return new DownloadResult ( true , statusCode ) ; } return new DownloadResult ( false , statusCode ) ; } finally { if ( outputStream != null ) { outputStream . close ( ) ; } if ( inputStream != null ) { inputStream . close ( ) ; } if ( conn != null && conn instanceof HttpURLConnection ) { ( ( HttpURLConnection ) conn ) . disconnect ( ) ; } } }
Downloads a file to disk.
Downloads a file to disk .