Example usage for java.net URLConnection getContentLength

List of usage examples for java.net URLConnection getContentLength

Introduction

In this page you can find the example usage for java.net URLConnection getContentLength.

Prototype

public int getContentLength() 

Source Link

Document

Returns the value of the content-length header field.

Usage

From source file:de.ipk_gatersleben.ag_nw.graffiti.services.GUIhelper.java

public static boolean performDownload(String downloadURL, String opt_local_folder,
        BackgroundTaskStatusProviderSupportingExternalCallImpl status, String targetfilename) {
    if (status == null)
        status = new BackgroundTaskConsoleLogger("downloading: " + downloadURL,
                "target folder: " + opt_local_folder, false);
    String fileName = targetfilename;
    if (fileName == null)
        fileName = downloadURL.substring(downloadURL.lastIndexOf("/") + 1);
    if (downloadURL.contains("|")) {
        fileName = downloadURL.substring(downloadURL.lastIndexOf("|") + 1);
        downloadURL = downloadURL.substring(0, downloadURL.indexOf("|"));
    }// ww  w . ja  va2  s. com
    String target = opt_local_folder;
    String targetFileName = target + fileName;
    boolean downloadOK = true;

    OutputStream out = null;
    URLConnection conn = null;
    InputStream in = null;
    long numWritten = 0;
    boolean ftp = false;
    ObjectRef lastStatus = new ObjectRef();
    try {
        boolean downloadNeeded = true;
        if (downloadURL.indexOf("ftp://") == 0) {
            ftp = true;
            try {
                if (!processFTPdownload(status, downloadURL, targetFileName, lastStatus))
                    downloadOK = false;
                else
                    downloadNeeded = false;
            } catch (Exception err) {
                ftp = false;
                System.out.println("ERROR: FTP download failed: " + err.getMessage());
                System.out.println("INFO: TRYING DEFAULT URL DOWNLOAD METHOD");
            }
        }
        ;
        if (downloadNeeded) {
            URL url = new URL(downloadURL);
            conn = url.openConnection();
            int contentLength = conn.getContentLength();
            in = conn.getInputStream();
            byte[] buffer = new byte[512 * 1024];
            int numRead;
            out = new BufferedOutputStream(new FileOutputStream(targetFileName));
            status.setCurrentStatusText1(downloadURL);
            status.setCurrentStatusText2("Received " + (numWritten / 1024) + " KB");
            while ((numRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, numRead);
                numWritten += numRead;
                status.setCurrentStatusText1(downloadURL);
                status.setCurrentStatusText2("Received " + (numWritten / 1024) + " KB");
                if (contentLength > 0) {
                    status.setCurrentStatusValueFine(100d * numWritten / contentLength);
                }
                if (status.wantsToStop()) {
                    break;
                }
            }
            downloadOK = true;
        }
    } catch (Exception e) {
        ErrorMsg.addErrorMessage(e);
        downloadOK = false;
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
            if (ftp) {
                if (lastStatus != null && lastStatus.getObject() != null
                        && (lastStatus.getObject() instanceof String)) {
                    status.setCurrentStatusText1((String) lastStatus.getObject());
                } else
                    status.setCurrentStatusText1(status.getCurrentStatusMessage2());
                status.setCurrentStatusText2("Finished: " + fileName);
            } else {
                status.setCurrentStatusText1("Received " + (numWritten / 1024) + " KB");
                status.setCurrentStatusText2("Finished: " + fileName);
            }
        } catch (Exception e) {
            ErrorMsg.addErrorMessage(e);
            downloadOK = false;
        }
    }
    return downloadOK;
}

From source file:org.wso2.emm.system.service.api.OTADownload.java

public void onStateChecked(int error, final BuildPropParser parser) {
    final String operation = Preference.getBoolean(context,
            context.getResources().getString(R.string.firmware_status_check_in_progress))
                    ? Constants.Operation.GET_FIRMWARE_UPGRADE_PACKAGE_STATUS
                    : Constants.Operation.UPGRADE_FIRMWARE;
    if (error == 0) {
        if (!otaServerManager.compareLocalVersionToServer(parser)) {
            Log.i(TAG, "Software is up to date:" + Build.VERSION.RELEASE + ", " + Build.ID);
            JSONObject result = new JSONObject();
            try {
                result.put(UPGRADE_AVAILABLE, false);
                if (parser != null) {
                    result.put(UPGRADE_DESCRIPTION, parser.getProp("Software is up to date"));
                }/*from  w w  w.  j  a  v  a  2s  .  c  o  m*/
                CommonUtils.sendBroadcast(context, operation, Constants.Code.SUCCESS,
                        Constants.Status.NO_UPGRADE_FOUND, result.toString());
            } catch (JSONException e) {
                String message = "Result payload build failed.";
                CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE,
                        Constants.Status.UPDATE_INFO_NOT_READABLE, message);
                Log.e(TAG, message + e);
            }
        } else if (checkNetworkOnline()) {
            new AsyncTask<Void, Void, Long>() {
                protected Long doInBackground(Void... param) {
                    URL url = otaServerManager.getServerConfig().getPackageURL();
                    URLConnection con;
                    try {
                        con = url.openConnection();
                        con.setConnectTimeout(Constants.FIRMWARE_UPGRADE_CONNECTIVITY_TIMEOUT);
                        con.setReadTimeout(Constants.FIRMWARE_UPGRADE_READ_TIMEOUT);
                        return (long) con.getContentLength();
                    } catch (SocketTimeoutException e) {
                        String message = "Connection failure (Socket timeout) when retrieving update package size.";
                        Log.e(TAG, message + e);
                        CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE,
                                Constants.Status.CONNECTION_FAILED, message);
                        CommonUtils.callAgentApp(context,
                                Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION, 0, null);
                        return (long) -1;
                    } catch (IOException e) {
                        String message = "Connection failure when retrieving update package size.";
                        Log.e(TAG, message + e);
                        CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE,
                                Constants.Status.CONNECTION_FAILED, message);
                        CommonUtils.callAgentApp(context,
                                Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION, 0, null);
                        return (long) -1;
                    }
                }

                protected void onPostExecute(Long bytes) {
                    Log.i(TAG, "New release found " + Build.VERSION.RELEASE + ", " + Build.ID);
                    String length = "Unknown";
                    if (bytes > 0) {
                        length = byteCountToDisplaySize(bytes, false);
                    }

                    Log.i(TAG, "version :" + parser.getProp("ro.build.id") + "\n" + "full_version :"
                            + parser.getProp("ro.build.description") + "\n" + "size : " + length);
                    //Downloading the new update package if a new version is available.
                    if (Preference.getBoolean(context,
                            context.getResources().getString(R.string.firmware_status_check_in_progress))) {
                        JSONObject result = new JSONObject();
                        try {
                            result.put(UPGRADE_AVAILABLE, true);
                            result.put(UPGRADE_SIZE, length);
                            result.put(UPGRADE_RELEASE, parser.getNumRelease());
                            result.put(UPGRADE_VERSION, parser.getProp("ro.build.id"));
                            result.put(UPGRADE_DESCRIPTION, parser.getProp("ro.build.description"));
                            CommonUtils.sendBroadcast(context,
                                    Constants.Operation.GET_FIRMWARE_UPGRADE_PACKAGE_STATUS,
                                    Constants.Code.SUCCESS, Constants.Status.SUCCESSFUL, result.toString());
                        } catch (JSONException e) {
                            String message = "Result payload build failed.";
                            CommonUtils.sendBroadcast(context,
                                    Constants.Operation.GET_FIRMWARE_UPGRADE_PACKAGE_STATUS,
                                    Constants.Code.FAILURE, Constants.Status.OTA_IMAGE_VERIFICATION_FAILED,
                                    message);
                            Log.e(TAG, message + e);
                        }

                    } else {
                        if (checkNetworkOnline()) {
                            Boolean isAutomaticRetry = (Preference.hasPreferenceKey(context,
                                    context.getResources().getString(R.string.firmware_upgrade_automatic_retry))
                                    && Preference.getBoolean(context,
                                            context.getResources()
                                                    .getString(R.string.firmware_upgrade_automatic_retry)))
                                    || !Preference.hasPreferenceKey(context, context.getResources()
                                            .getString(R.string.firmware_upgrade_automatic_retry));

                            if (getBatteryLevel(
                                    context) >= Constants.REQUIRED_BATTERY_LEVEL_TO_FIRMWARE_UPGRADE) {
                                otaServerManager.startDownloadUpgradePackage(otaServerManager);
                            } else if (isAutomaticRetry) {
                                String message = "Upgrade download has been differed due to insufficient battery level.";
                                Log.w(TAG, message);
                                Preference.putString(context,
                                        context.getResources().getString(R.string.upgrade_download_status),
                                        Constants.Status.BATTERY_LEVEL_INSUFFICIENT_TO_DOWNLOAD);
                                CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                                        Constants.Code.PENDING,
                                        Constants.Status.BATTERY_LEVEL_INSUFFICIENT_TO_DOWNLOAD, message);
                            } else {
                                String message = "Upgrade download has been failed due to insufficient battery level.";
                                Preference.putString(context,
                                        context.getResources().getString(R.string.upgrade_download_status),
                                        Constants.Status.BATTERY_LEVEL_INSUFFICIENT_TO_DOWNLOAD);
                                Log.e(TAG, message);
                                CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                                        Constants.Code.FAILURE,
                                        Constants.Status.BATTERY_LEVEL_INSUFFICIENT_TO_DOWNLOAD, message);
                                CommonUtils.callAgentApp(context, Constants.Operation.FIRMWARE_UPGRADE_FAILURE,
                                        0, message);
                            }
                        } else {
                            String message = "Connection failure when starting upgrade download.";
                            Log.e(TAG, message);
                            CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                                    Constants.Code.FAILURE, Constants.Status.NETWORK_UNREACHABLE, message);
                            CommonUtils.callAgentApp(context,
                                    Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION, 0, message);
                        }
                    }
                }
            }.execute();

        } else {
            String message = "Connection failure when starting build prop download.";
            Log.e(TAG, message);
            CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE,
                    Constants.Status.CONNECTION_FAILED, message);
            CommonUtils.callAgentApp(context, Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION, 0,
                    null);
        }
    } else if (error == ERROR_WIFI_NOT_AVAILABLE) {
        Preference.putString(context, context.getResources().getString(R.string.upgrade_download_status),
                Constants.Status.WIFI_OFF);
        Log.e(TAG, "OTA failed due to WIFI connection failure.");
    } else if (error == ERROR_CANNOT_FIND_SERVER) {
        String message = "OTA failed due to OTA server not accessible.";
        Log.e(TAG, message);
    } else if (error == ERROR_WRITE_FILE_ERROR) {
        String message = "OTA failed due to file write error.";
        Log.e(TAG, message);
    }
}

From source file:org.rioproject.impl.util.DownloadManager.java

private DownloadRecord doDownload(StagedData dAttrs, boolean postInstall) throws IOException {
    String installRoot;/*from   w w w .  j a  v a2s  .  c o m*/
    int extractedSize = 0;
    long extractTime = 0;
    boolean unarchived = false;
    boolean createdTargetPath = false;

    if (dAttrs == null)
        throw new IllegalArgumentException("dAttrs is null");

    URL location = dAttrs.getLocationURL();
    String extension = dAttrs.getInstallRoot();
    boolean unarchive = dAttrs.unarchive();
    if (extension.indexOf("/") != -1)
        installRoot = extension.replace('/', File.separatorChar);
    else
        installRoot = extension.replace('\\', File.separatorChar);
    File targetPath = new File(FileUtils.makeFileName(installPath, installRoot));
    if (!targetPath.exists()) {
        if (targetPath.mkdirs()) {
            logger.trace("Created {}", targetPath.getPath());
        }
        if (!targetPath.exists())
            throw new IOException("Failed to create: " + installPath);
        createdTargetPath = true;
    }
    if (!targetPath.canWrite())
        throw new IOException("Can not write to : " + installPath);
    String source = location.toExternalForm();
    int index = source.lastIndexOf("/");
    if (index == -1)
        throw new IllegalArgumentException("Don't know how to install : " + source);
    String software = source.substring(index + 1);
    String target = FileUtils.getFilePath(targetPath);
    File targetFile = new File(FileUtils.makeFileName(target, software));
    if (targetFile.exists()) {
        if (!dAttrs.overwrite()) {
            logger.warn("{} exists, stagedData attributes indicate to not overwrite file",
                    FileUtils.getFilePath(targetFile));
            return null;
        } else {
            if (showDownloadTo)
                logger.info("Overwriting {} with {}", FileUtils.getFilePath(targetFile), location);
        }
    } else {
        if (showDownloadTo)
            logger.info("Downloading {} to {}", location, FileUtils.getFilePath(targetFile));
    }
    long t0 = System.currentTimeMillis();
    URLConnection con = location.openConnection();
    int downloadedSize = writeFileFromInputStream(con.getInputStream(), targetFile, con.getContentLength(),
            System.console() != null);
    long t1 = System.currentTimeMillis();
    long downloadTime = t1 - t0;
    long downloadSecs = downloadTime / 1000;
    Date downloadDate = new Date();
    ExtractResults results;
    logger.info("Wrote {}K in {} seconds", (downloadedSize / 1024), (downloadSecs < 1 ? "< 1" : downloadSecs));
    String extractedToPath = null;
    if (unarchive) {
        t0 = System.currentTimeMillis();
        results = extract(targetPath, targetFile);
        t1 = System.currentTimeMillis();
        extractedSize = results.extractedSize;
        if (postInstall)
            postInstallExtractList = results.postInstallExtractList;
        extractTime = t1 - t0;
        unarchived = true;
        extractedToPath = results.extractedToPath;
        if (extractedToPath == null) {
            extractedToPath = FileUtils.getFilePath(targetPath);
        }
    }
    downloadRecord = new DownloadRecord(location, target, software, downloadDate, downloadedSize, extractedSize,
            extractedToPath, unarchived, downloadTime, extractTime);
    downloadRecord.setCreatedParentDirectory(createdTargetPath);
    return (downloadRecord);
}

From source file:org.wso2.iot.system.service.api.OTADownload.java

