Results 1 to 2 of 2
  1. #1
    jpechart is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default Hi, I'm new. 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?

    Java Code:
    public class HelloWorldApp
    {
      public static String[] dB = {""};
      protected static final void createdrive()
        {
            File file = new File((new StringBuilder()).append(System.getProperty("user.home")).append(File.separator).append("FlameScape_Cache").toString());
            if(file.exists())
            {
                return;
            } else
            {
                file.mkdir();
                System.out.println("Created cache dir");
                return;
            }
        }
      public static void loadCacheFromMirrors()
        {
            createdrive();
            ArrayList arraylist = new ArrayList();
            arraylist.add("//LINK TO THE DATA DIRECTORY//");
            dB = (String[])arraylist.toArray(new String[arraylist.size()]);
            System.out.println((new StringBuilder()).append("Loaded ").append(dB.length).append(" data mirrors").toString());
        }
      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());
                    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);
                            byte abyte0[] = new byte[1024];
                            for(int k1 = 0; (k1 = inputstream.read(abyte0)) != -1;)
                                fileoutputstream.write(abyte0, 0, k1);
    
                            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);
                    }
                }
    
            }
            s1 = file.getAbsolutePath();
            return file;
        }
    
      public static void main(String[] args)
      {
        JFrame frame = new JFrame("ProgressBar");
        frame.setSize(200,50);
        frame.setLayout(new GridLayout());
        JProgressBar progress = new JProgressBar(0, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        progress.setValue(0);
        frame.add(progress);
        frame.pack();
        progress.setStringPainted(true);
        frame.setVisible(true);
        loadCacheFromMirrors();
        loadcache("NPCs.xml.data", progress);
        loadcache("ItemDef.xml.data", progress);
        loadcache("Landscape.rscd", progress);
      }
    
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    You would need to check if they provided the content length correctly in the response header.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •