Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Hi, I'm new here. Need help bad!

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi, I'm new here. Need help bad!

    Now I am developing an embeded jar file to make a special game client for a java game on my website.

    Ok Here is what I have in my NetBeans IDE. (This is just a segment of one class of many, I transfered it to my NetBeans for quick testing purposes).

    The point is, it has to fetch the game files from my server and download them to the client's computer. I already programmed in downloading, and an updating system. It functions flawlessly. What I am working on now is a loading bar. The loading bar isn't swing for the client, it is UI inside the client. I am just using swing because I'm testing in NetBeans.

    ::::MY PROBLEM::::
    I can calculate how many bytes have been transferred so far, but I do not know how to find the total size of the value before it is all transferred over.
    I labeled where in the code box as "///////HERE///////////".

    Can someone tell me what I must do to get the total bytes of the file before it downloads so I can make an accurate loading bar?

     public static final File loadcache(String s1, JProgressBar progressbar)
        {
            progressbar.setIndeterminate(true);
            progressbar.setString("Analyzing " + s1);
            File file = new File((new StringBuilder()).append(System.getProperty("user.home")).append(File.separator).append("FlameScape_Cache").append(File.separator).append(s1).toString());
            System.out.print(file.length());
            if(!file.isFile())
            {
                System.out.println((new StringBuilder()).append("Data file not found locally: ").append(s1).toString());
                boolean flag = false;
                ArrayList arraylist = new ArrayList();
                String as1[] = dB;
                int i1 = as1.length;
                for(int j1 = 0; j1 < i1;)
                {
                    String s2 = as1[j1];
                    String s3 = (new StringBuilder()).append(s2).append(File.separator).append(s1).toString();
                    System.out.print((new StringBuilder()).append("\tTrying ").append(s3).append(" => ").toString());
                    try
                    {
                        HttpURLConnection httpurlconnection = (HttpURLConnection)(new URL(s3)).openConnection();
                        if(httpurlconnection.getResponseCode() != 200)
                        {
                            System.out.println("Unable to obtain file. (" + httpurlconnection.getResponseCode() + ")");
                            continue;
                        }
                        System.out.print("Downloading.. ");
                        InputStream inputstream = httpurlconnection.getInputStream();
                        file.createNewFile();
                        FileOutputStream fileoutputstream = new FileOutputStream(file);
                        int Total = ///////HERE///////////;
                        progressbar.setIndeterminate(false);
                        byte abyte1[] = new byte[1024];
                        int Progress = 0;
                        for(int k1 = 0; (k1 = inputstream.read(abyte1)) != -1;) {
                            Progress = Progress + abyte1.length;
                            fileoutputstream.write(abyte1, 0, k1);
                            progressbar.setValue(Math.round((Progress/Total)*100));
                            progressbar.setString(Math.round((Progress/Total)*100)) + "%");
     
                        }
                        inputstream.close();
                        fileoutputstream.close();
                        System.out.println("Done.");
                        break;
                    }
                    catch(Exception exception1)
                    {
                        System.out.println("Unable to obtain file.");
                        arraylist.add(exception1);
                        j1++;
                    }
                }
     
                if(arraylist.size() == dB.length)
                {
                    System.out.println((new StringBuilder()).append("Downloading data file ").append(s1).append(" failed.\nPlease report the error, and include the following exception data:").toString());
                    Exception exception;
                    for(Iterator iterator = arraylist.iterator(); iterator.hasNext(); exception.printStackTrace())
                        exception = (Exception)iterator.next();
     
                    System.exit(1);
                }
            } else {
                System.out.println("The file: " + s1 + " has been found locally.");
                System.out.print((new StringBuilder()).append("\tTesing Checksum (").append(s1).append(")").append(" => ").toString());
                String CHECKSUM1 = "";
                String CHECKSUM2 = "";
                try {
                     CHECKSUM1 = MD5Checksum.getMD5Checksum(file.getPath());
                     URL CheckServ = new URL("//LINK TO DATA DIRECTORY//remotechecksum.php?file=" + s1);
                     URLConnection yc = CheckServ.openConnection();
                     BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
                     String inputLine;
                     while ((inputLine = in.readLine()) != null) {
                         CHECKSUM2 = inputLine;
                         break;
                     }
                     in.close();
                } catch (Exception e) {
                     e.printStackTrace();
                }
                System.out.print("(1)" + CHECKSUM1 + ".. (2)" + CHECKSUM2 + "..");
                if(CHECKSUM1.equalsIgnoreCase(CHECKSUM2)) {
                    System.out.print("\tChecksum success.  Matching strings.");
                } else {
                    System.out.println((new StringBuilder()).append("\tChecksum failed.  Nonmatching strings.").append(s1).toString());
                   //DOWNLOAD CODE REPEATED//
                }
     
            }
            s1 = file.getAbsolutePath();
            return file;
        }


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Hi, I'm new here. Need help bad!

    I'd say you need to have a look at all the files you need to transfer and sum their size up into one total size.

    Does that make sense?

    // Json