public void onStateChecked(int error, final BuildPropParser parser) {
    final String operation = Preference.getBoolean(context,
            context.getResources().getString(R.string.firmware_status_check_in_progress))
                    ? Constants.Operation.GET_FIRMWARE_UPGRADE_PACKAGE_STATUS
                    : Constants.Operation.UPGRADE_FIRMWARE;
    if (error == 0) {
        if (!otaServerManager.compareLocalVersionToServer(parser)) {
            JSONObject result = new JSONObject();
            try {
                result.put(UPGRADE_AVAILABLE, false);
                if (parser != null) {
                    result.put(UPGRADE_DESCRIPTION, parser.getProp("Software is up to date"));
                }/*  w w  w  .j a v a2 s. com*/
                CommonUtils.sendBroadcast(context, operation, Constants.Code.SUCCESS,
                        Constants.Status.NO_UPGRADE_FOUND, result.toString());
            } catch (JSONException e) {
                String message = "Result payload build failed.";
                CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE,
                        Constants.Status.UPDATE_INFO_NOT_READABLE, message);
                Log.e(TAG, message + e);
            }
            String message = "Software is up to date:" + Build.VERSION.RELEASE + ", " + Build.ID;
            Log.i(TAG, message);
            CommonUtils.callAgentApp(context, Constants.Operation.FIRMWARE_UPGRADE_COMPLETE,
                    Preference.getInt(context, context.getResources().getString(R.string.operation_id)),
                    "No upgrades available. " + message);
        } else if (checkNetworkOnline()) {
            new AsyncTask<Void, Void, Long>() {
                protected Long doInBackground(Void... param) {
                    URL url = otaServerManager.getServerConfig().getPackageURL();
                    URLConnection con;
                    try {
                        con = url.openConnection();
                        con.setConnectTimeout(Constants.FIRMWARE_UPGRADE_CONNECTIVITY_TIMEOUT);
                        con.setReadTimeout(Constants.FIRMWARE_UPGRADE_READ_TIMEOUT);
                        return (long) con.getContentLength();
                    } catch (SocketTimeoutException e) {
                        String message = "Connection failure (Socket timeout) when retrieving update package size.";
                        Log.e(TAG, message + e);
                        CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE,
                                Constants.Status.CONNECTION_FAILED, message);
                        CommonUtils
                                .callAgentApp(context, Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION,
                                        Preference.getInt(context,
                                                context.getResources().getString(R.string.operation_id)),
                                        message);
                        return (long) -1;
                    } catch (IOException e) {
                        String message = "Connection failure when retrieving update package size.";
                        Log.e(TAG, message + e);
                        CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE,
                                Constants.Status.CONNECTION_FAILED, message);
                        CommonUtils
                                .callAgentApp(context, Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION,
                                        Preference.getInt(context,
                                                context.getResources().getString(R.string.operation_id)),
                                        message);
                        return (long) -1;
                    }
                }

                protected void onPostExecute(Long bytes) {
                    Log.i(TAG, "New release found " + Build.VERSION.RELEASE + ", " + Build.ID);
                    String length = "Unknown";
                    if (bytes > 0) {
                        length = byteCountToDisplaySize(bytes, false);
                    }

                    Log.i(TAG, "version :" + parser.getProp("ro.build.id") + "\n" + "full_version :"
                            + parser.getProp("ro.build.description") + "\n" + "size : " + length);
                    //Downloading the new update package if a new version is available.
                    if (Preference.getBoolean(context,
                            context.getResources().getString(R.string.firmware_status_check_in_progress))) {
                        JSONObject result = new JSONObject();
                        try {
                            result.put(UPGRADE_AVAILABLE, true);
                            result.put(UPGRADE_SIZE, length);
                            result.put(UPGRADE_RELEASE, parser.getNumRelease());
                            result.put(UPGRADE_VERSION, parser.getProp("ro.build.id"));
                            result.put(UPGRADE_DESCRIPTION, parser.getProp("ro.build.description"));
                            CommonUtils.sendBroadcast(context,
                                    Constants.Operation.GET_FIRMWARE_UPGRADE_PACKAGE_STATUS,
                                    Constants.Code.SUCCESS, Constants.Status.SUCCESSFUL, result.toString());
                        } catch (JSONException e) {
                            String message = "Result payload build failed.";
                            CommonUtils.sendBroadcast(context,
                                    Constants.Operation.GET_FIRMWARE_UPGRADE_PACKAGE_STATUS,
                                    Constants.Code.FAILURE, Constants.Status.OTA_IMAGE_VERIFICATION_FAILED,
                                    message);
                            Log.e(TAG, message + e);
                        }

                    } else {
                        Boolean isAutomaticRetry = !Preference.hasPreferenceKey(context,
                                context.getResources().getString(R.string.firmware_upgrade_automatic_retry))
                                || Preference.getBoolean(context, context.getResources()
                                        .getString(R.string.firmware_upgrade_automatic_retry));

                        if (checkNetworkOnline()) {
                            if (getBatteryLevel(
                                    context) >= Constants.REQUIRED_BATTERY_LEVEL_TO_FIRMWARE_UPGRADE) {
                                otaServerManager.startDownloadUpgradePackage(otaServerManager);
                            } else if (isAutomaticRetry) {
                                String message = "Upgrade download has been differed due to insufficient battery level.";
                                Log.w(TAG, message);
                                Preference.putString(context,
                                        context.getResources().getString(R.string.upgrade_download_status),
                                        Constants.Status.BATTERY_LEVEL_INSUFFICIENT_TO_DOWNLOAD);
                                CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                                        Constants.Code.PENDING,
                                        Constants.Status.BATTERY_LEVEL_INSUFFICIENT_TO_DOWNLOAD, message);
                            } else {
                                String message = "Upgrade download has been failed due to insufficient battery level.";
                                Preference.putString(context,
                                        context.getResources().getString(R.string.upgrade_download_status),
                                        Constants.Status.BATTERY_LEVEL_INSUFFICIENT_TO_DOWNLOAD);
                                Log.e(TAG, message);
                                CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                                        Constants.Code.FAILURE,
                                        Constants.Status.BATTERY_LEVEL_INSUFFICIENT_TO_DOWNLOAD, message);
                                CommonUtils.callAgentApp(context, Constants.Operation.FIRMWARE_UPGRADE_FAILURE,
                                        Preference.getInt(context,
                                                context.getResources().getString(R.string.operation_id)),
                                        message);
                            }
                        } else {
                            String message = "Connection failure when starting upgrade download.";
                            Log.e(TAG, message);
                            if (isAutomaticRetry) {
                                Preference.putString(context,
                                        context.getResources().getString(R.string.upgrade_download_status),
                                        Constants.Status.NETWORK_UNREACHABLE);
                                CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                                        Constants.Code.PENDING, Constants.Status.NETWORK_UNREACHABLE, message);
                                CommonUtils.callAgentApp(context,
                                        Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION,
                                        Preference.getInt(context,
                                                context.getResources().getString(R.string.operation_id)),
                                        message);
                            } else {
                                CommonUtils.sendBroadcast(context, Constants.Operation.UPGRADE_FIRMWARE,
                                        Constants.Code.FAILURE, Constants.Status.NETWORK_UNREACHABLE, message);
                                CommonUtils.callAgentApp(context, Constants.Operation.FIRMWARE_UPGRADE_FAILURE,
                                        Preference.getInt(context,
                                                context.getResources().getString(R.string.operation_id)),
                                        message);
                            }
                        }
                    }
                }
            }.execute();

        } else {
            String message = "Connection failure when starting build prop download.";
            Log.e(TAG, message);
            CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE,
                    Constants.Status.CONNECTION_FAILED, message);
            CommonUtils.callAgentApp(context, Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION,
                    Preference.getInt(context, context.getResources().getString(R.string.operation_id)), null);
        }
    } else if (error == ERROR_WIFI_NOT_AVAILABLE) {
        Preference.putString(context, context.getResources().getString(R.string.upgrade_download_status),
                Constants.Status.WIFI_OFF);
        Log.e(TAG, "OTA failed due to WIFI connection failure.");
    } else if (error == ERROR_CANNOT_FIND_SERVER) {
        String message = "OTA failed due to OTA server not accessible.";
        Log.e(TAG, message);
    } else if (error == ERROR_WRITE_FILE_ERROR) {
        String message = "OTA failed due to file write error.";
        Log.e(TAG, message);
    }
}

From source file:eu.faircode.netguard.ServiceExternal.java

@Override
protected void onHandleIntent(Intent intent) {
    try {/*  w  w  w  .j a v  a 2 s .  c om*/
        startForeground(ServiceSinkhole.NOTIFY_EXTERNAL, getForegroundNotification(this));

        Log.i(TAG, "Received " + intent);
        Util.logExtras(intent);

        if (ACTION_DOWNLOAD_HOSTS_FILE.equals(intent.getAction())) {
            final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

            String hosts_url = prefs.getString("hosts_url", null);
            File tmp = new File(getFilesDir(), "hosts.tmp");
            File hosts = new File(getFilesDir(), "hosts.txt");

            InputStream in = null;
            OutputStream out = null;
            URLConnection connection = null;
            try {
                URL url = new URL(hosts_url);
                connection = url.openConnection();
                connection.connect();

                if (connection instanceof HttpURLConnection) {
                    HttpURLConnection httpConnection = (HttpURLConnection) connection;
                    if (httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK)
                        throw new IOException(
                                httpConnection.getResponseCode() + " " + httpConnection.getResponseMessage());
                }

                int contentLength = connection.getContentLength();
                Log.i(TAG, "Content length=" + contentLength);
                in = connection.getInputStream();
                out = new FileOutputStream(tmp);

                long size = 0;
                byte buffer[] = new byte[4096];
                int bytes;
                while ((bytes = in.read(buffer)) != -1) {
                    out.write(buffer, 0, bytes);
                    size += bytes;
                }

                Log.i(TAG, "Downloaded size=" + size);

                if (hosts.exists())
                    hosts.delete();
                tmp.renameTo(hosts);

                String last = SimpleDateFormat.getDateTimeInstance().format(new Date().getTime());
                prefs.edit().putString("hosts_last_download", last).apply();

                ServiceSinkhole.reload("hosts file download", this, false);

            } catch (Throwable ex) {
                Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));

                if (tmp.exists())
                    tmp.delete();
            } finally {
                try {
                    if (out != null)
                        out.close();
                } catch (IOException ex) {
                    Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }
                try {
                    if (in != null)
                        in.close();
                } catch (IOException ex) {
                    Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
                }

                if (connection instanceof HttpURLConnection)
                    ((HttpURLConnection) connection).disconnect();
            }
        }
    } finally {
        stopForeground(true);
    }
}

