Commits

Anonymous committed f91fd3b

Channels

  • Participants
  • Parent commits dd84d0b

Comments (0)

Files changed (4)

 test.results.dir=${build.dir}/test/results
 jar=${build.dir}/nbNetCatUpdater.jar
 javadoc.dir=${build.dir}/javadoc
+lib.dir=${classes.dir}/lib
 
 # E.g.: cp=lib/x.jar:lib/y.jar
-cp=
+cp=${lib.dir}/httpclient.jar:${lib.dir}/httpcore.jar
 extra.run.cp=
 extra.test.cp=
 extra.test.run.cp=
         </depend>
         <javac srcdir="${src.dir}" destdir="${classes.dir}" source="1.5" debug="${debug}" deprecation="${deprecation}" includeantruntime="false">
             <classpath path="${cp}"/>
+            <classpath path="${lib.dir}"/>
             <compilerarg value="-Xlint:unchecked"/>
         </javac>
         <copy todir="${classes.dir}">

src/org/narsereg/nbnetcatupdater/NBUpdaterDownloadFile.java

 package org.narsereg.nbnetcatupdater;
 
 import java.io.*;
-import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
  *
  * @author narsereg
  */
-public class NBUpdaterDownloadFile extends SwingWorker<Boolean, Integer> {
+public class NBUpdaterDownloadFile extends SwingWorker<Boolean, Integer> implements Runnable{
     NBUpdaterDownloadFile(String fileURL, NBUpdaterForm nbuf){
-        this.fileURL = fileURL;
         this.nbuf = nbuf;
-        conn = openConnection();
+        
+        String pathToFile = (!nbuf.getDirectoryToDownload().equals("")) ? (nbuf.getDirectoryToDownload() + File.separator) : ""; 
+        downloadedFile = new File(pathToFile + fileURL.substring(fileURL.lastIndexOf("/") + 1));    
+        try {
+            fileLink = new URL(fileURL);
+        } catch (MalformedURLException ex) {
+            Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
+        }
+        
+        fileSize = getFileSize();
     }
     
-    volatile boolean interruptThread = false;
+
+    int fileSize;
+    private NBUpdaterForm nbuf = null;
+    private ReadableByteChannel rbc = null;
+    private File downloadedFile = null;
+    private URL fileLink;
+    FileOutputStream fos = null;
+    
     
     @Override
     public Boolean doInBackground(){
-        String pathToFile = (!nbuf.getDirectoryToDownload().equals("")) ? (nbuf.getDirectoryToDownload() + File.separatorChar) : ""; 
-        File downloadedFile = new File(pathToFile + fileURL.substring(fileURL.lastIndexOf("/") + 1));
-        try {
-            
-            BufferedInputStream in = new BufferedInputStream(conn.getInputStream(), 8096);
-            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(downloadedFile), 8096);
+            PublishThread pt = null;
             try {
-                byte[] buffer = new byte[1024];
-                int numRead = 0;
-                int current = 0;
-                while (!isCancelled() &&(numRead = in.read(buffer)) != -1){
-                    current += numRead;
-                    publish(current);
-                    out.write(buffer, 0, numRead);
+                fos = new FileOutputStream(downloadedFile);
+            } catch (FileNotFoundException ex) {
+                Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
+            }
+            try {
+                pt = new PublishThread(fos, this);
+                pt.start();
+                try {
+                    rbc = Channels.newChannel(fileLink.openStream());
+                    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
+                } catch (IOException ex) {
+                    Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
                 }
-            }  finally {
-                out.flush();
-                out.close();
-                in.close();
-                
+        } finally {
+            try {
+                pt.isDownloaded = true;
+                fos.flush();
+                fos.close();
+                publish(fileSize);
+            } catch (IOException ex) {
+                Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
             }
-        } catch (IOException ex) {
-            Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
         }
         nbuf.unzipDownloadedFile(downloadedFile.getAbsolutePath());
         return true;
     }
     
+    protected void closeConnection() throws InterruptedException{
+//        throw new ThreadDeath();
+//        try {
+//            fos.close();
+//        } catch (IOException ex) {
+//            Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
+//        }
+//        try {
+//            fos.getChannel().close();
+//            fos.getChannel().force(true);
+//        } catch (IOException ex) {
+//            Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
+//        }
+//        try {
+//            rbc.close();
+//        } catch (IOException ex) {
+//            Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
+//        }
+        Thread.currentThread().interrupt();
+    }
+    
     @Override
     protected void process(List<Integer> downloadedBytes){
         for(Integer i : downloadedBytes){
         }
     }
     
-    private String fileURL;
-    HttpURLConnection conn = null;
-    NBUpdaterForm nbuf = null;
-    
-    private HttpURLConnection openConnection(){
-        HttpURLConnection conn = null;
+    private int getFileSize(){
+        URLConnection conn = null;
         try {
-            URL fileLink = new URL(fileURL);
-            conn = (HttpURLConnection) fileLink.openConnection();
-            
-        } catch (MalformedURLException ex) {
-            Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
+            conn = fileLink.openConnection();
         } catch (IOException ex) {
             Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
         }
-        
-        return conn;
-    }
-    
-    public int getFileSize(){
+
         return conn.getContentLength();
     }
     
+    class PublishThread extends Thread{
+
+        FileOutputStream fos = null;
+        NBUpdaterDownloadFile nbudf = null;
+        boolean isDownloaded = false;
+
+        public PublishThread(FileOutputStream fos, NBUpdaterDownloadFile nbudf) {
+            this.fos = fos;
+            this.nbudf = nbudf;
+        }
+
+        @Override
+        public void run() {
+            while(!isDownloaded){
+                try {
+                    nbudf.publish((int)fos.getChannel().size());
+                } catch (IOException ex) {
+                    Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
+                }
+                try {
+                    Thread.sleep(1000);
+                } catch (InterruptedException ex) {
+                    Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
+                }
+
+            }
+        }
+    }
+    
 }
+
+

src/org/narsereg/nbnetcatupdater/NBUpdaterForm.java

 import java.util.zip.ZipException;
 import java.util.zip.ZipFile;
 import javax.swing.JFileChooser;
-
 /**
  *
  * @author narsereg
     }//GEN-LAST:event_chooseDownloadDirButtonActionPerformed
 
     private void downloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadButtonActionPerformed
-        downloadButton.setEnabled(false);
+        //downloadButton.setEnabled(false);
         NBUpdaterUtils nbUtils = new NBUpdaterUtils();
         nbUtils.setOption(options[bundleComboBox.getSelectedIndex()]);
         nbUtils.setPlatform(platforms[platformComboBox.getSelectedIndex()]);
         NBUpdaterDownloadFile df = new NBUpdaterDownloadFile(nbUtils.getDownloadFileURL(), this);
+        if (downloadButton.getText().equals("Download")) {
+            this.setComponentsEnabled(false);
+            
             downloadProgressBar.setMinimum(0);
-            downloadProgressBar.setMaximum(df.getFileSize());
+            downloadProgressBar.setMaximum(df.fileSize);
             downloadProgressBar.setStringPainted(true);
             df.execute();
+            downloadButton.setText("Cancel");
+        } else {
+            this.setComponentsEnabled(true);
+//            try {
+//                //df.
+//                df.cancel(true);
+//            } catch (java.lang.NullPointerException ex) {
+//                
+//            }
+//            df = null;
+            //df.cancel(true);
+            df.cancel(true);
+//            try {
+//                df.closeConnection();
+//            } catch (InterruptedException ex) {
+//                Logger.getLogger(NBUpdaterForm.class.getName()).log(Level.SEVERE, null, ex);
+//            }
+            
+            //df.cancel(true);
+            downloadButton.setText("Download");
+        }
+//            downloadProgressBar.setMinimum(0);
+//            downloadProgressBar.setMaximum(df.fileSize);
+//            downloadProgressBar.setStringPainted(true);
+//            df.execute();
+            
+            
     }//GEN-LAST:event_downloadButtonActionPerformed
 
+    private void setComponentsEnabled(boolean enabled){
+        chooseDownloadDirButton.setEnabled(enabled);
+        chooseExtractToButton.setEnabled(enabled);
+        platformComboBox.setEnabled(enabled);
+        bundleComboBox.setEnabled(enabled);
+    }
+        
     protected String getDirectoryToDownload(){
         return downloadToTextField.getText();
     }
             chooseExtractToButton.setEnabled(false);
     }//GEN-LAST:event_platformComboBoxItemStateChanged
 
-    protected void updateDownloadProgressbar(Integer bytes){
+    protected void updateDownloadProgressbar(int bytes){
         downloadProgressBar.setValue(bytes);
     }
     
                 File file = new File(pathToFileToUnzip);
 
                 ZipFile zip = new ZipFile(file);
-                String newPath = extractToTextField.getText().concat(File.pathSeparator).concat(file.getName().replace(".zip", ""));
+                String newPath = extractToTextField.getText().concat(File.separator).concat(file.getName().replace(".zip", ""));
 
                 new File(newPath).mkdir();
                 Enumeration zipFileEntries = zip.entries();
 
                     if (!entry.isDirectory())
                     {
-                        BufferedInputStream is = new BufferedInputStream(zip
-                        .getInputStream(entry));
+                        BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
                         int currentByte;
                         byte data[] = new byte[BUFFER];