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:org.qedeq.base.io.UrlUtility.java

/**
 * Make local copy of an URL.//from w ww. j a  va 2s.  co m
 *
 * @param   url             Save this URL.
 * @param   f               Save into this file. An existing file is overwritten.
 * @param   proxyHost       Use this proxy host.
 * @param   proxyPort       Use this port at proxy host.
 * @param   nonProxyHosts   This are hosts not to be proxied.
 * @param   connectTimeout  Connection timeout.
 * @param   readTimeout     Read timeout.
 * @param   listener        Here completion events are fired.
 * @throws  IOException     Saving failed.
 */
public static void saveUrlToFile(final String url, final File f, final String proxyHost, final String proxyPort,
        final String nonProxyHosts, final int connectTimeout, final int readTimeout,
        final LoadingListener listener) throws IOException {
    final String method = "saveUrlToFile()";
    Trace.begin(CLASS, method);

    // if we are not web started and running under Java 1.4 we use apache commons
    // httpclient library (so we can set timeouts)
    if (!isSetConnectionTimeOutSupported() && !IoUtility.isWebStarted()) {
        saveQedeqFromWebToBufferApache(url, f, proxyHost, proxyPort, nonProxyHosts, connectTimeout, readTimeout,
                listener);
        Trace.end(CLASS, method);
        return;
    }

    // set proxy properties according to kernel configuration (if not webstarted)
    if (!IoUtility.isWebStarted()) {
        if (proxyHost != null) {
            System.setProperty("http.proxyHost", proxyHost);
        }
        if (proxyPort != null) {
            System.setProperty("http.proxyPort", proxyPort);
        }
        if (nonProxyHosts != null) {
            System.setProperty("http.nonProxyHosts", nonProxyHosts);
        }
    }

    FileOutputStream out = null;
    InputStream in = null;
    try {
        final URLConnection connection = new URL(url).openConnection();

        if (connection instanceof HttpURLConnection) {
            final HttpURLConnection httpConnection = (HttpURLConnection) connection;
            // if we are running at least under Java 1.5 the following code should be executed
            if (isSetConnectionTimeOutSupported()) {
                try {
                    YodaUtility.executeMethod(httpConnection, "setConnectTimeout", new Class[] { Integer.TYPE },
                            new Object[] { new Integer(connectTimeout) });
                } catch (NoSuchMethodException e) {
                    Trace.fatal(CLASS, method, "URLConnection.setConnectTimeout was previously found", e);
                } catch (InvocationTargetException e) {
                    Trace.fatal(CLASS, method, "URLConnection.setConnectTimeout throwed an error", e);
                }
            }
            // if we are running at least under Java 1.5 the following code should be executed
            if (isSetReadTimeoutSupported()) {
                try {
                    YodaUtility.executeMethod(httpConnection, "setReadTimeout", new Class[] { Integer.TYPE },
                            new Object[] { new Integer(readTimeout) });
                } catch (NoSuchMethodException e) {
                    Trace.fatal(CLASS, method, "URLConnection.setReadTimeout was previously found", e);
                } catch (InvocationTargetException e) {
                    Trace.fatal(CLASS, method, "URLConnection.setReadTimeout throwed an error", e);
                }
            }
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == 200) {
                in = httpConnection.getInputStream();
            } else {
                in = httpConnection.getErrorStream();
                final String errorText = IoUtility.loadStreamWithoutException(in, 1000);
                throw new IOException("Response code from HTTP server was " + responseCode
                        + (errorText.length() > 0 ? "\nResponse  text from HTTP server was:\n" + errorText
                                : ""));
            }
        } else {
            Trace.paramInfo(CLASS, method, "connection.getClass", connection.getClass().toString());
            in = connection.getInputStream();
        }

        if (!url.equals(connection.getURL().toString())) {
            throw new FileNotFoundException(
                    "\"" + url + "\" was substituted by " + "\"" + connection.getURL() + "\" from server");
        }
        final double maximum = connection.getContentLength();
        IoUtility.createNecessaryDirectories(f);
        out = new FileOutputStream(f);
        final byte[] buffer = new byte[4096];
        int bytesRead; // bytes read during one buffer read
        int position = 0; // current reading position within the whole document
        // continue writing
        while ((bytesRead = in.read(buffer)) != -1) {
            position += bytesRead;
            out.write(buffer, 0, bytesRead);
            if (maximum > 0) {
                double completeness = position / maximum;
                if (completeness < 0) {
                    completeness = 0;
                }
                if (completeness > 100) {
                    completeness = 1;
                }
                listener.loadingCompletenessChanged(completeness);
            }
        }
        listener.loadingCompletenessChanged(1);
    } finally {
        IoUtility.close(out);
        out = null;
        IoUtility.close(in);
        in = null;
        Trace.end(CLASS, method);
    }
}