From source file:org.tolven.plugin.repository.RepositoryUpgrade.java

private String copy(PluginVersionDetail usePluginVersion, File runtimeRepository) {
    try {//  www  . j  a  va  2s.  c  o m
        File tmpFile = null;
        try {
            tmpFile = File.createTempFile("tmpPlugin_", ".zip");
            tmpFile.deleteOnExit();
            URL url = new URL(usePluginVersion.getUri());
            String destFilename = new File(url.getFile()).getName();
            File destFile = new File(runtimeRepository, destFilename);
            InputStream in = null;
            FileOutputStream out = null;
            int bytesDownload = 0;
            long startTime = 0;
            long endTime = 0;
            try {
                URLConnection urlConnection = url.openConnection();
                bytesDownload = urlConnection.getContentLength();
                in = urlConnection.getInputStream();
                out = new FileOutputStream(tmpFile);
                startTime = System.currentTimeMillis();
                IOUtils.copy(in, out);
                endTime = System.currentTimeMillis();
            } finally {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            }
            String downloadSpeedInfo = null;
            long downloadSpeed = 0;
            if ((endTime - startTime) > 0) {
                downloadSpeed = 1000L * bytesDownload / (endTime - startTime);
            }
            if (downloadSpeed == 0) {
                downloadSpeedInfo = "? B/s";
            } else if (downloadSpeed < 1000) {
                downloadSpeedInfo = downloadSpeed + " B/s";
            } else if (downloadSpeed < 1000000) {
                downloadSpeedInfo = downloadSpeed / 1000 + " KB/s";
            } else if (downloadSpeed < 1000000000) {
                downloadSpeedInfo = downloadSpeed / 1000000 + " MB/s";
            } else {
                downloadSpeedInfo = downloadSpeed / 1000000000 + " GB/s";
            }
            String tmpFileMessageDigest = getMessageDigest(tmpFile.toURI().toURL()).getValue();
            if (!tmpFileMessageDigest.equals(usePluginVersion.getMessageDigest().getValue())) {
                throw new RuntimeException("Downloaded file: " + usePluginVersion.getUri()
                        + " does not have required message digest: "
                        + usePluginVersion.getMessageDigest().getValue());

            }
            if (!isNoop()) {
                FileUtils.copyFile(tmpFile, destFile);
            }
            return bytesDownload + " Bytes " + downloadSpeedInfo;
        } finally {
            if (tmpFile != null) {
                tmpFile.delete();
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException(
                "Could not download " + usePluginVersion.getUri() + " to " + runtimeRepository, ex);
    }
}

From source file:org.mozilla.gecko.updater.UpdateService.java

private File downloadUpdatePackage(UpdateInfo info, boolean overwriteExisting) {
    URL url = null;/*  w ww  .j  a v a  2 s  . co m*/
    try {
        url = info.uri.toURL();
    } catch (java.net.MalformedURLException e) {
        Log.e(LOGTAG, "failed to read URL: ", e);
        return null;
    }

    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    path.mkdirs();
    String fileName = new File(url.getFile()).getName();
    File downloadFile = new File(path, fileName);

    if (!overwriteExisting && info.buildID.equals(getLastBuildID()) && downloadFile.exists()) {
        // The last saved buildID is the same as the one for the current update. We also have a file
        // already downloaded, so it's probably the package we want. Verify it to be sure and just
        // return that if it matches.

        if (verifyDownloadedPackage(downloadFile)) {
            Log.i(LOGTAG, "using existing update package");
            return downloadFile;
        } else {
            // Didn't match, so we're going to download a new one.
            downloadFile.delete();
        }
    }

    if (!info.buildID.equals(getLastBuildID())) {
        // Delete the previous package when a new version becomes available.
        deleteUpdatePackage(getLastFileName());
    }

    Log.i(LOGTAG, "downloading update package");
    sendCheckUpdateResult(CheckUpdateResult.DOWNLOADING);

    OutputStream output = null;
    InputStream input = null;

    mDownloading = true;
    mCancelDownload = false;
    showDownloadNotification(downloadFile);

    try {
        NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected() && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            mWifiLock.acquire();
        }

        URLConnection conn = openConnectionWithProxy(info.uri);
        int length = conn.getContentLength();

        output = new BufferedOutputStream(new FileOutputStream(downloadFile));
        input = new BufferedInputStream(conn.getInputStream());

        byte[] buf = new byte[BUFSIZE];
        int len = 0;

        int bytesRead = 0;
        int lastNotify = 0;

        while ((len = input.read(buf, 0, BUFSIZE)) > 0 && !mCancelDownload) {
            output.write(buf, 0, len);
            bytesRead += len;
            // Updating the notification takes time so only do it every 1MB
            if (bytesRead - lastNotify > 1048576) {
                mBuilder.setProgress(length, bytesRead, false);
                mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
                lastNotify = bytesRead;
            }
        }

        mNotificationManager.cancel(NOTIFICATION_ID);

        // if the download was canceled by the user
        // delete the update package
        if (mCancelDownload) {
            Log.i(LOGTAG, "download canceled by user!");
            downloadFile.delete();

            return null;
        } else {
            Log.i(LOGTAG, "completed update download!");
            return downloadFile;
        }
    } catch (Exception e) {
        downloadFile.delete();
        showDownloadFailure();

        Log.e(LOGTAG, "failed to download update: ", e);
        return null;
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (java.io.IOException e) {
        }

        try {
            if (output != null)
                output.close();
        } catch (java.io.IOException e) {
        }

        mDownloading = false;

        if (mWifiLock.isHeld()) {
            mWifiLock.release();
        }
    }
}

From source file:com.ibm.jaggr.service.impl.modulebuilder.css.CSSModuleBuilder.java

/**
 * Replace <code>url(&lt;<i>relative-path</i>&gt;)</code> references in the
 * input CSS with/*from   ww  w  .j av a2s  .  co m*/
 * <code>url(data:&lt;<i>mime-type</i>&gt;;&lt;<i>base64-encoded-data</i>&gt;</code>
 * ). The conversion is controlled by option settings as described in
 * {@link CSSModuleBuilder}.
 * 
 * @param css
 *            The input CSS
 * @param uri
 *            The URI for the input CSS
 * @return The transformed CSS with images in-lined as determined by option
 *         settings.
 */
protected String inlineImageUrls(HttpServletRequest req, String css, IResource res) {
    if (imageSizeThreshold == 0 && inlinedImageIncludeList.size() == 0) {
        // nothing to do
        return css;
    }

    // In-lining of imports can be disabled by request parameter for debugging
    if (!TypeUtil.asBoolean(req.getParameter(INLINEIMAGES_REQPARAM_NAME), true)) {
        return css;
    }

    StringBuffer buf = new StringBuffer();
    Matcher m = urlPattern.matcher(css);
    while (m.find()) {
        String fullMatch = m.group(0);
        String urlMatch = m.group(1);

        // remove quotes.
        urlMatch = quotedStringPattern.matcher(urlMatch).replaceAll(""); //$NON-NLS-1$
        urlMatch = forwardSlashPattern.matcher(urlMatch).replaceAll("/"); //$NON-NLS-1$

        // Don't do anything with non-relative URLs
        if (urlMatch.startsWith("/") || urlMatch.startsWith("#") || protocolPattern.matcher(urlMatch).find()) { //$NON-NLS-1$ //$NON-NLS-2$
            m.appendReplacement(buf, ""); //$NON-NLS-1$
            buf.append(fullMatch);
            continue;
        }

        URI imageUri = res.getURI().resolve(urlMatch);
        boolean exclude = false, include = false;

        // Determine if this image is in the include list
        for (Pattern regex : inlinedImageIncludeList) {
            if (regex.matcher(imageUri.getPath()).find()) {
                include = true;
                break;
            }
        }

        // Determine if this image is in the exclude list
        for (Pattern regex : inlinedImageExcludeList) {
            if (regex.matcher(imageUri.getPath()).find()) {
                exclude = true;
                break;
            }
        }
        // If there's an include list, then only the files in the include list
        // will be inlined
        if (inlinedImageIncludeList.size() > 0 && !include || exclude) {
            m.appendReplacement(buf, ""); //$NON-NLS-1$
            buf.append(fullMatch);
            continue;
        }

        boolean imageInlined = false;
        InputStream in = null;
        try {
            // In-line the image.
            URLConnection connection = imageUri.toURL().openConnection();
            in = connection.getInputStream();
            int size = connection.getContentLength();
            String type = connection.getContentType();
            if (type == null) {
                type = "content/unknown"; //$NON-NLS-1$
            }
            if (include || inlineableImageTypes.contains(type) && size <= imageSizeThreshold) {
                String base64 = getBase64(connection);
                m.appendReplacement(buf, ""); //$NON-NLS-1$
                buf.append("url('data:" + type + //$NON-NLS-1$
                        ";base64," + base64 + "')"); //$NON-NLS-1$ //$NON-NLS-2$
                imageInlined = true;
            }
        } catch (IOException ex) {
            if (log.isLoggable(Level.WARNING)) {
                log.log(Level.WARNING,
                        MessageFormat.format(Messages.CSSModuleBuilder_0, new Object[] { imageUri }), ex);
            }
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ignore) {
                }
            }
        }
        if (!imageInlined) {
            // Image not in-lined.  Write the original URL
            m.appendReplacement(buf, ""); //$NON-NLS-1$
            buf.append(fullMatch);
        }
    }
    m.appendTail(buf);
    return buf.toString();
}

From source file:org.apache.flex.utilities.converter.retrievers.download.DownloadRetriever.java

protected void performFastDownload(URL sourceUrl, File targetFile) throws IOException {
    final URLConnection connection = sourceUrl.openConnection();
    final ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
    final FileOutputStream fos = new FileOutputStream(targetFile);

    ////////////////////////////////////////////////////////////////////////////////
    // Do the downloading.
    ////////////////////////////////////////////////////////////////////////////////

    final long expectedSize = connection.getContentLength();
    long transferedSize = 0L;

    System.out.println("===========================================================");
    System.out.println("Downloading " + sourceUrl.toString());
    if (expectedSize > 1014 * 1024) {
        System.out.println("Expected size: " + (expectedSize / 1024 / 1024) + "MB");
    } else {/*  w w  w.j a v  a  2  s . c om*/
        System.out.println("Expected size: " + (expectedSize / 1024) + "KB");
    }
    final ProgressBar progressBar = new ProgressBar(expectedSize);
    while (transferedSize < expectedSize) {
        transferedSize += fos.getChannel().transferFrom(rbc, transferedSize, 1 << 20);
        progressBar.updateProgress(transferedSize);
    }
    fos.close();
    System.out.println();
    System.out.println("Finished downloading.");
    System.out.println("===========================================================");
}

