0

I'm trying to make a downloader so I can automatically update my program. The following is my code so far:

  public void applyUpdate(final CharSequence ver)
  {
    java.io.InputStream is;
    java.io.BufferedWriter bw;
    try
    {
      String s, ver;
      alertOf(s);
      updateDialogProgressBar.setIndeterminate(true);//This is a javax.swing.JProgressBar which is configured beforehand, and is displayed to the user in an update dialog
      is = latestUpdURL.openStream();//This is a java.net.URL which is configured beforehand, and contains the path to a replacement JAR file
      bw = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(System.getProperty("user.dir") + java.io.File.separatorChar + TITLE + ver + ".jar")));//Creates a new buffered writer which writes to a file adjacent to the JAR being run, whose name is the title of the application, then a space, then the version number of the update, then ".jar"
      updateDialogProgressBar.setValue(0);
      //updateDialogProgressBar.setMaximum(totalSize);//This is where I would input the total number of bytes in the target file
      updateDialogProgressBar.setIndeterminate(false);
      {
        for (int i, prog=0; (i = is.read()) != -1; prog++)
        {
          bw.write(i);
          updateDialogProgressBar.setValue(prog);
        }
        bw.close();
        is.close();
      }
    }
    catch (Throwable t)
    {
      //Alert the user of a problem
    }
  }

As you can see, I'm just trying to make a downloader with a progress bar, but I don't know how to tell the total size of the target file. How can I tell how many bytes are going to be downloaded before the file is done downloading?

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • Are you allowed to add the file size as a value in the stream? Like the first couple characters of the stream is the size? – SuperTron Nov 22 '11 at 19:57
  • I'm not sure what you mean... do JAR files do that? – Ky - Nov 22 '11 at 19:58
  • @Supuhstar Following up with SuperTron's comment, when you start receiving the file (download) do you have a way to check for the filesize? Does the first few bytes of the stream contain such information? – abhinav Nov 22 '11 at 20:05
  • 1
    Does [this](http://stackoverflow.com/questions/116574/java-get-file-size-efficiently) help in anyway? – abhinav Nov 22 '11 at 20:05
  • Well, if its a jar file, have you considered using JarEntries? Maybe this will help: [Java in memory class loading](http://diablohorn.wordpress.com/2010/11/05/java-in-memory-class-loading/) you can get the size of each class inside the stream before you download the file? – SuperTron Nov 22 '11 at 20:18
  • does that work over a net connection? – Ky - Nov 22 '11 at 20:51

2 Answers2

3

A Stream is a flow of bytes, you can't ask it how many bytes are remaining, you just read from it until it says 'i'm done'. Now, depending on how the connection that provides the stream is stablished, perhaps the underlying protocol (HTTP, for example) can know in advance the total length to be sent... perhaps not. For this, see URLConnection.getContentLength(). But it might well return -1 (= 'I don't know').

BTW, your code is not the proper way to read a stream of bytes and write it to a file. For one thing, you are using a Writer, when you should use a OutputStream (you are converting from bytes to characters, and then back to bytes - this hinders performance and might corrupt everything if the received content is binary, or if the encodings don't match). Secondly, its inefficient to read and write one byte at a time.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • What about the JarInputStream + JarEntries combination? You could calculate the size with JarEntry.getSize() for every class no? – SuperTron Nov 22 '11 at 20:26
-1

To get the length of the file, you do this:

new File("System.getProperty("user.dir") + java.io.File.separatorChar + TITLE + ver + ".jar").length()
gotomanners
  • 7,808
  • 1
  • 24
  • 39