From source file:osu.beatmapdownloader.JFrame.java

public boolean downloadSong(String idMap, int prio, String Directory) {
       String fileName = null;//ww  w .java 2  s  .  c  o  m
       try {
           String url = "";
           if (model.get(prio).toString().contains("Blood")) {
               url = "http://bloodcat.com/osu/d/" + idMap;
               L_server.setText("Bloodcat Server");
           } else {
               if (C_OsuServer.isSelected() && model.get(0).toString().contains("Osu")) {
                   if (C_noVideo.isSelected())
                       url = "http://osu.ppy.sh/d/" + idMap + "n";
                   else
                       url = "http://osu.ppy.sh/d/" + idMap;
                   L_server.setText("Osu! Server");
               }
           }
           long start = System.nanoTime();
           long totalRead = 0;
           final double NANOS_PER_SECOND = 1000000000.0;
           final double BYTES_PER_MIB = 1024 * 1024;
           URLConnection request = null;
           request = new URL(url).openConnection();
           request.setRequestProperty("Cookie", url.contains("ppy") ? Cookie : "");
           request.setRequestProperty("User-Agent",
                   "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
           InputStream in = request.getInputStream();
           String raw = request.getHeaderField("Content-Disposition");
           fileName = raw.split("=")[1].replaceAll("\"", "").replaceAll("; filename*", "");
           request.getContentLength();
           double size = request.getContentLength();
           File aux = File.createTempFile(fileName.replaceAll("\\*", "").replaceAll(";", ""), ".osz");
           L_FileName.setText(fileName.replaceAll("\\*", "").replaceAll(";", ""));
           FileOutputStream out = new FileOutputStream(aux);
           byte[] buffer = new byte[1024];
           int len = in.read(buffer);
           L_totalSize.setText((String) new DecimalFormat("#.##").format(size * 0.000001) + " Mb");
           int bytes = 0;
           Pro_ProgressBar.setMinimum(0);
           Pro_ProgressBar.setMaximum((int) (size / 1000));
           long acu = 0;
           while (len != -1) {
               bytes++;
               out.write(buffer, 0, len);
               len = in.read(buffer);
               if (len == 1024) {
                   acu += len;
                   BigDecimal a = new BigDecimal(acu * 0.000001);
                   BigDecimal roundOff = a.setScale(2, BigDecimal.ROUND_HALF_EVEN);
                   L_fileValue.setText(roundOff + "");
               }
               Pro_ProgressBar.setValue(bytes);
               totalRead += len;
               BigDecimal a = new BigDecimal(
                       ((NANOS_PER_SECOND / BYTES_PER_MIB * totalRead / (System.nanoTime() - start + 1)) * 1000));
               BigDecimal speed = a.setScale(2, BigDecimal.ROUND_HALF_EVEN);
               //String speed = new DecimalFormat("#.##").format(((NANOS_PER_SECOND / BYTES_PER_MIB * totalRead / (System.nanoTime() - start + 1)) * 1000));
               L_Second.setText(speed + "");
               BigDecimal b = new BigDecimal((((size * 0.000001) - (acu * 0.000001)) * 0.1)
                       / (((NANOS_PER_SECOND / BYTES_PER_MIB * totalRead / (System.nanoTime() - start + 1))
                               * 1000))
                       * 10000);
               BigDecimal speed_total = b.setScale(2, BigDecimal.ROUND_HALF_EVEN);
               L_seconds.setText(speed_total + "");
               if (Thread.interrupted()) {
                   in.close();
                   out.close();
                   aux.deleteOnExit();
                   throw new InterruptedException();
               }
           }
           in.close();
           out.close();
           FileUtils.copyFile(aux,
                   new File(Directory + File.separator + fileName.replaceAll("\\*", "").replaceAll(";", "")));
           aux.deleteOnExit();
           return true;
       } catch (Exception e) {
           errorFatal("DOWNLOADING");
           errorFatal(e.toString());
           errorFatal("--------------------------------------");
           prio++;
           errorConection++;
           L_Trying.setText(errorConection + "");
           int limitTry = 3;
           if (errorConection >= limitTry) {
               Errors++;
               L_Errors.setText(Errors + "");
               errorDownload("-The connection to this Beatmap was failed, '" + errorConection
                       + "' times, it was skipped.");
               errorDownload("--The filename is '" + fileName + "', with the id '" + idMap + "'. ");
               errorDownload("----------------------");
           } else {
               if (model.getSize() == prio)
                   prio = 0;
               downloadSong(idMap, prio, Directory);
           }
           return false;
       }
   }

From source file:JNLPAppletLauncher.java

private int copyURLToFile(URLConnection inConnection, File outFile) throws IOException {

    int totalNumBytes = 0;
    InputStream in = new BufferedInputStream(inConnection.getInputStream());
    try {/*from   w ww. j  a v a  2s.  c  o m*/
        OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
        try {
            totalNumBytes = copyStream(in, out, inConnection.getContentLength());
        } finally {
            out.close();
        }
    } finally {
        in.close();
    }

    return totalNumBytes;
}

From source file:focusedCrawler.util.parser.PaginaURL.java

public PaginaURL(URL url, URLConnection conexao, int max, StopList sl) throws IOException {
    this(url, conexao.getDate(), conexao.getLastModified(), conexao.getContentLength(),
            conexao.getInputStream(), max, sl);

    //        System.out.println("CONEXAO: RESPONSE CODE = " + ((HttpURLConnection) conexao).getResponseCode());

    URL url_final = ((HttpURLConnection) conexao).getURL();

    //        System.out.println("CONEXAO: GET URL       = " + url_final);

    //        if (!url_final.equals(url)) {
    //            System.out.println("A URL '" + url + "' foi redirecionada para '" + url_final + "'");
    //        } else {
    //            System.out.println("URL OK");
    //        }/*from  w ww .  jav  a  2s. com*/

    ((HttpURLConnection) conexao).disconnect();
}

From source file:com.blackducksoftware.integration.hub.cli.CLIDownloadService.java

public void customInstall(HubProxyInfo hubProxyInfo, CLILocation cliLocation,
        CIEnvironmentVariables ciEnvironmentVariables, final URL archive, String hubVersion,
        final String localHostName) throws IOException, InterruptedException, HubIntegrationException,
        IllegalArgumentException, EncryptionException {
    boolean cliMismatch = true;
    try {//from ww w  .ja  v a2s.c o  m
        final File hubVersionFile = cliLocation.createHubVersionFile();
        if (hubVersionFile.exists()) {
            final String storedHubVersion = IOUtils.toString(new FileReader(hubVersionFile));
            if (hubVersion.equals(storedHubVersion)) {
                cliMismatch = false;
            } else {
                hubVersionFile.delete();
                hubVersionFile.createNewFile();
            }
        }
        final File cliInstallDirectory = cliLocation.getCLIInstallDir();
        if (!cliInstallDirectory.exists()) {
            cliMismatch = true;
        }

        if (cliMismatch) {
            logger.debug("Attempting to download the Hub CLI.");
            final FileWriter writer = new FileWriter(hubVersionFile);
            writer.write(hubVersion);
            writer.close();
            hubVersionFile.setLastModified(0L);
        }
        final long cliTimestamp = hubVersionFile.lastModified();

        URLConnection connection = null;
        try {
            Proxy proxy = null;
            if (hubProxyInfo != null) {
                String proxyHost = hubProxyInfo.getHost();
                int proxyPort = hubProxyInfo.getPort();
                String proxyUsername = hubProxyInfo.getUsername();
                String proxyPassword = hubProxyInfo.getDecryptedPassword();

                if (StringUtils.isNotBlank(proxyHost) && proxyPort > 0) {
                    proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
                }
                if (proxy != null) {
                    if (StringUtils.isNotBlank(proxyUsername) && StringUtils.isNotBlank(proxyPassword)) {
                        AuthenticatorUtil.setAuthenticator(proxyUsername, proxyPassword);
                    } else {
                        AuthenticatorUtil.resetAuthenticator();
                    }
                }
            }
            if (proxy != null) {
                connection = archive.openConnection(proxy);
            } else {
                connection = archive.openConnection();
            }
            connection.setIfModifiedSince(cliTimestamp);
            connection.connect();
        } catch (final IOException ioe) {
            logger.error("Skipping installation of " + archive + " to " + cliLocation.getCanonicalPath() + ": "
                    + ioe.toString());
            return;
        }

        if (connection instanceof HttpURLConnection
                && ((HttpURLConnection) connection).getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
            // CLI has not been modified
            return;
        }

        final long sourceTimestamp = connection.getLastModified();

        if (cliInstallDirectory.exists() && cliInstallDirectory.listFiles().length > 0) {
            if (!cliMismatch && sourceTimestamp == cliTimestamp) {
                logger.debug("The current Hub CLI is up to date.");
                return;
            }
            for (final File file : cliInstallDirectory.listFiles()) {
                FileUtils.deleteDirectory(file);
            }
        } else {
            cliInstallDirectory.mkdir();
        }
        logger.debug("Updating the Hub CLI.");
        hubVersionFile.setLastModified(sourceTimestamp);

        logger.info("Unpacking " + archive.toString() + " to " + cliInstallDirectory.getCanonicalPath() + " on "
                + localHostName);

        final CountingInputStream cis = new CountingInputStream(connection.getInputStream());
        try {
            unzip(cliInstallDirectory, cis, logger);
            updateJreSecurity(logger, cliLocation, ciEnvironmentVariables);
        } catch (final IOException e) {
            throw new IOException(String.format("Failed to unpack %s (%d bytes read of total %d)", archive,
                    cis.getByteCount(), connection.getContentLength()), e);
        }
    } catch (final IOException e) {
        throw new IOException("Failed to install " + archive + " to " + cliLocation.getCanonicalPath(), e);
    }
}

From source file:hudson.FilePath.java

/**
 * Given a tgz/zip file, extracts it to the given target directory, if necessary.
 *
 * <p>//from   w ww  .  j  av a2s .c  om
 * This method is a convenience method designed for installing a binary package to a location
 * that supports upgrade and downgrade. Specifically,
 *
 * <ul>
 * <li>If the target directory doesn't exist {@linkplain #mkdirs() it'll be created}.
 * <li>The timestamp of the .tgz file is left in the installation directory upon extraction.
 * <li>If the timestamp left in the directory doesn't match with the timestamp of the current archive file,
 *     the directory contents will be discarded and the archive file will be re-extracted.
 * <li>If the connection is refused but the target directory already exists, it is left alone.
 * </ul>
 *
 * @param archive
 *      The resource that represents the tgz/zip file. This URL must support the "Last-Modified" header.
 *      (Most common usage is to get this from {@link ClassLoader#getResource(String)})
 * @param listener
 *      If non-null, a message will be printed to this listener once this method decides to
 *      extract an archive.
 * @return
 *      true if the archive was extracted. false if the extraction was skipped because the target directory
 *      was considered up to date.
 * @since 1.299
 */
public boolean installIfNecessaryFrom(URL archive, TaskListener listener, String message)
        throws IOException, InterruptedException {
    URLConnection con;
    try {
        con = archive.openConnection();
        con.connect();
    } catch (IOException x) {
        if (this.exists()) {
            // Cannot connect now, so assume whatever was last unpacked is still OK.
            if (listener != null) {
                listener.getLogger()
                        .println("Skipping installation of " + archive + " to " + remote + ": " + x);
            }
            return false;
        } else {
            throw x;
        }
    }
    long sourceTimestamp = con.getLastModified();
    FilePath timestamp = this.child(".timestamp");

    if (this.exists()) {
        if (timestamp.exists() && sourceTimestamp == timestamp.lastModified())
            return false; // already up to date
        this.deleteContents();
    }

    if (listener != null)
        listener.getLogger().println(message);

    CountingInputStream cis = new CountingInputStream(con.getInputStream());
    try {
        if (archive.toExternalForm().endsWith(".zip"))
            unzipFrom(cis);
        else
            untarFrom(cis, GZIP);
    } catch (IOException e) {
        throw new IOException2(String.format("Failed to unpack %s (%d bytes read of total %d)", archive,
                cis.getByteCount(), con.getContentLength()), e);
    }
    timestamp.touch(sourceTimestamp);
    return true;
}

From source file:tufts.vue.URLResource.java

private Properties scrapeHTMLmetaData(URLConnection connection, int maxSearchBytes) throws java.io.IOException {
    Properties metaData = new Properties();

    InputStream byteStream = connection.getInputStream();

    if (DEBUG.DND && DEBUG.META) {
        System.err.println("Getting headers from " + connection);
        System.err.println("Headers: " + connection.getHeaderFields());
    }//www.  j av a 2s . c  om

    // note: be sure to call getContentType and don't rely on getting it from the HeaderFields map,
    // as sometimes it's set by the OS for a file:/// URL when there are no header fields (no http server)
    // (actually, this is set by java via a mime type table based on file extension, or a guess based on the stream)
    if (DEBUG.DND)
        System.err.println("*** getting contentType & encoding...");
    final String contentType = connection.getContentType();
    final String contentEncoding = connection.getContentEncoding();
    final int contentLength = connection.getContentLength();

    if (DEBUG.DND)
        System.err.println("*** contentType [" + contentType + "]");
    if (DEBUG.DND)
        System.err.println("*** contentEncoding [" + contentEncoding + "]");
    if (DEBUG.DND)
        System.err.println("*** contentLength [" + contentLength + "]");

    setProperty("url.contentType", contentType);
    setProperty("url.contentEncoding", contentEncoding);
    if (contentLength >= 0)
        setProperty("url.contentLength", contentLength);

    //if (contentType.toLowerCase().startsWith("text/html") == false) {
    if (!isHTML()) { // we only currently handle HTML
        if (DEBUG.Enabled)
            System.err.println("*** contentType [" + contentType + "] not HTML; skipping title extraction");
        return metaData;
    }

    if (DEBUG.DND)
        System.err.println("*** scanning for HTML meta-data...");

    try {
        final BufferedInputStream bufStream = new BufferedInputStream(byteStream, maxSearchBytes);
        bufStream.mark(maxSearchBytes);

        final byte[] byteBuffer = new byte[maxSearchBytes];
        int bytesRead = 0;
        int len = 0;
        // BufferedInputStream still won't read thru a block, so we need to allow
        // a few reads here to get thru a couple of blocks, so we can get up to
        // our maxbytes (e.g., a common return chunk count is 1448 bytes, presumably related to the MTU)
        do {
            int max = maxSearchBytes - bytesRead;
            len = bufStream.read(byteBuffer, bytesRead, max);
            System.out.println("*** read " + len);
            if (len > 0)
                bytesRead += len;
            else if (len < 0)
                break;
        } while (len > 0 && bytesRead < maxSearchBytes);
        if (DEBUG.DND)
            System.out.println("*** Got total chars: " + bytesRead);
        String html = new String(byteBuffer, 0, bytesRead);
        if (DEBUG.DND && DEBUG.META)
            System.out.println("*** HTML-STRING[" + html + "]");

        // first, look for a content encoding, so we can search for and get the title
        // on a properly encoded character stream

        String charset = null;

        Matcher cm = Content_Charset_Regex.matcher(html);
        if (cm.lookingAt()) {
            charset = cm.group(1);
            if (DEBUG.DND)
                System.err.println("*** found HTML specified charset [" + charset + "]");
            setProperty("charset", charset);
        }

        if (charset == null && contentEncoding != null) {
            if (DEBUG.DND || true)
                System.err.println("*** no charset found: using contentEncoding charset " + contentEncoding);
            charset = contentEncoding;
        }

        final String decodedHTML;

        if (charset != null) {
            bufStream.reset();
            InputStreamReader decodedStream = new InputStreamReader(bufStream, charset);
            //InputStreamReader decodedStream = new InputStreamReader(new ByteArrayInputStream(byteBuffer), charset);
            if (true || DEBUG.DND)
                System.out.println("*** decoding bytes into characters with official encoding "
                        + decodedStream.getEncoding());
            setProperty("contentEncoding", decodedStream.getEncoding());
            char[] decoded = new char[bytesRead];
            int decodedChars = decodedStream.read(decoded);
            decodedStream.close();
            if (true || DEBUG.DND)
                System.err.println("*** " + decodedChars + " characters decoded using " + charset);
            decodedHTML = new String(decoded, 0, decodedChars);
        } else
            decodedHTML = html; // we'll just have to go with the default platform charset...

        // these needed to be left open till the decodedStream was done, which
        // although it should never need to read beyond what's already buffered,
        // some internal java code has checks that make sure the underlying stream
        // isn't closed, even it it isn't used.
        byteStream.close();
        bufStream.close();

        Matcher m = HTML_Title_Regex.matcher(decodedHTML);
        if (m.lookingAt()) {
            String title = m.group(1);
            if (true || DEBUG.DND)
                System.err.println("*** found title [" + title + "]");
            metaData.put("title", title.trim());
        }

    } catch (Throwable e) {
        System.err.println("scrapeHTMLmetaData: " + e);
        if (DEBUG.DND)
            e.printStackTrace();
    }

    if (DEBUG.DND || DEBUG.Enabled)
        System.err.println("*** scrapeHTMLmetaData returning [" + metaData + "]");
    return metaData;
}

From source file:com.jp.miaulavirtual.DisplayMessageActivity.java

public int downloadFile(String request) {
    URL url2;//ww  w .j a v a2  s  .c o  m
    URLConnection conn;
    int lastSlash;
    Long fileSize = null;
    BufferedInputStream inStream;
    BufferedOutputStream outStream;
    FileOutputStream fileStream;
    String cookies = cookieFormat(scookie); // format cookie for URL setRequestProperty
    final int BUFFER_SIZE = 23 * 1024;
    int id = 1;

    File file = null;

    Log.d("Document", "2 respueesta");
    try {
        // Just resources
        lastSlash = url.toString().lastIndexOf('/');

        // Directory creation
        String root = Environment.getExternalStorageDirectory().toString();
        Boolean isSDPresent = android.os.Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED);
        // check if is there external storage
        if (!isSDPresent) {
            task_status = false;
            id = 9;
        } else {
            String folder;
            if (comunidades) {
                folder = onData.get(2)[1];
            } else {
                folder = onData.get(1)[1];
            }
            folder = folder.replaceAll(
                    "\\d{4}-\\d{4}\\s|\\d{4}-\\d{2}\\s|Documentos\\sde\\s?|Gr\\..+?\\s|\\(.+?\\)", "");
            folder = folder.toString().trim();
            Log.d("Folder", folder);
            File myDir = new File(root + "/Android/data/com.jp.miaulavirtual/files/" + folder);
            myDir.mkdirs();

            // Document creation
            String name = url.toString().substring(lastSlash + 1);
            file = new File(myDir, name);
            Log.d("Document", name);
            fileSize = (long) file.length();

            // Check if we have already downloaded the whole file
            if (file.exists()) {
                dialog.setProgress(100); // full progress if file already donwloaded
            } else {
                // Start the connection with COOKIES (we already verified that the cookies aren't expired and we can use them)
                url2 = new URL(request);
                conn = url2.openConnection();
                conn.setUseCaches(false);
                conn.setRequestProperty("Cookie", cookies);
                conn.setConnectTimeout(10 * 1000);
                conn.setReadTimeout(20 * 1000);
                fileSize = (long) conn.getContentLength();

                // Check if we have necesary space
                if (fileSize >= myDir.getUsableSpace()) {
                    task_status = false;
                    id = 2;
                } else {

                    // Start downloading
                    inStream = new BufferedInputStream(conn.getInputStream());
                    fileStream = new FileOutputStream(file);
                    outStream = new BufferedOutputStream(fileStream, BUFFER_SIZE);
                    byte[] data = new byte[BUFFER_SIZE];
                    int bytesRead = 0;
                    int setMax = (conn.getContentLength() / 1024);
                    dialog.setMax(setMax);

                    while (task_status && (bytesRead = inStream.read(data, 0, data.length)) >= 0) {
                        outStream.write(data, 0, bytesRead);
                        // update progress bar
                        dialog.incrementProgressBy((int) (bytesRead / 1024));
                    }

                    // Close stream
                    outStream.close();
                    fileStream.close();
                    inStream.close();

                    // Delete file if Cancel button
                    if (!task_status) {
                        file.delete();
                        if (myDir.listFiles().length <= 0)
                            myDir.delete();
                        id = 0;
                    }
                }
            }
            Log.d("Status", String.valueOf(task_status));
            // Open file
            if (task_status) {
                Log.d("Type", "Hola2");
                dialog.dismiss();
                Intent intent = new Intent();
                intent.setAction(android.content.Intent.ACTION_VIEW);

                MimeTypeMap mime = MimeTypeMap.getSingleton();
                // Get extension file
                String file_s = file.toString();
                String extension = "";

                int i = file_s.lastIndexOf('.');
                int p = Math.max(file_s.lastIndexOf('/'), file_s.lastIndexOf('\\'));

                if (i > p) {
                    extension = file_s.substring(i + 1);
                }

                // Get extension reference
                String doc_type = mime.getMimeTypeFromExtension(extension);

                Log.d("Type", extension);

                intent.setDataAndType(Uri.fromFile(file), doc_type);
                startActivity(intent);
            }
        }
    } catch (MalformedURLException e) // Invalid URL
    {
        task_status = false;
        if (file.exists()) {
            file.delete();
        }
        id = 3;
    } catch (FileNotFoundException e) // FIle not found
    {
        task_status = false;
        if (file.exists()) {
            file.delete();
        }
        id = 4;
    } catch (SocketTimeoutException e) // time out
    {
        Log.d("Timeout", "Timeout");
        task_status = false;
        if (file.exists()) {
            file.delete();
        }
        id = 7;
    } catch (Exception e) // General error
    {
        task_status = false;
        if (file.exists()) {
            file.delete();
        }
        id = 8;
    }
    Log.d("Type", String.valueOf(id));
    Log.d("StartOk3", "Como he llegado hasta aqu?");
    // notify completion
    Log.d("ID", String.valueOf(id));
    return id;

}