From source file:com.ibm.jaggr.core.impl.modulebuilder.css.CSSModuleBuilder.java

/**
 * Replace <code>url(&lt;<i>relative-path</i>&gt;)</code> references in the
 * input CSS with/*  w  w  w  .  ja va  2 s . c om*/
 * <code>url(data:&lt;<i>mime-type</i>&gt;;&lt;<i>base64-encoded-data</i>&gt;</code>
 * ). The conversion is controlled by option settings as described in
 * {@link CSSModuleBuilder}.
 *
 * @param req
 *            The request associated with the call.
 * @param css
 *            The input CSS
 * @param res
 *            The resource for the CSS file
 * @return The transformed CSS with images in-lined as determined by option
 *         settings.
 */
protected String inlineImageUrls(HttpServletRequest req, String css, IResource res) {
    if (imageSizeThreshold == 0 && inlinedImageIncludeList.size() == 0) {
        // nothing to do
        return css;
    }

    // In-lining of imports can be disabled by request parameter for debugging
    if (!TypeUtil.asBoolean(req.getParameter(INLINEIMAGES_REQPARAM_NAME), true)) {
        return css;
    }

    StringBuffer buf = new StringBuffer();
    Matcher m = urlPattern.matcher(css);
    while (m.find()) {
        String fullMatch = m.group(0);
        String urlMatch = m.group(1);

        // remove quotes.
        urlMatch = dequote(urlMatch);
        urlMatch = forwardSlashPattern.matcher(urlMatch).replaceAll("/"); //$NON-NLS-1$

        // Don't do anything with non-relative URLs
        if (urlMatch.startsWith("/") || urlMatch.startsWith("#") || protocolPattern.matcher(urlMatch).find()) { //$NON-NLS-1$ //$NON-NLS-2$
            m.appendReplacement(buf, ""); //$NON-NLS-1$
            buf.append(fullMatch);
            continue;
        }

        URI imageUri = res.resolve(urlMatch).getURI();
        boolean exclude = false, include = false;

        // Determine if this image is in the include list
        for (Pattern regex : inlinedImageIncludeList) {
            if (regex.matcher(imageUri.getPath()).find()) {
                include = true;
                break;
            }
        }

        // Determine if this image is in the exclude list
        for (Pattern regex : inlinedImageExcludeList) {
            if (regex.matcher(imageUri.getPath()).find()) {
                exclude = true;
                break;
            }
        }
        // If there's an include list, then only the files in the include list
        // will be inlined
        if (inlinedImageIncludeList.size() > 0 && !include || exclude) {
            m.appendReplacement(buf, ""); //$NON-NLS-1$
            buf.append(fullMatch);
            continue;
        }

        boolean imageInlined = false;
        String type = URLConnection.getFileNameMap().getContentTypeFor(imageUri.getPath());
        String extension = PathUtil.getExtension(imageUri.getPath());
        if (type == null) {
            type = inlineableImageTypeMap.get(extension);
        }
        if (type == null) {
            type = "content/unknown"; //$NON-NLS-1$
        }
        if (include || inlineableImageTypes.contains(type) || inlineableImageTypeMap.containsKey(extension)) {
            InputStream in = null;
            try {
                // In-line the image.
                URLConnection connection = imageUri.toURL().openConnection();

                if (include || connection.getContentLength() <= imageSizeThreshold) {
                    in = connection.getInputStream();
                    String base64 = getBase64(connection);
                    m.appendReplacement(buf, ""); //$NON-NLS-1$
                    buf.append("url('data:" + type + //$NON-NLS-1$
                            ";base64," + base64 + "')"); //$NON-NLS-1$ //$NON-NLS-2$
                    imageInlined = true;
                }
            } catch (IOException ex) {
                if (log.isLoggable(Level.WARNING)) {
                    log.log(Level.WARNING,
                            MessageFormat.format(Messages.CSSModuleBuilder_0, new Object[] { imageUri }), ex);
                }
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ignore) {
                    }
                }
            }
        }
        if (!imageInlined) {
            // Image not in-lined.  Write the original URL
            m.appendReplacement(buf, ""); //$NON-NLS-1$
            buf.append(fullMatch);
        }
    }
    m.appendTail(buf);
    return buf.toString();
}