Results 1 to 2 of 2
  1. #1
    VisualPK is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Client not auto downloading cache.

    We want our client to auto download a cache. We copy and pasted the code from our other client to this one and it wouldnt even try to download it. We updated the version number and changed the link for the cache but it still did not work. here is the relevant code.

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.FileWriter;
    import java.io.BufferedWriter;
    import java.io.BufferedOutputStream;
    import java.io.BufferedInputStream;
    import java.io.FileOutputStream;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.net.URLConnection;
    import java.net.URL;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import java.util.Enumeration;
    
    import sign.signlink;
    
    public class CacheDownloader {
    
     private client client;
    
     private final int BUFFER = 1024;
    
     private final int VERSION = 11; // Version of cache
     private String cacheLink = "http://dl.dropbox.com/u/51164319/cache.zip"; // Link to cache
    
     private String fileToExtract = getCacheDir() + getArchivedName();
    
     public CacheDownloader(client client) {
      this.client = client;
     }
    
     private void drawLoadingText(String text) {
      client.drawLoadingText(35, text);
      //System.out.println(text);
     }
    
    
     private void drawLoadingText(int amount, String text) {
      client.drawLoadingText(amount, text);
      //System.out.println(text);
     }
    
     private String getCacheDir() {
      return signlink.findcachedir();
     }
    
     private String getCacheLink() {
      return cacheLink;
     }
    
     private int getCacheVersion() {
      return VERSION;
     }
    
     public CacheDownloader downloadCache() {
      try {
      File location = new File(getCacheDir());
      File version = new File(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat");
      
      if(!location.exists()) {
       //drawLoadingText("Loading new Updates....");
       downloadFile(getCacheLink(), getArchivedName());
    
       unZip();
       //System.out.println("UNZIP");
    
       BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
       versionFile.close();
      } else {
       if(!version.exists()) {
        //drawLoadingText("Downloading Cache Please wait...");
        downloadFile(getCacheLink(), getArchivedName());
    
        unZip();
        //System.out.println("UNZIP");
    
        BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
        versionFile.close();
    
       } else {
        return null;
       }
      }
      } catch(Exception e) {
    
      }
      return null;
     }
     
     private void downloadFile(String adress, String localFileName) {
      OutputStream out = null;
      URLConnection conn;
      InputStream in = null;
      
      try {
    
       URL url = new URL(adress);
       out = new BufferedOutputStream(
        new FileOutputStream(getCacheDir() + "/" +localFileName)); 
    
       conn = url.openConnection();
       in = conn.getInputStream(); 
      
       byte[] data = new byte[BUFFER]; 
     
       int numRead;
       long numWritten = 0;
       int length = conn.getContentLength();
    
     
       while((numRead = in.read(data)) != -1) {
        out.write(data, 0, numRead);
        numWritten += numRead;
    
                   int percentage = (int)(((double)numWritten / (double)length) * 100D);
        drawLoadingText(percentage, "Downloading Cache " + percentage + "%");
    
       }
    
       System.out.println(localFileName + "\t" + numWritten);
       drawLoadingText("Finished downloading "+getArchivedName()+"!");
    
      } catch (Exception exception) {
       exception.printStackTrace();
      } finally {
       try {
        if (in != null) {
         in.close();
        }
        if (out != null) {
         out.close();
        }
       } catch (IOException ioe) {
       }
      }
    
     }
    
     private String getArchivedName() {
      int lastSlashIndex = getCacheLink().lastIndexOf('/');
      if (lastSlashIndex >= 0 
       && lastSlashIndex < getCacheLink().length() -1) { 
       return getCacheLink().substring(lastSlashIndex + 1);
      } else {
       //System.err.println("error retreiving archivaed name.");
      }
      return "";
     }
    
    
    
    
     private void unZip() {
    
      try {
           InputStream in = 
        new BufferedInputStream(new FileInputStream(fileToExtract));
       ZipInputStream zin = new ZipInputStream(in);
       ZipEntry e;
    
       while((e=zin.getNextEntry()) != null) {
    
                      if(e.isDirectory()) {
         (new File(getCacheDir() + e.getName())).mkdir();
                      } else {
    
        if (e.getName().equals(fileToExtract)) {
         unzip(zin, fileToExtract);
         break;
        }
                unzip(zin, getCacheDir() + e.getName());
        }
        //System.out.println("unzipping2 " + e.getName());
       }
       zin.close();
    
      } catch(Exception e) {
       e.printStackTrace();
      }
     }
    
     private void unzip(ZipInputStream zin, String s) 
      throws IOException {
    
      FileOutputStream out = new FileOutputStream(s);
      //System.out.println("unzipping " + s);
      byte [] b = new byte[BUFFER];
      int len = 0;
    
      while ((len = zin.read(b)) != -1) {
       out.write(b,0,len);
      }
      out.close();
     }
    }
    Last edited by pbrockway2; 04-15-2012 at 04:48 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Client not auto downloading cache.

    Also at javaprogrammingforums.com where "Unsupported major.minor version 51.0" is mentioned.

    -----

    When posting code, use the "code" tags: put [code] at the start of the code and [/code] at the end so that the code is readable. The same applies at javaprogrammingforums.com.

    Also it is nice to alert people when you are pursuing a discussion in multiple fora. But, be aware, that many will not engage in a discussion presented this way for fear of wasting their time with something that has been dealt with elsewhere. See Be Forthright When Cross Posting To Other Sites at JavaRanch for example.
    Last edited by pbrockway2; 04-15-2012 at 04:51 AM.

Similar Threads

  1. Auto contrast and auto brightness
    By oxxxis in forum Java 2D
    Replies: 0
    Last Post: 01-21-2010, 09:32 PM
  2. Auto-complete/Auto-fix for custom statement
    By dark_cybernetics in forum Eclipse
    Replies: 0
    Last Post: 08-19-2008, 12:19 PM
  3. JSP - using connection cache
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 10:54 AM
  4. BuffededImage cache
    By tim in forum New To Java
    Replies: 1
    Last Post: 01-24-2008, 01:00 AM
  5. cache problem
    By MichYer in forum Java Applets
    Replies: 3
    Last Post: 07-11-2007, 10:13 AM

Posting Permissions

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