From source file:JNLPAppletLauncher.java

private void validateCache(URLConnection conn, File nativeFile, File indexFile) throws IOException {

    // Lock the cache directory
    final String lckFileName = "cache.lck";
    File lckFile = new File(cacheDir, lckFileName);
    lckFile.createNewFile();//from  w ww.ja v  a  2s.  c o  m
    final FileOutputStream lckOut = new FileOutputStream(lckFile);
    final FileChannel lckChannel = lckOut.getChannel();
    final FileLock lckLock = lckChannel.lock();

    try {
        // Check to see whether the cached jar file exists and is valid
        boolean valid = false;
        long cachedTimeStamp = readTimeStamp(indexFile);
        long urlTimeStamp = conn.getLastModified();

        if (nativeFile.exists() && urlTimeStamp > 0 && urlTimeStamp == readTimeStamp(indexFile)) {

            valid = true;
        }

        // Validate the cache, download the jar if needed
        if (!valid) {
            if (VERBOSE) {
                System.err.println("processNativeJar: downloading " + nativeFile.getAbsolutePath());
            }
            indexFile.delete();
            nativeFile.delete();

            // Copy from URL to File
            int len = conn.getContentLength();
            if (VERBOSE) {
                System.err.println("Content length = " + len + " bytes");
            }

            int totalNumBytes = copyURLToFile(conn, nativeFile);
            if (DEBUG) {
                System.err.println("processNativeJar: " + conn.getURL().toString() + " --> "
                        + nativeFile.getAbsolutePath() + " : " + totalNumBytes + " bytes written");
            }

            // Write timestamp to index file.
            writeTimeStamp(indexFile, urlTimeStamp);

        } else {
            if (DEBUG) {
                System.err
                        .println("processNativeJar: using previously cached: " + nativeFile.getAbsolutePath());
            }
        }
    } finally {
        // Unlock the cache directory
        lckLock.release();
    }
}

