Java Code Examples for java.net.URLConnection#getContentLengthLong()

The following examples show how to use java.net.URLConnection#getContentLengthLong() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@Override
public long contentLength() throws IOException {
	URL url = getURL();
	if (ResourceUtils.isFileURL(url)) {
		// Proceed with file system resolution
		File file = getFile();
		long length = file.length();
		if (length == 0L && !file.exists()) {
			throw new FileNotFoundException(getDescription() +
					" cannot be resolved in the file system for checking its content length");
		}
		return length;
	}
	else {
		// Try a URL connection content-length header
		URLConnection con = url.openConnection();
		customizeConnection(con);
		return con.getContentLengthLong();
	}
}
 
Example 2
Source Project: djl   File: DownloadUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Downloads a file from specified url.
 *
 * @param url the url to download
 * @param output the output location
 * @param progress the progress tracker to show download progress
 * @throws IOException when IO operation fails in downloading
 */
public static void download(URL url, Path output, Progress progress) throws IOException {
    if (Files.exists(output)) {
        return;
    }
    Path dir = output.toAbsolutePath().getParent();
    if (dir != null) {
        Files.createDirectories(dir);
    }
    URLConnection conn = url.openConnection();
    if (progress != null) {
        long contentLength = conn.getContentLengthLong();
        if (contentLength > 0) {
            progress.reset("Downloading", contentLength, output.toFile().getName());
        }
    }
    try (InputStream is = conn.getInputStream()) {
        ProgressInputStream pis = new ProgressInputStream(is, progress);
        String fileName = url.getFile();
        if (fileName.endsWith(".gz")) {
            Files.copy(new GZIPInputStream(pis), output);
        } else {
            Files.copy(pis, output);
        }
    }
}
 
Example 3
Resource download(URL url, ProgressListener progressListener) throws IOException {
	URLConnection connection = connect(url);
	try (InputStream is = connection.getInputStream()) {
		long totalSize = connection.getContentLengthLong();
		Path tempFile = createTempFile(url);
		progressListener.start();
		try (OutputStream os = Files.newOutputStream(tempFile)) {
			byte[] buffer = new byte[8192];
			long readBytes = 0;
			int read;
			while ((read = is.read(buffer)) != -1) {
				os.write(buffer, 0, read);
				readBytes += read;
				if (totalSize > 0 && readBytes > 0) {
					progressListener.update(readBytes, totalSize);
				}
			}
		}
		if (Thread.interrupted()) {
			throw new ClosedByInterruptException();
		}
		progressListener.finish();
		return new FileSystemResource(tempFile);
	}
}
 
Example 4
public void download() throws IOException {
    FileOutputStream fos = new FileOutputStream(destFilename);
    URLConnection connection = new URL(url).openConnection();
    long fileSize = connection.getContentLengthLong();
    InputStream inputStream = connection.getInputStream();
    byte[] buffer = new byte[10 * 1024 * 1024];
    int numberOfBytesRead;
    long totalNumberOfBytesRead = 0;
    ConsoleProgressBar bar = new ConsoleProgressBar();
    while ((numberOfBytesRead = inputStream.read(buffer)) != -1) {
        fos.write(buffer, 0, numberOfBytesRead);
        totalNumberOfBytesRead += numberOfBytesRead;
        bar.show(totalNumberOfBytesRead * 100 / fileSize);
    }
    fos.close();
    inputStream.close();
}
 
Example 5
Source Project: FXTutorials   File: DownloaderApp.java    License: MIT License 6 votes vote down vote up
@Override
protected Void call() throws Exception {
    String ext = url.substring(url.lastIndexOf("."), url.length());
    URLConnection connection = new URL(url).openConnection();
    long fileLength = connection.getContentLengthLong();

    try (InputStream is = connection.getInputStream();
            OutputStream os = Files.newOutputStream(Paths.get("downloadedfile" + ext))) {

        long nread = 0L;
        byte[] buf = new byte[8192];
        int n;
        while ((n = is.read(buf)) > 0) {
            os.write(buf, 0, n);
            nread += n;
            updateProgress(nread, fileLength);
        }
    }

    return null;
}
 
Example 6
@Override
public long size()
{
  ClassLoader loader = _loader;
  
  URL url = loader.getResource(_path);
  
  if (url == null) {
    return -1;
  }
  else {
    try {
      URLConnection conn = url.openConnection();
      
      return conn.getContentLengthLong();
      /*
      Path path = Paths.get(url.toURI());
    
      return Files.size(path);
      */
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
}
 
Example 7
@Override
public long getFileSize(String path, boolean virtual) throws IOException {
    long fileSize = -1;
    try {
        URLConnection urlConnection = getURLConnection(path, virtual);
        fileSize = urlConnection.getContentLengthLong();
    } catch (IOException e) {
        // Ignore this. It will always fail for non-file based includes
    }
    return fileSize;
}
 
Example 8
@Override
public boolean isReadable() {
	try {
		URL url = getURL();
		if (ResourceUtils.isFileURL(url)) {
			// Proceed with file system resolution
			File file = getFile();
			return (file.canRead() && !file.isDirectory());
		}
		else {
			// Try InputStream resolution for jar resources
			URLConnection con = url.openConnection();
			customizeConnection(con);
			if (con instanceof HttpURLConnection) {
				HttpURLConnection httpCon = (HttpURLConnection) con;
				int code = httpCon.getResponseCode();
				if (code != HttpURLConnection.HTTP_OK) {
					httpCon.disconnect();
					return false;
				}
			}
			long contentLength = con.getContentLengthLong();
			if (contentLength > 0) {
				return true;
			}
			else if (contentLength == 0) {
				// Empty file or directory -> not considered readable...
				return false;
			}
			else {
				// Fall back to stream existence: can we open the stream?
				getInputStream().close();
				return true;
			}
		}
	}
	catch (IOException ex) {
		return false;
	}
}
 
Example 9
@Override
public boolean isReadable() {
	try {
		URL url = getURL();
		if (ResourceUtils.isFileURL(url)) {
			// Proceed with file system resolution
			File file = getFile();
			return (file.canRead() && !file.isDirectory());
		}
		else {
			// Try InputStream resolution for jar resources
			URLConnection con = url.openConnection();
			customizeConnection(con);
			if (con instanceof HttpURLConnection) {
				HttpURLConnection httpCon = (HttpURLConnection) con;
				int code = httpCon.getResponseCode();
				if (code != HttpURLConnection.HTTP_OK) {
					httpCon.disconnect();
					return false;
				}
			}
			long contentLength = con.getContentLengthLong();
			if (contentLength > 0) {
				return true;
			}
			else if (contentLength == 0) {
				// Empty file or directory -> not considered readable...
				return false;
			}
			else {
				// Fall back to stream existence: can we open the stream?
				getInputStream().close();
				return true;
			}
		}
	}
	catch (IOException ex) {
		return false;
	}
}
 
Example 10
Source Project: chipster   File: UrlTransferUtil.java    License: MIT License 5 votes vote down vote up
public static Long getContentLength(URL url, boolean isChipsterServer) throws IOException {
	URLConnection connection = null;
	try {
		connection = url.openConnection();
		if (isChipsterServer) {				
			KeyAndTrustManager.configureForChipsterCertificate(connection);
		} else {
			KeyAndTrustManager.configureForCACertificates(connection);
		}
		
		if (connection instanceof HttpURLConnection) {
			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			// check the response code first because the content length may be the length of the error message
			if (httpConnection.getResponseCode() >= 200 && httpConnection.getResponseCode() < 300) {
				long contentLength = connection.getContentLengthLong();

				if (contentLength >= 0) {
					return contentLength;
				} else {
					throw new IOException("content length not available: " + connection.getContent());
				}					
			} else {
				throw new IOException("content length not available: " + httpConnection.getResponseCode() + " " + httpConnection.getResponseMessage());
			}
		} else {
			throw new IOException("the remote content location isn't using http or https protocol: " + url);
		}
	} finally {
		IOUtils.disconnectIfPossible(connection);
	}
}
 
Example 11
@Override
public String getETag() throws IOException {
    URLConnection openConnection = url.openConnection();
    openConnection.connect();
    String eTag = openConnection.getHeaderField("ETag");
    if (eTag != null) {
        return eTag;
    }
    long lastModified = openConnection.getLastModified();
    long length = openConnection.getContentLengthLong();
    eTag = getSimpleWeakETag(url.toString(), length, lastModified);
    return eTag == null ? null : eTag.substring(2);
}
 
Example 12
private static void downloadFromServer(@NotNull URL url, long maxFileSize, @NotNull OutputStream outputStream,
									   @NotNull Function<Double, Double> progressUpdate) throws IOException, NotEnoughFreeSpaceException {

	BufferedInputStream in = null;
	URLConnection urlConnection = null;
	double workDone = 0;

	try {
		urlConnection = url.openConnection();
		in = new BufferedInputStream(urlConnection.getInputStream());

		long downloadSize = urlConnection.getContentLengthLong();
		if (maxFileSize < downloadSize) {
			in.close();
			outputStream.close();
			urlConnection.getInputStream().close();
			throw new NotEnoughFreeSpaceException(ADCUpdater.bundle.getString("Updater.not_enough_free_space"));
		}

		final byte data[] = new byte[1024];
		int count;
		while ((count = in.read(data, 0, 1024)) != -1) {
			outputStream.write(data, 0, count);
			workDone += count;
			progressUpdate.apply(workDone / downloadSize);
		}
	} finally {
		if (in != null) {
			in.close();
		}
		if (urlConnection != null) {
			urlConnection.getInputStream().close();
		}
		outputStream.close();
	}
}
 
Example 13
Source Project: JglTF   File: IO.java    License: MIT License 5 votes vote down vote up
/**
 * Try to obtain the content length from the given URI. Returns -1
 * if the content length can not be determined.
 * 
 * @param uri The URI
 * @return The content length
 */
public static long getContentLength(URI uri)
{
    try
    {
        URLConnection connection = uri.toURL().openConnection();
        return connection.getContentLengthLong();
    }
    catch (IOException e)
    {
        return -1;
    }
}
 
Example 14
public UrlWebResource(final URL url, final String path, final String contentType, final boolean cacheable) {
  this.url = checkNotNull(url);
  this.path = checkNotNull(path);
  this.cacheable = cacheable;

  // open connection to get details about the resource
  try {
    final URLConnection connection = this.url.openConnection();
    try (final InputStream ignore = connection.getInputStream()) {
      if (Strings.isNullOrEmpty(contentType)) {
        this.contentType = connection.getContentType();
      }
      else {
        this.contentType = contentType;
      }

      // support for legacy int and modern long content-length
      long detectedSize = connection.getContentLengthLong();
      if (detectedSize == -1) {
        detectedSize = connection.getContentLength();
      }
      this.size = detectedSize;

      this.lastModified = connection.getLastModified();
    }
  }
  catch (IOException e) {
    throw new IllegalArgumentException("Resource inaccessible: " + url, e);
  }
}
 
Example 15
@Override
public boolean exists() {
	try {
		URL url = getURL();
		if (ResourceUtils.isFileURL(url)) {
			// Proceed with file system resolution
			return getFile().exists();
		}
		else {
			// Try a URL connection content-length header
			URLConnection con = url.openConnection();
			customizeConnection(con);
			HttpURLConnection httpCon =
					(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
			if (httpCon != null) {
				int code = httpCon.getResponseCode();
				if (code == HttpURLConnection.HTTP_OK) {
					return true;
				}
				else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
					return false;
				}
			}
			if (con.getContentLengthLong() > 0) {
				return true;
			}
			if (httpCon != null) {
				// No HTTP OK status, and no content-length header: give up
				httpCon.disconnect();
				return false;
			}
			else {
				// Fall back to stream existence: can we open the stream?
				getInputStream().close();
				return true;
			}
		}
	}
	catch (IOException ex) {
		return false;
	}
}
 
Example 16
@Override
public boolean exists() {
	try {
		URL url = getURL();
		if (ResourceUtils.isFileURL(url)) {
			// Proceed with file system resolution
			return getFile().exists();
		}
		else {
			// Try a URL connection content-length header
			URLConnection con = url.openConnection();
			customizeConnection(con);
			HttpURLConnection httpCon =
					(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
			if (httpCon != null) {
				int code = httpCon.getResponseCode();
				if (code == HttpURLConnection.HTTP_OK) {
					return true;
				}
				else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
					return false;
				}
			}
			if (con.getContentLengthLong() > 0) {
				return true;
			}
			if (httpCon != null) {
				// No HTTP OK status, and no content-length header: give up
				httpCon.disconnect();
				return false;
			}
			else {
				// Fall back to stream existence: can we open the stream?
				getInputStream().close();
				return true;
			}
		}
	}
	catch (IOException ex) {
		return false;
	}
}
 
Example 17
Source Project: opoopress   File: ProgressURLDownloader.java    License: Apache License 2.0 4 votes vote down vote up
private void downloadInternal(URL url, File destination) throws IOException {
    OutputStream out = null;
    URLConnection conn;
    InputStream in = null;
    try {
        //URL url = address.toURL();
        conn = url.openConnection();

        //user agent
        final String userAgentValue = calculateUserAgent();
        conn.setRequestProperty("User-Agent", userAgentValue);

        //do not set gzip header if download zip file
        if (useGzip) {
            conn.setRequestProperty("Accept-Encoding", "gzip");
        }

        if (!useCache) {
            conn.setRequestProperty("Pragma", "no-cache");
        }

        in = conn.getInputStream();
        out = new BufferedOutputStream(new FileOutputStream(destination));

        copy(in, out);

        if(checkContentLength) {
            long contentLength = conn.getContentLengthLong();
            if (contentLength > 0 && contentLength != destination.length()) {
                throw new IllegalArgumentException("File length mismatch. expected: "
                        + contentLength + ", actual: " + destination.length());
            }
        }

        if(keepLastModified) {
            long lastModified = conn.getLastModified();
            if (lastModified > 0) {
                destination.setLastModified(lastModified);
            }
        }
    } finally {
        logMessage("");
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}
 
Example 18
/** Download an archive, this will NOT overwrite a previously existing file. */
public void download() throws IOException, InterruptedException {
  if (!Files.exists(destinationFile.getParent())) {
    Files.createDirectories(destinationFile.getParent());
  }

  if (Files.exists(destinationFile)) {
    throw new FileAlreadyExistsException(destinationFile.toString());
  }
  URLConnection connection = address.openConnection();
  connection.setRequestProperty("User-Agent", userAgentString);

  try (InputStream in = connection.getInputStream()) {
    // note : contentLength can potentially be -1 if it is unknown.
    long contentLength = connection.getContentLengthLong();

    logger.info("Downloading " + address + " to " + destinationFile);

    try (BufferedOutputStream out =
        new BufferedOutputStream(
            Files.newOutputStream(destinationFile, StandardOpenOption.CREATE_NEW))) {

      progressListener.start(
          getDownloadStatus(contentLength, Locale.getDefault()), contentLength);

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

      while ((bytesRead = in.read(buffer)) != -1) {
        if (Thread.currentThread().isInterrupted()) {
          logger.warning("Download was interrupted\n");
          cleanUp();
          throw new InterruptedException("Download was interrupted");
        }

        out.write(buffer, 0, bytesRead);
        progressListener.update(bytesRead);
      }
    }
  }
  progressListener.done();
}
 
Example 19
@Override
public void runImport ( final ImportContext context, final String configuration ) throws Exception
{
    final Configuration cfg = this.gson.fromJson ( configuration, Configuration.class );

    logger.debug ( "Get URL: {}", cfg.getUrl () );

    final URL url = new URL ( cfg.getUrl () );

    final Path file = Files.createTempFile ( "import", null );

    final URLConnection con = url.openConnection ();

    con.setRequestProperty ( "User-Agent", VersionInformation.USER_AGENT );

    String name;

    final Context job = context.getJobContext ();

    try ( final InputStream in = con.getInputStream ();
          OutputStream out = new BufferedOutputStream ( new FileOutputStream ( file.toFile () ) ) )
    {
        final long length = con.getContentLengthLong ();

        if ( length > 0 )
        {
            job.beginWork ( String.format ( "Downloading %s", Strings.bytes ( length ) ), length );
        }

        // manual copy
        final byte[] buffer = new byte[COPY_BUFFER_SIZE];
        int rc;
        while ( ( rc = in.read ( buffer ) ) > 0 )
        {
            out.write ( buffer, 0, rc );
            job.worked ( rc );
        }

        job.complete ();

        // get the name inside here, since this will properly clean up if something fails

        name = makeName ( cfg, url, con );
        if ( name == null )
        {
            throw new IllegalStateException ( String.format ( "Unable to determine name for %s", cfg.getUrl () ) );
        }
    }
    catch ( final Exception e )
    {
        logger.debug ( "Failed to download", e );
        Files.deleteIfExists ( file );
        throw e;
    }

    context.scheduleImport ( file, name );
}
 
Example 20
@Override
protected Mod call() throws Exception {
    try {
        updateMessage("Preparing");
        updateProgress(-1, 100);
        updateMessage("Connecting to server");
        if (expectedName != null) {
            updateTitle("Download " + expectedName + (expectedVersion != null ? "#" + expectedVersion.toString() : ""));
        } else {
            updateTitle("Download: " + url.toString());
        }
        URLConnection conn = url.openConnection();
        conn.setConnectTimeout(CONNECT_TIMEOUT);
        conn.setReadTimeout(READ_TIMEOUT);
        conn.connect();
        InputStream stream = conn.getInputStream();
        long contentLength = conn.getContentLengthLong();
        updateProgress(0, contentLength);
        downloadingFile = Datastore.getInstance().getFMMDir().resolve("downloads").resolve("Download-tmp-" + random.nextLong() + ".part");
        Files.createDirectories(downloadingFile.getParent());
        FileOutputStream writer = new FileOutputStream(downloadingFile.toFile());
        byte buf[] = new byte[DOWNLOAD_BUFFER_SIZE];
        int read = 0;
        long totalRead = 0;
        updateMessage("Downloading");
        started = System.currentTimeMillis();
        int currentReadingSpeed = 32;
        do {
            if (isCancelled()) {
                updateMessage("Cancelled");
                failed();
                return null;
            }
            read = stream.read(buf, 0, currentReadingSpeed);

            if (currentReadingSpeed < DOWNLOAD_BUFFER_SIZE) {
                currentReadingSpeed *= 2;
            }
            if (read > 0) {
                totalRead += read;
                writer.write(buf, 0, read);
                if (System.currentTimeMillis() - lastUpdate > 200) {
                    updateMessage("Downloading: " + Util.formatBytes(totalRead) + " / " + Util.formatBytes(contentLength));
                    updateProgress(totalRead, contentLength);
                    lastUpdate = System.currentTimeMillis();
                }
            }
        } while (read > 0);
        ended = System.currentTimeMillis();
        updateMessage("Took " + (ended - started) + " ms to download");
        updateProgress(totalRead, contentLength);
        updateMessage("Installing");
        Mod mod = ModpackDetectorVisitor.parseMod(downloadingFile);
        if (mod == null) {
            updateMessage("File was not a mod");
            failed();
        } else {
            if (expectedName != null) {
                if (!expectedName.equals(mod.getName())) {
                    updateMessage("This file does not contain the expected mod");
                    failed();
                }
            }
            if (expectedVersion != null) {
                if (!expectedVersion.equals(mod.getVersion())) {
                    updateMessage("This file does not contain the expected version of the mod");
                    failed();
                }
            }
            mod.setPath(Datastore.getInstance().getFMMDir().resolve("mods").resolve(mod.getName() + "_" + mod.getVersion().toString() + ".zip"));
            updateMessage("Done");
            succeeded();
            return mod;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}