From source file:org.bimserver.webservices.impl.ServiceImpl.java

@Override
public Long checkinFromUrl(Long poid, String comment, Long deserializerOid, String fileName, String urlString,
        Boolean merge, Boolean sync) throws ServerException, UserException {
    requireAuthenticationAndRunningServer();
    final DatabaseSession session = getBimServer().getDatabase().createSession();
    String username = "Unknown";
    String userUsername = "Unknown";
    try {/*from  ww  w .  j  av a 2  s  .c  o m*/
        User user = (User) session.get(StorePackage.eINSTANCE.getUser(), getAuthorization().getUoid(),
                OldQuery.getDefault());
        username = user.getName();
        userUsername = user.getUsername();
        Path homeDirIncoming = getBimServer().getHomeDir().resolve("incoming");
        if (!Files.isDirectory(homeDirIncoming)) {
            Files.createDirectory(homeDirIncoming);
        }
        Path userDirIncoming = homeDirIncoming.resolve(userUsername);
        if (!Files.exists(userDirIncoming)) {
            Files.createDirectory(userDirIncoming);
        }

        Project project = session.get(poid, OldQuery.getDefault());
        if (project == null) {
            throw new UserException("No project found with poid " + poid);
        }

        Long topicId = initiateCheckin(poid, deserializerOid);

        URL url = new URL(urlString);
        URLConnection openConnection = url.openConnection();
        InputStream input = openConnection.getInputStream();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        if (fileName == null) {
            if (urlString.contains("/")) {
                fileName = urlString.substring(urlString.lastIndexOf("/") + 1);
            } else {
                fileName = urlString;
            }
            if (fileName.contains("?")) {
                fileName = fileName.substring(0, fileName.indexOf("?"));
            }
            fileName = URLDecoder.decode(fileName, Charsets.UTF_8.name());
        } else {
            fileName = dateFormat.format(new Date()) + "-" + fileName;
        }
        Path file = userDirIncoming.resolve(fileName);

        if (fileName.contains(" ")) {
            fileName = fileName.replace(" ", "_");
        }

        return checkinInternal(topicId, poid, comment, deserializerOid,
                (long) openConnection.getContentLength(), fileName, input, merge, sync, session, username,
                userUsername, project, file);

        //         DeserializerPluginConfiguration deserializerPluginConfiguration = session.get(StorePackage.eINSTANCE.getDeserializerPluginConfiguration(), deserializerOid, OldQuery.getDefault());
        //         if (deserializerPluginConfiguration == null) {
        //            throw new UserException("Deserializer with oid " + deserializerOid + " not found");
        //         }
        //         OutputStream outputStream = Files.newOutputStream(file);
        //         InputStream inputStream = new MultiplexingInputStream(input, outputStream);
        //         DeserializerPlugin deserializerPlugin = (DeserializerPlugin) getBimServer().getPluginManager().getPlugin(deserializerPluginConfiguration.getPluginDescriptor().getPluginClassName(), true);
        //         ObjectType settings = deserializerPluginConfiguration.getSettings();
        //
        //         Deserializer deserializer = deserializerPlugin.createDeserializer(new PluginConfiguration(settings));
        //         deserializer.init(getBimServer().getDatabase().getMetaDataManager().getPackageMetaData("ifc2x3tc1"));
        //
        //         IfcModelInterface model = deserializer.read(inputStream, fileName, 0, null);
        //         
        //         CheckinDatabaseAction checkinDatabaseAction = new CheckinDatabaseAction(getBimServer(), null, getInternalAccessMethod(), poid, getAuthorization(), model, comment, fileName, merge);
        //         LongCheckinAction longAction = new LongCheckinAction(-1L, getBimServer(), username, userUsername, getAuthorization(), checkinDatabaseAction);
        //         getBimServer().getLongActionManager().start(longAction);
        //         if (sync) {
        //            longAction.waitForCompletion();
        //         }
        //         return longAction.getProgressTopic().getKey().getId();
    } catch (UserException e) {
        throw e;
    } catch (Throwable e) {
        LOGGER.error("", e);
        throw new ServerException(e);
    } finally {
        session.close();
    }
}