├── 0072229713_code.zip ├── Capture-(Part 1).JPG ├── Capture-(Part 2).JPG ├── Capture_SUCCESS.JPG ├── manifest.mf ├── Capture_SUCCESS_2.JPG ├── Capture_SUCCESS_3.JPG ├── dist ├── Download_Manager.jar └── README.TXT ├── Screenshots ├── screenshot_1.png ├── screenshot_2.JPG └── screenshot_3.JPG ├── nbproject ├── private │ ├── private.properties │ └── private.xml ├── genfiles.properties ├── project.xml ├── project.properties └── build-impl.xml ├── Download Links.txt ├── src └── download │ └── manager │ ├── ProgressRenderer.java │ ├── DownloadsTableModel.java │ ├── abc.java │ ├── Download.java │ └── DownloadManager.java ├── README.md └── df /0072229713_code.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nowshad-hasan/Download_Manager/master/0072229713_code.zip -------------------------------------------------------------------------------- /Capture-(Part 1).JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nowshad-hasan/Download_Manager/master/Capture-(Part 1).JPG -------------------------------------------------------------------------------- /Capture-(Part 2).JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nowshad-hasan/Download_Manager/master/Capture-(Part 2).JPG -------------------------------------------------------------------------------- /Capture_SUCCESS.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nowshad-hasan/Download_Manager/master/Capture_SUCCESS.JPG -------------------------------------------------------------------------------- /manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /Capture_SUCCESS_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nowshad-hasan/Download_Manager/master/Capture_SUCCESS_2.JPG -------------------------------------------------------------------------------- /Capture_SUCCESS_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nowshad-hasan/Download_Manager/master/Capture_SUCCESS_3.JPG -------------------------------------------------------------------------------- /dist/Download_Manager.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nowshad-hasan/Download_Manager/master/dist/Download_Manager.jar -------------------------------------------------------------------------------- /Screenshots/screenshot_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nowshad-hasan/Download_Manager/master/Screenshots/screenshot_1.png -------------------------------------------------------------------------------- /Screenshots/screenshot_2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nowshad-hasan/Download_Manager/master/Screenshots/screenshot_2.JPG -------------------------------------------------------------------------------- /Screenshots/screenshot_3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nowshad-hasan/Download_Manager/master/Screenshots/screenshot_3.JPG -------------------------------------------------------------------------------- /nbproject/private/private.properties: -------------------------------------------------------------------------------- 1 | compile.on.save=true 2 | user.properties.file=C:\\Users\\NowshadApu\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties 3 | -------------------------------------------------------------------------------- /Download Links.txt: -------------------------------------------------------------------------------- 1 | http://vorboss.dl.sourceforge.net/project/codeblocks/Binaries/16.01/Windows/codeblocks-16.01-setup.exe 2 | 3 | http://books.mcgraw-hill.com/downloads/products/0072229713/0072229713_code.zip -------------------------------------------------------------------------------- /nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=26d9174a 2 | build.xml.script.CRC32=311b5870 3 | build.xml.stylesheet.CRC32=8064a381@1.75.2.48 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=26d9174a 7 | nbproject/build-impl.xml.script.CRC32=0ae71abd 8 | nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 9 | -------------------------------------------------------------------------------- /nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | Download Manager 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /nbproject/private/private.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | src/download/manager/DownloadManager.java 6 | 7 | 8 | 62 9 | 10 | 11 | 12 | 13 | 14 | 15 | file:/F:/Projects/My%20Projects/Networking%20Lab/Download_Manager/src/download/manager/abc.java 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/download/manager/ProgressRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package download.manager; 7 | 8 | /** 9 | * 10 | * @author NowshadApu 11 | */ 12 | 13 | import java.awt.*; 14 | import javax.swing.*; 15 | import javax.swing.table.TableCellRenderer; 16 | 17 | public class ProgressRenderer extends JProgressBar implements TableCellRenderer{ 18 | 19 | public ProgressRenderer(int min,int max) 20 | 21 | { 22 | super(min,max); 23 | } 24 | 25 | @Override 26 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 27 | 28 | setValue((int)((Float)value).floatValue()); 29 | return this; 30 | } 31 | 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /dist/README.TXT: -------------------------------------------------------------------------------- 1 | ======================== 2 | BUILD OUTPUT DESCRIPTION 3 | ======================== 4 | 5 | When you build an Java application project that has a main class, the IDE 6 | automatically copies all of the JAR 7 | files on the projects classpath to your projects dist/lib folder. The IDE 8 | also adds each of the JAR files to the Class-Path element in the application 9 | JAR files manifest file (MANIFEST.MF). 10 | 11 | To run the project from the command line, go to the dist folder and 12 | type the following: 13 | 14 | java -jar "Download_Manager.jar" 15 | 16 | To distribute this project, zip up the dist folder (including the lib folder) 17 | and distribute the ZIP file. 18 | 19 | Notes: 20 | 21 | * If two JAR files on the project classpath have the same name, only the first 22 | JAR file is copied to the lib folder. 23 | * Only JAR files are copied to the lib folder. 24 | If the classpath contains other types of files or folders, these files (folders) 25 | are not copied. 26 | * If a library on the projects classpath also has a Class-Path element 27 | specified in the manifest,the content of the Class-Path element has to be on 28 | the projects runtime path. 29 | * To set a main class in a standard Java project, right-click the project node 30 | in the Projects window and choose Properties. Then click Run and enter the 31 | class name in the Main Class field. Alternatively, you can manually type the 32 | class name in the manifest Main-Class element. 33 | 34 | 35 | It is the Jar file of Download Manager . It can parallely download 36 | different parts of the file andmerge those in correct order. I check 37 | it form the link bellow: 38 | http://books.mcgraw-hill.com/downloads/products/0072229713/0072229713_code.zip 39 | But I'm not sure if it works well in the other link or not. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Download_Manager 2 | This project is made for our academic projct of **Networking lab**. It is the project which is described in _Java- The Complete Reference_. But some extra features are added. 3 | 4 | ## Getting Started 5 | **JAVA- The Complete Reference** is one of the nicest books I've ever read for JAVA language by **_Herbert Schildt_**. There is a project in the last chapter named **Download Manager**. It is a nice project but has some limitations. For individual download, it creates a separate thread. But for a particular file download, it uses only one thread. If we can download a file using multiple threads, wouldn't be it nice? That's the project I did in my _Networking Lab_. 6 | 7 | ![alt text](https://github.com/nowshad-hasan/Download_Manager/blob/master/Screenshots/screenshot_1.png) 8 | 9 | We all know that file is downloaded _byte by byte_. So, I save those bytes from the server in different ArrayList from different threads. After downloading the last byte, I merged those downloaded bytes from ArrayList sequentially. Finally, the **_File to be Downloaded_** is made. Here is the screenshot of those bytes of the downloading file. 10 | 11 | ![alt text](https://github.com/nowshad-hasan/Download_Manager/blob/master/Screenshots/screenshot_3.JPG) 12 | 13 | After finishing the download, the file is saved on the destination folder. 14 | 15 | ![alt text](https://github.com/nowshad-hasan/Download_Manager/blob/master/Screenshots/screenshot_2.JPG) 16 | 17 | ### Prerequisites 18 | You need to install _JAVA_ for running this project. Please check by typing `java -version` if _JAVA_ is installed or not. 19 | 20 | ### Installation 21 | Go to **_Download_Manager/dist_** folder or click the link https://github.com/nowshad-hasan/Download_Manager/tree/master/dist. 22 | Download the **Download_Manager.jar** file
and run `java -jar Download_Manager.jar` in _terminal or cmd_. 23 | -------------------------------------------------------------------------------- /src/download/manager/DownloadsTableModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package download.manager; 7 | 8 | /** 9 | * 10 | * @author NowshadApu 11 | */ 12 | 13 | import java.util.*; 14 | import javax.swing.*; 15 | import javax.swing.table.*; 16 | 17 | public class DownloadsTableModel extends AbstractTableModel implements Observer{ 18 | 19 | private static final String[] columnNames={"URL","Size","Progress","Status"}; 20 | private static final Class[] columnClasses={String.class,String.class,JProgressBar.class,String.class}; 21 | private ArrayList downloadList=new ArrayList(); 22 | 23 | public void addDownload(Download download) 24 | { 25 | download.addObserver(this); 26 | downloadList.add(download); 27 | fireTableRowsInserted(getRowCount()-1, getRowCount()-1); 28 | } 29 | 30 | public Download getDownload(int row) 31 | { 32 | return downloadList.get(row); 33 | } 34 | 35 | public void clearDownload(int row) 36 | { 37 | downloadList.remove(row); 38 | fireTableRowsDeleted(row, row); 39 | } 40 | 41 | public int getColumnCount() 42 | { 43 | return columnNames.length; 44 | } 45 | 46 | public String getColumnName(int col) 47 | { 48 | return columnNames[col]; 49 | 50 | } 51 | 52 | public Class getColumnClass(int col) 53 | { 54 | return columnClasses[col]; 55 | } 56 | 57 | public int getRowCount() 58 | { 59 | return downloadList.size(); 60 | } 61 | 62 | public Object getValueAt(int row,int col) 63 | { 64 | Download download=downloadList.get(row); 65 | switch(col) 66 | { 67 | case 0: 68 | return download.getUrl(); 69 | 70 | case 1: 71 | int size= download.getSize(); 72 | return (size==-1)? "":Integer.toString(size); 73 | 74 | case 2: 75 | return new Float(download.getProgress()); 76 | 77 | case 3: 78 | return Download.STATUSES[download.getStatus()]; 79 | } 80 | return ""; 81 | } 82 | 83 | public void update(Observable o,Object arg) 84 | { 85 | int index=downloadList.indexOf(o); 86 | fireTableRowsUpdated(index, index); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processor.options= 4 | annotation.processing.processors.list= 5 | annotation.processing.run.all.processors=true 6 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 7 | build.classes.dir=${build.dir}/classes 8 | build.classes.excludes=**/*.java,**/*.form 9 | # This directory is removed when the project is cleaned: 10 | build.dir=build 11 | build.generated.dir=${build.dir}/generated 12 | build.generated.sources.dir=${build.dir}/generated-sources 13 | # Only compile against the classpath explicitly listed here: 14 | build.sysclasspath=ignore 15 | build.test.classes.dir=${build.dir}/test/classes 16 | build.test.results.dir=${build.dir}/test/results 17 | # Uncomment to specify the preferred debugger connection transport: 18 | #debug.transport=dt_socket 19 | debug.classpath=\ 20 | ${run.classpath} 21 | debug.test.classpath=\ 22 | ${run.test.classpath} 23 | # Files in build.classes.dir which should be excluded from distribution jar 24 | dist.archive.excludes= 25 | # This directory is removed when the project is cleaned: 26 | dist.dir=dist 27 | dist.jar=${dist.dir}/Download_Manager.jar 28 | dist.javadoc.dir=${dist.dir}/javadoc 29 | excludes= 30 | includes=** 31 | jar.compress=false 32 | javac.classpath= 33 | # Space-separated list of extra javac options 34 | javac.compilerargs= 35 | javac.deprecation=false 36 | javac.processorpath=\ 37 | ${javac.classpath} 38 | javac.source=1.8 39 | javac.target=1.8 40 | javac.test.classpath=\ 41 | ${javac.classpath}:\ 42 | ${build.classes.dir} 43 | javac.test.processorpath=\ 44 | ${javac.test.classpath} 45 | javadoc.additionalparam= 46 | javadoc.author=false 47 | javadoc.encoding=${source.encoding} 48 | javadoc.noindex=false 49 | javadoc.nonavbar=false 50 | javadoc.notree=false 51 | javadoc.private=false 52 | javadoc.splitindex=true 53 | javadoc.use=true 54 | javadoc.version=false 55 | javadoc.windowtitle= 56 | main.class=download.manager.DownloadManager 57 | manifest.file=manifest.mf 58 | meta.inf.dir=${src.dir}/META-INF 59 | mkdist.disabled=false 60 | platform.active=default_platform 61 | run.classpath=\ 62 | ${javac.classpath}:\ 63 | ${build.classes.dir} 64 | # Space-separated list of JVM arguments used when running the project. 65 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. 66 | # To set system properties for unit tests define test-sys-prop.name=value: 67 | run.jvmargs= 68 | run.test.classpath=\ 69 | ${javac.test.classpath}:\ 70 | ${build.test.classes.dir} 71 | source.encoding=UTF-8 72 | src.dir=src 73 | test.src.dir=test 74 | -------------------------------------------------------------------------------- /src/download/manager/abc.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package download.manager; 7 | 8 | import static download.manager.Download.DOWNLOADING; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | import java.net.HttpURLConnection; 12 | import java.util.logging.Level; 13 | import java.util.logging.Logger; 14 | 15 | /** 16 | * 17 | * @author NowshadApu 18 | * 19 | * This class is the actual 'function1' which runs in the different thread.And parallel thread is 'Main thread '. The 'Download' class is referenced 20 | * in this class.So, when I change the different values in this class original values in the 'Download' class are also changed. 21 | */ 22 | 23 | /* 24 | This class extends thread. that means it runs thread by calling run() method. Here, all value come from Download class .So, I had to be careful if those are 25 | changed accidently in this class. 26 | */ 27 | public class abc extends Thread { 28 | 29 | Download d; 30 | int result; 31 | 32 | public abc(Download dd) { 33 | 34 | //Here dd is the reference of original Download object. 35 | d = dd; 36 | 37 | } 38 | 39 | //Now thread starts with the function calling run() . 40 | 41 | public void run() { 42 | 43 | /* 44 | In this function all values are changed in d.(something). That means all changes are kept in Download object. 45 | */ 46 | 47 | int flag1 = 0; 48 | 49 | HttpURLConnection connection; 50 | try { 51 | 52 | connection = (HttpURLConnection) d.url.openConnection(); 53 | connection.setRequestProperty("Range", "bytes=" + d.downloaded + "-"); 54 | connection.connect(); 55 | d.stream = connection.getInputStream(); 56 | } catch (IOException ex) { 57 | ex.printStackTrace(); 58 | } 59 | 60 | while (d.status == DOWNLOADING) { 61 | 62 | try { 63 | 64 | d.read1[d.counter1] = d.stream.read(d.buffer1[d.counter1]); 65 | } catch (IOException ex) { 66 | ex.printStackTrace(); 67 | } 68 | 69 | System.out.println("Downloaded from Part 1 " + d.downloaded); 70 | 71 | System.out.println("Read-- Psrt1 " + d.read1[d.counter1] + "\n"); 72 | System.out.println("size-downloaded Part1 " + (d.size - d.downloaded)); 73 | 74 | if (flag1 == 1) { 75 | d.downloaded = d.size; 76 | } 77 | 78 | if (d.read1[d.counter1] == -1 || (d.size - d.downloaded) == 0) { 79 | System.out.println("It is broken! Part1\n"); 80 | break; 81 | 82 | } 83 | 84 | if ((d.size - d.downloaded) <= d.MAX_BUFFER_SIZE) { 85 | flag1 = 1; 86 | } 87 | System.out.println("Bytes from Buffer1 ar (Part 1) "); 88 | for (int i = 0; i < d.buffer1[d.counter1].length; ++i) { 89 | System.out.print(" " + d.buffer1[d.counter1][i] + " "); 90 | } 91 | System.out.print("\n"); 92 | 93 | d.downloaded += d.read1[d.counter1]; 94 | 95 | ++d.counter1; 96 | 97 | System.out.println("Hiji Biji- Part1 " + d.counter1); 98 | d.stateChanged(); 99 | } 100 | 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/download/manager/Download.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package download.manager; 7 | 8 | /** 9 | * 10 | * @author NowshadApu 11 | */ 12 | import java.io.*; 13 | import java.net.HttpURLConnection; 14 | import java.net.URL; 15 | import java.util.Observable; 16 | import java.util.*; 17 | import java.net.*; 18 | import java.nio.file.Path; 19 | 20 | public class Download extends Observable implements Runnable { 21 | 22 | public static final int MAX_BUFFER_SIZE = 1024; 23 | public static final String STATUSES[] = {"Downloading", "Paused", "Complete", "Cancelled", "Error"}; 24 | 25 | public static final int DOWNLOADING = 0; 26 | public static final int PAUSED = 1; 27 | public static final int COMPLETE = 2; 28 | public static final int CANCELLED = 3; 29 | public static final int ERROR = 4; 30 | 31 | public URL url; 32 | public int size; 33 | public int downloaded; 34 | public int status; 35 | 36 | public String fileNameNew; 37 | public int counter1 = 0; 38 | 39 | /*The reason for taking two dimensional byte array is to store the byte arrarys in another array. I don't want to read and write sequentially. 40 | My plan is to read all the byte arrays sequentially or separately and then to write sequentially. 41 | */ 42 | byte buffer1[][] = new byte[100000][MAX_BUFFER_SIZE]; 43 | byte buffer2[][] = new byte[100000][MAX_BUFFER_SIZE]; 44 | 45 | //These two read array are also for storing 'read' information from reading bytes. 46 | 47 | int[] read1 = new int[100000]; 48 | int[] read2 = new int[100000]; 49 | 50 | public Download(URL url) { 51 | this.url = url; 52 | size = -1; 53 | downloaded = 0; 54 | status = DOWNLOADING; 55 | 56 | download(); 57 | } 58 | 59 | public String getUrl() { 60 | return url.toString(); 61 | } 62 | 63 | public int getSize() { 64 | return size; 65 | } 66 | 67 | public float getProgress() { 68 | return ((float) downloaded / size) * 100; 69 | } 70 | 71 | public int getStatus() { 72 | return status; 73 | } 74 | 75 | public void pause() { 76 | status = PAUSED; 77 | stateChanged(); 78 | } 79 | 80 | public void cancel() { 81 | status = CANCELLED; 82 | stateChanged(); 83 | } 84 | 85 | public void resume() { 86 | status = DOWNLOADING; 87 | stateChanged(); 88 | download(); 89 | } 90 | 91 | public void error() { 92 | status = ERROR; 93 | stateChanged(); 94 | } 95 | 96 | public void download() { 97 | Thread thread = new Thread(this); 98 | thread.start(); 99 | } 100 | 101 | public String getFileName(URL url) { 102 | 103 | String fileName = url.getFile(); 104 | return fileName.substring(fileName.lastIndexOf('/') + 1); 105 | 106 | } 107 | RandomAccessFile file = null; 108 | RandomAccessFile file2 = null; 109 | InputStream stream = null; 110 | InputStream stream2 = null; 111 | int a, b; 112 | abc ABC; 113 | 114 | public void run() { 115 | 116 | try { 117 | 118 | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 119 | 120 | connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); 121 | connection.connect(); 122 | 123 | 124 | if (connection.getResponseCode() / 100 != 2) { 125 | error(); 126 | } 127 | 128 | int contentLength = connection.getContentLength(); 129 | 130 | if (contentLength < 1) { 131 | error(); 132 | } 133 | System.out.println("Full Content is " + contentLength); 134 | 135 | if (size == -1) { 136 | size = 10000; 137 | /*Some of the place in the code I use 10000.It means that I want to download 10000 bytes at the first time. 138 | Then remaining bytes will be downloaded. 139 | */ 140 | 141 | stateChanged(); 142 | } 143 | 144 | fileNameNew = getFileName(url); 145 | file = new RandomAccessFile(getFileName(url), "rw"); 146 | 147 | file.seek(downloaded); 148 | 149 | stream = connection.getInputStream(); 150 | 151 | size = 10000; 152 | downloaded = 0; 153 | 154 | /* 155 | Here I make a function named 'function1' to download the first 10000 bytes.I use 'size' and 'downloaded' as parameter. 156 | */ 157 | 158 | function1(size, downloaded); 159 | 160 | System.out.println("Starting of FUNCTION2"); 161 | 162 | /* 163 | 'function2' downloads from 100001(In original,I think 1000001+1215) to remainging bytes. 164 | */ 165 | 166 | int sizeX = contentLength; 167 | int downloadedX = 10000 + 1215; 168 | int counter2 = finction2(sizeX, downloadedX); 169 | 170 | /* 171 | From this thread(Main thread) I run another thread from 'abc' class from 'function1'. So, I wait here untill that thread finishes its work. After 172 | finishing I write the random access file with buffer1(from function1) and buffer2 (from function2). 173 | */ 174 | 175 | ABC.join(); 176 | 177 | /* 178 | From these part I write the file 179 | equentially . That means, bytes downloaded from function1 and function2 are sequentially merged in the file . 180 | */ 181 | 182 | for (int i = 0; i <= (counter1); ++i) { 183 | file.write(buffer1[i], 0, read1[i]); 184 | } 185 | 186 | for (int i = 0; i < (counter2); ++i) { 187 | file.write(buffer2[i], 0, read2[i]); 188 | } 189 | 190 | if (status == DOWNLOADING) { 191 | System.out.println("Joss!!!"); 192 | status = COMPLETE; 193 | stateChanged(); 194 | 195 | } 196 | 197 | /* 198 | movingDirectory is the current directory and targerDirectory is what I choose from 'Select Folder' button.If I select nothing then the 199 | file will'be downloaded in the Project folder(By default). 200 | */ 201 | 202 | String movingDirectory = DownloadManager.targetDirectory; 203 | System.out.println(movingDirectory); 204 | 205 | } catch (Exception e) { 206 | error(); 207 | } finally { 208 | if (file != null) { 209 | 210 | 211 | 212 | String movingDirectory = DownloadManager.targetDirectory; 213 | String currentDirectory = System.getProperty("user.dir"); 214 | 215 | try { 216 | file.close(); 217 | 218 | try { 219 | 220 | //if there is a file created then finally that file will be moved to target directory 221 | 222 | File afile = new File(currentDirectory + "\\" + getFileName(url)); 223 | 224 | if (afile.renameTo(new File(movingDirectory + "\\" + getFileName(url)))) { 225 | System.out.println("File is moved successful!"); 226 | } else { 227 | System.out.println("File is failed to move!"); 228 | } 229 | 230 | } catch (Exception e) { 231 | e.printStackTrace(); 232 | } 233 | 234 | } catch (Exception e) { 235 | e.printStackTrace(); 236 | } 237 | } 238 | 239 | if (stream != null) { 240 | try { 241 | stream.close(); 242 | } catch (Exception e) { 243 | e.printStackTrace(); 244 | } 245 | } 246 | } 247 | } 248 | 249 | public void stateChanged() { 250 | setChanged(); 251 | notifyObservers(); 252 | } 253 | 254 | public void function1(int size, int downloaded) throws IOException { 255 | 256 | /* 257 | Here, new Thread in abc class is started. But value of this 'Download' class is passed to 'abc' class. This is called Pass By Reference. 258 | So if there is a change in abc class then, the value of this class is changed . 259 | */ 260 | 261 | ABC = new abc(this); 262 | ABC.start(); 263 | 264 | } 265 | 266 | public int finction2(int size, int downloaded) throws IOException, InterruptedException { 267 | 268 | /* 269 | Here, new Input Stream, HTTPURL connection, setRequestProperty all are set for this function. 270 | */ 271 | 272 | 273 | InputStream streamX = null; 274 | HttpURLConnection connectionX = (HttpURLConnection) url.openConnection(); 275 | connectionX.setRequestProperty("Range", "bytes=" + 0 + "-"); 276 | connectionX.connect(); 277 | streamX = connectionX.getInputStream(); 278 | 279 | int counter2 = 0; 280 | int flag2 = 0; 281 | 282 | //As I want to download 10,001-remaining bytes for this URL , so I use 'skip' function for skipping the bytes. 283 | 284 | System.out.println("Skipped " + (byte) streamX.skip(10000 + 1215)); 285 | 286 | while (status == DOWNLOADING) { 287 | 288 | if (size - downloaded > MAX_BUFFER_SIZE) { 289 | 290 | System.out.println("First Size-downloaded Part2 " + (size - downloaded)); 291 | } else { 292 | 293 | System.out.println("second Size-downloaded Part 2 " + (size - downloaded)); 294 | } 295 | 296 | read2[counter2] = streamX.read(buffer2[counter2]); 297 | 298 | System.out.println("Read-- Part2 " + read2[counter2] + "\n"); 299 | 300 | if (read2[counter2] == -1 || (size - downloaded) == 0) { 301 | System.out.println("It is broken! part2 \n"); 302 | break; 303 | } 304 | 305 | /* 306 | This part is to actually seeing the original bytes 307 | */ 308 | 309 | System.out.println("Bytes from Buffer1 ar (Part 2) "); 310 | for (int i = 0; i < buffer2[counter2].length; ++i) { 311 | System.out.print(" " + buffer2[counter2][i] + " "); 312 | } 313 | System.out.print("\n"); 314 | 315 | downloaded += read2[counter2]; 316 | ++counter2; 317 | System.out.println("Hiji Biji- Part 2 " + counter2); 318 | stateChanged(); 319 | 320 | } 321 | 322 | return counter2; 323 | 324 | } 325 | 326 | } 327 | -------------------------------------------------------------------------------- /src/download/manager/DownloadManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package download.manager; 7 | 8 | /** 9 | * 10 | * @author NowshadApu 11 | */ 12 | 13 | import java.awt.*; 14 | import java.awt.event.*; 15 | import java.io.File; 16 | import java.net.*; 17 | import java.util.*; 18 | import javafx.stage.DirectoryChooser; 19 | import javax.swing.*; 20 | import javax.swing.*; 21 | import javax.swing.event.*; 22 | 23 | 24 | 25 | 26 | 27 | public class DownloadManager extends JFrame implements Observer 28 | { 29 | private JTextField addTextField; 30 | 31 | private DownloadsTableModel tableModel; 32 | 33 | private JTable table; 34 | 35 | private JButton pauseButton,resumeButton; 36 | 37 | private JButton cancelButton, clearButton; 38 | private JButton selectFolder,showDownloads; 39 | 40 | private static Download selectedDownload; 41 | 42 | private boolean clearing; 43 | 44 | static public String emni,targetDirectory; 45 | 46 | public static boolean selectionFlag=false; 47 | 48 | public DownloadManager() 49 | { 50 | 51 | setTitle("Download Manager"); 52 | 53 | setSize(640, 480); 54 | 55 | addWindowListener(new WindowAdapter(){ 56 | public void windowClosing(WindowEvent e){ 57 | actionExit(); 58 | } 59 | }); 60 | 61 | JMenuBar menuBar=new JMenuBar(); 62 | JMenu fileMenu=new JMenu("File"); 63 | fileMenu.setMnemonic(KeyEvent.VK_F); 64 | JMenuItem fileExitMenuItem=new JMenuItem("Exit",KeyEvent.VK_X); 65 | fileExitMenuItem.addActionListener(new ActionListener() { 66 | 67 | @Override 68 | public void actionPerformed(ActionEvent e) { 69 | actionExit(); 70 | } 71 | }); 72 | 73 | 74 | fileMenu.add(fileExitMenuItem); 75 | menuBar.add(fileMenu); 76 | setJMenuBar(menuBar); 77 | 78 | 79 | 80 | JPanel addPanel= new JPanel(); 81 | 82 | selectFolder=new JButton("Select Folder"); 83 | selectFolder.addActionListener(new ActionListener() { 84 | 85 | @Override 86 | public void actionPerformed(ActionEvent e) { 87 | actionSelectFolder(); 88 | } 89 | }); 90 | addPanel.add(selectFolder); 91 | selectFolder.setEnabled(true); 92 | 93 | 94 | addTextField= new JTextField(30); 95 | addPanel.add(addTextField); 96 | 97 | 98 | 99 | 100 | 101 | JButton addButton = new JButton("Add Download"); 102 | addButton.addActionListener(new ActionListener(){ 103 | public void actionPerformed(ActionEvent e){ 104 | actionAdd(); 105 | } 106 | }); 107 | 108 | addPanel.add(addButton); 109 | 110 | 111 | tableModel= new DownloadsTableModel(); 112 | table= new JTable(tableModel); 113 | table.getSelectionModel().addListSelectionListener(new 114 | ListSelectionListener(){ 115 | public void valueChanged(ListSelectionEvent e){ 116 | tableSelectionChanged(); 117 | } 118 | }); 119 | 120 | table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 121 | ProgressRenderer renderer= new ProgressRenderer(0, 100); 122 | renderer.setStringPainted(true); 123 | table.setDefaultRenderer(JProgressBar.class, renderer); 124 | 125 | table.setRowHeight((int)renderer.getPreferredSize().getHeight()); 126 | 127 | JPanel downloadsPanel= new JPanel(); 128 | downloadsPanel.setBorder( 129 | BorderFactory.createTitledBorder("downloads")); 130 | downloadsPanel.setLayout(new BorderLayout()); 131 | downloadsPanel.add(new JScrollPane(table), 132 | BorderLayout.CENTER); 133 | 134 | JPanel buttonsPanel= new JPanel(); 135 | 136 | 137 | 138 | 139 | 140 | 141 | pauseButton= new JButton("pause"); 142 | pauseButton.addActionListener(new ActionListener() { 143 | 144 | @Override 145 | public void actionPerformed(ActionEvent e) { 146 | actionPause(); 147 | } 148 | }); 149 | pauseButton.setEnabled(false); 150 | buttonsPanel.add(pauseButton); 151 | 152 | 153 | resumeButton= new JButton("Resume"); 154 | resumeButton.addActionListener(new ActionListener(){ 155 | public void actionPerformed(ActionEvent e){ 156 | actionResume(); 157 | } 158 | }); 159 | resumeButton.setEnabled(false); 160 | buttonsPanel.add(resumeButton); 161 | 162 | 163 | cancelButton= new JButton("cancel"); 164 | cancelButton.addActionListener(new ActionListener(){ 165 | public void actionPerformed(ActionEvent e){ 166 | actionCancel(); 167 | } 168 | }); 169 | cancelButton.setEnabled(false); 170 | buttonsPanel.add(cancelButton); 171 | 172 | 173 | clearButton= new JButton("clear"); 174 | clearButton.addActionListener(new ActionListener(){ 175 | public void actionPerformed(ActionEvent e){ 176 | actionClear(); 177 | } 178 | }); 179 | clearButton.setEnabled(false); 180 | buttonsPanel.add(clearButton); 181 | 182 | 183 | 184 | showDownloads=new JButton("Show Downloads"); 185 | showDownloads.addActionListener(new ActionListener() { 186 | 187 | @Override 188 | public void actionPerformed(ActionEvent e) { 189 | actionShowDownloads(); 190 | } 191 | }); 192 | buttonsPanel.add(showDownloads); 193 | showDownloads.setEnabled(true); 194 | 195 | 196 | 197 | getContentPane().setLayout(new BorderLayout()); 198 | getContentPane().add(addPanel, BorderLayout.NORTH); 199 | getContentPane().add(downloadsPanel, BorderLayout.CENTER); 200 | getContentPane().add(buttonsPanel, BorderLayout.SOUTH); 201 | 202 | //String dfgh=selectedDownload.fileNameNew; 203 | //System.out.println(dfgh); 204 | } 205 | 206 | private void actionExit(){ 207 | System.exit(0); 208 | } 209 | 210 | private void actionAdd(){ 211 | URL verifiedUrl= verifyUrl(addTextField.getText()); 212 | if(verifiedUrl!= null){ 213 | tableModel.addDownload(new Download(verifiedUrl)); 214 | addTextField.setText(""); 215 | }else{ 216 | JOptionPane.showMessageDialog(this, 217 | "Invalid Download URL", "Error", JOptionPane.ERROR_MESSAGE); 218 | } 219 | } 220 | 221 | private URL verifyUrl(String url) 222 | { 223 | if(!url.toLowerCase().startsWith("http://")) 224 | return null; 225 | 226 | URL verifiedUrl =null; 227 | try 228 | { 229 | verifiedUrl=new URL(url); 230 | }catch(Exception e) 231 | { 232 | return null; 233 | 234 | } 235 | 236 | if(verifiedUrl.getFile().length()<2) 237 | return null; 238 | 239 | return verifiedUrl; 240 | } 241 | 242 | private void tableSelectionChanged() 243 | { 244 | if(selectedDownload!=null) 245 | selectedDownload.deleteObserver(DownloadManager.this); 246 | 247 | if(!clearing && table.getSelectedRow()>-1) 248 | { 249 | selectedDownload=tableModel.getDownload(table.getSelectedRow()); 250 | selectedDownload.addObserver(DownloadManager.this); 251 | updateButtons(); 252 | } 253 | } 254 | 255 | 256 | 257 | public void actionSelectFolder() 258 | { 259 | JFileChooser fc=new JFileChooser(); 260 | fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 261 | if(fc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) 262 | { 263 | targetDirectory=String.valueOf(fc.getSelectedFile()); 264 | 265 | } 266 | 267 | } 268 | 269 | public static String getTargetDirectory() 270 | { 271 | return targetDirectory; 272 | } 273 | 274 | private void actionPause() 275 | { 276 | selectedDownload.pause(); 277 | updateButtons(); 278 | 279 | } 280 | 281 | private void actionResume() 282 | { 283 | selectedDownload.resume(); 284 | updateButtons(); 285 | } 286 | 287 | private void actionCancel() 288 | { 289 | selectedDownload.cancel(); 290 | updateButtons(); 291 | } 292 | 293 | private void actionClear() 294 | { 295 | clearing=true; 296 | tableModel.clearDownload(table.getSelectedRow()); 297 | clearing=false; 298 | selectedDownload=null; 299 | updateButtons(); 300 | } 301 | 302 | private void actionShowDownloads() 303 | { 304 | try{ 305 | if(targetDirectory==null) 306 | { 307 | //String currentDirectory=System.getProperty("user.dir"); 308 | Desktop.getDesktop().open(new File(System.getProperty("user.dir"))); 309 | } 310 | else 311 | { 312 | Desktop.getDesktop().open(new File(targetDirectory)); 313 | // System.out.println(targetDirectory); 314 | } 315 | } 316 | catch(Exception ex) 317 | {ex.printStackTrace();} 318 | } 319 | 320 | private void updateButtons() 321 | { 322 | if(selectedDownload!=null) 323 | { 324 | int status=selectedDownload.getStatus(); 325 | 326 | switch(status) 327 | { 328 | case Download.DOWNLOADING: 329 | pauseButton.setEnabled(true); 330 | resumeButton.setEnabled(false); 331 | cancelButton.setEnabled(true); 332 | clearButton.setEnabled(false); 333 | break; 334 | 335 | case Download.PAUSED: 336 | pauseButton.setEnabled(false); 337 | resumeButton.setEnabled(true); 338 | cancelButton.setEnabled(true); 339 | clearButton.setEnabled(false); 340 | break; 341 | 342 | case Download.ERROR: 343 | pauseButton.setEnabled(false); 344 | resumeButton.setEnabled(true); 345 | cancelButton.setEnabled(false); 346 | clearButton.setEnabled(true); 347 | break; 348 | 349 | default: 350 | pauseButton.setEnabled(false); 351 | resumeButton.setEnabled(false); 352 | cancelButton.setEnabled(false); 353 | clearButton.setEnabled(true); 354 | 355 | } 356 | } 357 | else 358 | { 359 | pauseButton.setEnabled(false); 360 | resumeButton.setEnabled(false); 361 | cancelButton.setEnabled(false); 362 | clearButton.setEnabled(false); 363 | } 364 | } 365 | 366 | public void update(Observable o,Object arg) 367 | { 368 | if(selectedDownload!=null && selectedDownload.equals(o)) 369 | 370 | SwingUtilities.invokeLater(new Runnable() { 371 | 372 | @Override 373 | public void run() { 374 | updateButtons(); 375 | } 376 | }); 377 | } 378 | 379 | /* public static String returnString() 380 | { 381 | return selectedDownload.fileNameNew; 382 | } 383 | */ 384 | 385 | 386 | public static void main(String[] args) 387 | { 388 | 389 | SwingUtilities.invokeLater(new Runnable() { 390 | 391 | @Override 392 | public void run() { 393 | DownloadManager manager=new DownloadManager(); 394 | manager.setVisible(true); 395 | 396 | 397 | 398 | } 399 | }); 400 | 401 | 402 | 403 | 404 | /*String movingDirectory=DownloadManager.getTargetDirectory(); 405 | System.out.println(currentDirectory); 406 | System.out.println(movingDirectory); 407 | */ 408 | 409 | 410 | 411 | 412 | 413 | } 414 | 415 | 416 | 417 | 418 | } 419 | 420 | 421 | 422 | 423 | 424 | -------------------------------------------------------------------------------- /df: -------------------------------------------------------------------------------- 1 | diff --git a/build/classes/download/manager/Download.class b/build/classes/download/manager/Download.class 2 | index 194cc89..8440bf9 100644 3 | Binary files a/build/classes/download/manager/Download.class and b/build/classes/download/manager/Download.class differ 4 | diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml 5 | index 1071aad..e326a28 100644 6 | --- a/nbproject/private/private.xml 7 | +++ b/nbproject/private/private.xml 8 | @@ -11,6 +11,8 @@ 9 |  10 |  11 |  12 | -  13 | +  14 | + file:/F:/Projects/My%20Projects/Networking%20Lab/Download_Manager/src/download/manager/abc.java 15 | +  16 |  17 |  18 | diff --git a/src/download/manager/Download.java b/src/download/manager/Download.java 19 | index 797bca7..29ba4ea 100644 20 | --- a/src/download/manager/Download.java 21 | +++ b/src/download/manager/Download.java 22 | @@ -9,7 +9,6 @@ package download.manager; 23 | * 24 | * @author NowshadApu 25 | */ 26 | - 27 | import java.io.*; 28 | import java.net.HttpURLConnection; 29 | import java.net.URL; 30 | @@ -18,227 +17,310 @@ import java.util.*; 31 | import java.net.*; 32 | import java.nio.file.Path; 33 |  34 | - 35 | public class Download extends Observable implements Runnable { 36 | -  37 | - private static final int MAX_BUFFER_SIZE=1024; 38 | - public static final String STATUSES[]={"Downloading","Paused","Complete","Cancelled","Error"}; 39 | -  40 | - public static final int DOWNLOADING=0; 41 | - public static final int PAUSED=1; 42 | - public static final int COMPLETE=2; 43 | - public static final int CANCELLED=3; 44 | - public static final int ERROR=4; 45 | -  46 | - private URL url; 47 | - private int size; 48 | - private int downloaded; 49 | - private int status; 50 | -  51 | + 52 | + public static final int MAX_BUFFER_SIZE = 1024; 53 | + public static final String STATUSES[] = {"Downloading", "Paused", "Complete", "Cancelled", "Error"}; 54 | + 55 | + public static final int DOWNLOADING = 0; 56 | + public static final int PAUSED = 1; 57 | + public static final int COMPLETE = 2; 58 | + public static final int CANCELLED = 3; 59 | + public static final int ERROR = 4; 60 | + 61 | + public URL url; 62 | + public int size; 63 | + public int downloaded; 64 | + public int status; 65 | + 66 | public String fileNameNew; 67 | + public int counter1 = 0; 68 |  69 | - public Download(URL url) 70 | - { 71 | - this.url=url; 72 | - size=-1; 73 | - downloaded=0; 74 | - status=DOWNLOADING; 75 | -  76 | + /*The reason for taking two dimensional byte array is to store the byte arrarys in another array. I don't want to read and write sequentially. 77 | + My plan is to read all the byte arrays sequentially or separately and then to write sequentially.  78 | +*/ 79 | + byte buffer1[][] = new byte[100000][MAX_BUFFER_SIZE]; 80 | + byte buffer2[][] = new byte[100000][MAX_BUFFER_SIZE]; 81 | +  82 | + //These two read array are also for storing 'read' information from reading bytes. 83 | + 84 | + int[] read1 = new int[100000]; 85 | + int[] read2 = new int[100000]; 86 | + 87 | + public Download(URL url) { 88 | + this.url = url; 89 | + size = -1; 90 | + downloaded = 0; 91 | + status = DOWNLOADING; 92 | + 93 | download(); 94 | } 95 | - public String getUrl() 96 | - { 97 | - return url.toString(); 98 | - } 99 | -  100 | - public int getSize() 101 | - { 102 | - return size; 103 | - } 104 | -  105 | - public float getProgress() 106 | - { 107 | - return ((float)downloaded/size)*100; 108 | - } 109 | -  110 | - public int getStatus(){ 111 | - return status; 112 | - } 113 | -  114 | - public void pause() 115 | - { 116 | - status=PAUSED; 117 | - stateChanged(); 118 | - } 119 | -  120 | - public void cancel() 121 | - { 122 | - status=CANCELLED; 123 | - stateChanged(); 124 | - } 125 | -  126 | - public void resume() 127 | - { 128 | - status=DOWNLOADING; 129 | - stateChanged(); 130 | - download(); 131 | - } 132 | -  133 | - private void error() 134 | - { 135 | - status=ERROR; 136 | - stateChanged(); 137 | - } 138 |  139 | -  140 | + public String getUrl() { 141 | + return url.toString(); 142 | + } 143 | + 144 | + public int getSize() { 145 | + return size; 146 | + } 147 | + 148 | + public float getProgress() { 149 | + return ((float) downloaded / size) * 100; 150 | + } 151 | + 152 | + public int getStatus() { 153 | + return status; 154 | + } 155 | + 156 | + public void pause() { 157 | + status = PAUSED; 158 | + stateChanged(); 159 | + } 160 |  161 | - private void download() { 162 | - Thread thread=new Thread(this); 163 | + public void cancel() { 164 | + status = CANCELLED; 165 | + stateChanged(); 166 | + } 167 | + 168 | + public void resume() { 169 | + status = DOWNLOADING; 170 | + stateChanged(); 171 | + download(); 172 | + } 173 | + 174 | + public void error() { 175 | + status = ERROR; 176 | + stateChanged(); 177 | + } 178 | + 179 | + public void download() { 180 | + Thread thread = new Thread(this); 181 | thread.start(); 182 | } 183 | -  184 | - public String getFileName(URL url) 185 | - { 186 | - String fileName=url.getFile(); 187 | - return fileName.substring(fileName.lastIndexOf('/')+1); 188 | -  189 | -  190 | + 191 | + public String getFileName(URL url) { 192 | + 193 | + String fileName = url.getFile(); 194 | + return fileName.substring(fileName.lastIndexOf('/') + 1); 195 | + 196 | } 197 | -  198 | - public void run () 199 | - { 200 | - RandomAccessFile file=null; 201 | - InputStream stream=null; 202 | -  203 | - try{ 204 | - HttpURLConnection connection=(HttpURLConnection)url.openConnection(); 205 | - connection.setRequestProperty("Range", "bytes="+downloaded+"-"); 206 | - connection.connect(); 207 | -  208 | - if(connection.getResponseCode()/100!=2) 209 | - { 210 | - error(); 211 | - } 212 | -  213 | - int contentLength=connection.getContentLength(); 214 | - if(contentLength<1) 215 | - error(); 216 | -  217 | - if(size==-1) 218 | - { 219 | - size=contentLength; 220 | - stateChanged(); 221 | - } 222 | -  223 | - fileNameNew=getFileName(url); 224 | - file=new RandomAccessFile(getFileName(url),"rw"); 225 | - file.seek(downloaded); 226 | - //System.out.println(fileNameNew); 227 | -  228 | -  229 | - stream=connection.getInputStream(); 230 | - while(status==DOWNLOADING) 231 | - { 232 | - byte buffer[]; 233 | - if(size-downloaded>MAX_BUFFER_SIZE) 234 | - buffer=new byte[MAX_BUFFER_SIZE]; 235 | - else 236 | - buffer=new byte[size-downloaded]; 237 | -  238 | - int read=stream.read(buffer); 239 | - if(read==-1) 240 | - break; 241 | -  242 | - file.write( buffer,0,read); 243 | - downloaded+=read; 244 | - stateChanged(); 245 | - } 246 | -  247 | - if(status==DOWNLOADING) 248 | - { 249 | - status=COMPLETE; 250 | - stateChanged(); 251 | -  252 | - } 253 | -  254 | -  255 | - String movingDirectory=DownloadManager.targetDirectory; 256 | - System.out.println(movingDirectory); 257 | - /* String currentDirectory=System.getProperty("user.dir"); 258 | -  259 | -  260 | -  261 | -  262 | - // System.out.println(currentDirectory+" "+movingDirectory); 263 | -  264 | - try{ 265 | - 266 | - File afile =new File(currentDirectory+"\\"+getFileName(url)); 267 | - 268 | - if(afile.renameTo(new File(movingDirectory+"\\"+getFileName(url)+afile.getName()))){ 269 | - System.out.println("File is moved successful!"); 270 | - }else{ 271 | - System.out.println("File is failed to move!"); 272 | - } 273 | - 274 | - }catch(Exception e){ 275 | - e.printStackTrace(); 276 | - } 277 | - */ 278 | -  279 | - }catch(Exception e) 280 | - { 281 | - error(); 282 | - } 283 | - finally 284 | - { 285 | - if(file!=null) 286 | - { 287 | -  288 | -  289 | - String movingDirectory=DownloadManager.targetDirectory; 290 | - String currentDirectory=System.getProperty("user.dir"); 291 | -  292 | - try 293 | - { 294 | - file.close(); 295 | -  296 | - try{ 297 | - 298 | - File afile =new File(currentDirectory+"\\"+getFileName(url)); 299 | - 300 | - if(afile.renameTo(new File(movingDirectory+"\\"+getFileName(url)))){ 301 | - System.out.println("File is moved successful!"); 302 | - }else{ 303 | - System.out.println("File is failed to move!"); 304 | - } 305 | - 306 | - }catch(Exception e){ 307 | - e.printStackTrace(); 308 | - } 309 | -  310 | -  311 | -  312 | -  313 | - }catch(Exception e) 314 | - { 315 | - e.printStackTrace(); 316 | - } 317 | - } 318 | -  319 | - if(stream!=null) 320 | - { 321 | - try{ 322 | - stream.close(); 323 | - }catch(Exception e) 324 | - { e.printStackTrace();} 325 | - } 326 | - } 327 | - } 328 | -  329 | - private void stateChanged() { 330 | + RandomAccessFile file = null; 331 | + RandomAccessFile file2 = null; 332 | + InputStream stream = null; 333 | + InputStream stream2 = null; 334 | + int a, b; 335 | + abc ABC; 336 | + 337 | + public void run() { 338 | + 339 | + try { 340 | + 341 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 342 | + 343 | + connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); 344 | + connection.connect(); 345 | + 346 | +  347 | + if (connection.getResponseCode() / 100 != 2) { 348 | + error(); 349 | + } 350 | + 351 | + int contentLength = connection.getContentLength(); 352 | + 353 | + if (contentLength < 1) { 354 | + error(); 355 | + } 356 | + System.out.println("Full Content is " + contentLength); 357 | + 358 | + if (size == -1) { 359 | + size = 10000;  360 | +/*Some of the place in the code I use 10000.It means that I want to download 10000 bytes at the first time. 361 | + Then remaining bytes will be downloaded. 362 | + */ 363 | + 364 | + stateChanged(); 365 | + } 366 | + 367 | + fileNameNew = getFileName(url); 368 | + file = new RandomAccessFile(getFileName(url), "rw"); 369 | + 370 | + file.seek(downloaded); 371 | + 372 | + stream = connection.getInputStream(); 373 | + 374 | + size = 10000; 375 | + downloaded = 0; 376 | +  377 | + /* 378 | + Here I make a function named 'function1' to download the first 10000 bytes.I use 'size' and 'downloaded' as parameter.  379 | + */ 380 | +  381 | + function1(size, downloaded); 382 | + 383 | + System.out.println("Starting of FUNCTION2"); 384 | +  385 | + /* 386 | + 'function2' downloads from 100001(In original,I think 1000001+1215) to remainging bytes. 387 | + */ 388 | + 389 | + int sizeX = contentLength; 390 | + int downloadedX = 10000 + 1215; 391 | + int counter2 = finction2(sizeX, downloadedX); 392 | + 393 | + /* 394 | + From this thread(Main thread) I run another thread from 'abc' class from 'function1'. So, I wait here untill that thread finishes its work. After  395 | + finishing I write the random access file with buffer1(from function1) and buffer2 (from function2). 396 | + */ 397 | +  398 | + ABC.join(); 399 | +  400 | + /* 401 | + From these part I write the file  402 | + equentially . That means, bytes downloaded from function1 and function2 are sequentially merged in the file .  403 | + */ 404 | + 405 | + for (int i = 0; i <= (counter1); ++i) { 406 | + file.write(buffer1[i], 0, read1[i]); 407 | + } 408 | + 409 | + for (int i = 0; i < (counter2); ++i) { 410 | + file.write(buffer2[i], 0, read2[i]); 411 | + } 412 | + 413 | + if (status == DOWNLOADING) { 414 | + System.out.println("Joss!!!"); 415 | + status = COMPLETE; 416 | + stateChanged(); 417 | + 418 | + } 419 | +  420 | + /* 421 | + movingDirectory is the current directory and targerDirectory is what I choose from 'Select Folder' button.If I select nothing then the 422 | + file will'be downloaded in the Project folder(By default). 423 | + */ 424 | + 425 | + String movingDirectory = DownloadManager.targetDirectory; 426 | + System.out.println(movingDirectory); 427 | + 428 | + } catch (Exception e) { 429 | + error(); 430 | + } finally { 431 | + if (file != null) { 432 | +  433 | +  434 | + 435 | + String movingDirectory = DownloadManager.targetDirectory; 436 | + String currentDirectory = System.getProperty("user.dir"); 437 | + 438 | + try { 439 | + file.close(); 440 | + 441 | + try { 442 | +  443 | + //if there is a file created then finally that file will be moved to target directory 444 | + 445 | + File afile = new File(currentDirectory + "\\" + getFileName(url)); 446 | + 447 | + if (afile.renameTo(new File(movingDirectory + "\\" + getFileName(url)))) { 448 | + System.out.println("File is moved successful!"); 449 | + } else { 450 | + System.out.println("File is failed to move!"); 451 | + } 452 | + 453 | + } catch (Exception e) { 454 | + e.printStackTrace(); 455 | + } 456 | + 457 | + } catch (Exception e) { 458 | + e.printStackTrace(); 459 | + } 460 | + } 461 | + 462 | + if (stream != null) { 463 | + try { 464 | + stream.close(); 465 | + } catch (Exception e) { 466 | + e.printStackTrace(); 467 | + } 468 | + } 469 | + } 470 | + } 471 | + 472 | + public void stateChanged() { 473 | setChanged(); 474 | notifyObservers(); 475 | } 476 | -  477 | + 478 | + public void function1(int size, int downloaded) throws IOException { 479 | +  480 | + /* 481 | + Here, new Thread in abc class is started. But value of this 'Download' class is passed to 'abc' class. This is called Pass By Reference. 482 | + So if there is a change in abc class then, the value of this class is changed .  483 | + */ 484 | + 485 | + ABC = new abc(this); 486 | + ABC.start(); 487 | + 488 | + } 489 | + 490 | + public int finction2(int size, int downloaded) throws IOException, InterruptedException { 491 | +  492 | + /* 493 | + Here, new Input Stream, HTTPURL connection, setRequestProperty all are set for this function. 494 | + */ 495 | +  496 | + 497 | + InputStream streamX = null; 498 | + HttpURLConnection connectionX = (HttpURLConnection) url.openConnection(); 499 | + connectionX.setRequestProperty("Range", "bytes=" + 0 + "-"); 500 | + connectionX.connect(); 501 | + streamX = connectionX.getInputStream(); 502 | + 503 | + int counter2 = 0; 504 | + int flag2 = 0; 505 | +  506 | + //As I want to download 10,001-remaining bytes for this URL , so I use 'skip' function for skipping the bytes.  507 | + 508 | + System.out.println("Skipped " + (byte) streamX.skip(10000 + 1215)); 509 | + 510 | + while (status == DOWNLOADING) { 511 | + 512 | + if (size - downloaded > MAX_BUFFER_SIZE) { 513 | + 514 | + System.out.println("First Size-downloaded Part2 " + (size - downloaded)); 515 | + } else { 516 | + 517 | + System.out.println("second Size-downloaded Part 2 " + (size - downloaded)); 518 | + } 519 | + 520 | + read2[counter2] = streamX.read(buffer2[counter2]); 521 | + 522 | + System.out.println("Read-- Part2 " + read2[counter2] + "\n"); 523 | + 524 | + if (read2[counter2] == -1 || (size - downloaded) == 0) { 525 | + System.out.println("It is broken! part2 \n"); 526 | + break; 527 | + } 528 | +  529 | + /* 530 | + This part is to actually seeing the original bytes  531 | + */ 532 | + 533 | + System.out.println("Bytes from Buffer1 ar (Part 2) "); 534 | + for (int i = 0; i < buffer2[counter2].length; ++i) { 535 | + System.out.print(" " + buffer2[counter2][i] + " "); 536 | + } 537 | + System.out.print("\n"); 538 | + 539 | + downloaded += read2[counter2]; 540 | + ++counter2; 541 | + System.out.println("Hiji Biji- Part 2 " + counter2); 542 | + stateChanged(); 543 | + 544 | + } 545 | + 546 | + return counter2; 547 | + 548 | + } 549 | + 550 | } 551 | diff --git a/src/download/manager/DownloadManager.java b/src/download/manager/DownloadManager.java 552 | index 9ec86ba..d5bd420 100644 553 | --- a/src/download/manager/DownloadManager.java 554 | +++ b/src/download/manager/DownloadManager.java 555 | @@ -261,9 +261,9 @@ import javax.swing.event.*; 556 | if(fc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) 557 | { 558 | targetDirectory=String.valueOf(fc.getSelectedFile()); 559 | - //System.out.println(targetDirectory); 560 | +  561 | } 562 | - //selectionFlag=true; 563 | +  564 | } 565 |  566 | public static String getTargetDirectory() 567 | -------------------------------------------------------------------------------- /nbproject/build-impl.xml: -------------------------------------------------------------------------------- 1 | 2 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | Must set src.dir 227 | Must set test.src.dir 228 | Must set build.dir 229 | Must set dist.dir 230 | Must set build.classes.dir 231 | Must set dist.javadoc.dir 232 | Must set build.test.classes.dir 233 | Must set build.test.results.dir 234 | Must set build.classes.excludes 235 | Must set dist.jar 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | Must set javac.includes 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | No tests executed. 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | Must set JVM to use for profiling in profiler.info.jvm 716 | Must set profiler agent JVM arguments in profiler.info.jvmargs.agent 717 | 718 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | Must select some files in the IDE or set javac.includes 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | To run this application from the command line without Ant, try: 995 | 996 | java -jar "${dist.jar.resolved}" 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | Must select one file in the IDE or set run.class 1044 | 1045 | 1046 | 1047 | Must select one file in the IDE or set run.class 1048 | 1049 | 1050 | 1055 | 1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | Must select one file in the IDE or set debug.class 1075 | 1076 | 1077 | 1078 | 1079 | Must select one file in the IDE or set debug.class 1080 | 1081 | 1082 | 1083 | 1084 | Must set fix.includes 1085 | 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 1096 | 1099 | 1100 | This target only works when run from inside the NetBeans IDE. 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | Must select one file in the IDE or set profile.class 1110 | This target only works when run from inside the NetBeans IDE. 1111 | 1112 | 1113 | 1114 | 1115 | 1116 | 1117 | 1118 | 1119 | This target only works when run from inside the NetBeans IDE. 1120 | 1121 | 1122 | 1123 | 1124 | 1125 | 1126 | 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | This target only works when run from inside the NetBeans IDE. 1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1154 | 1157 | 1158 | 1159 | 1160 | 1161 | 1162 | 1163 | 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | 1170 | Must select one file in the IDE or set run.class 1171 | 1172 | 1173 | 1174 | 1175 | 1176 | Must select some files in the IDE or set test.includes 1177 | 1178 | 1179 | 1180 | 1181 | Must select one file in the IDE or set run.class 1182 | 1183 | 1184 | 1185 | 1186 | Must select one file in the IDE or set applet.url 1187 | 1188 | 1189 | 1190 | 1195 | 1196 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 | 1207 | 1208 | 1209 | 1210 | 1211 | 1212 | 1213 | 1214 | 1215 | 1216 | 1217 | 1218 | 1219 | 1220 | 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | 1230 | 1231 | 1232 | 1233 | 1234 | 1239 | 1240 | 1241 | 1242 | 1243 | 1244 | 1245 | 1246 | 1247 | 1248 | 1249 | 1250 | 1251 | 1252 | 1253 | 1254 | 1255 | 1256 | 1257 | 1258 | 1259 | 1260 | 1261 | 1262 | 1263 | 1264 | 1265 | Must select some files in the IDE or set javac.includes 1266 | 1267 | 1268 | 1269 | 1270 | 1271 | 1272 | 1273 | 1274 | 1275 | 1276 | 1277 | 1282 | 1283 | 1284 | 1285 | 1286 | 1287 | 1288 | 1289 | Some tests failed; see details above. 1290 | 1291 | 1292 | 1293 | 1294 | 1295 | 1296 | 1297 | 1298 | Must select some files in the IDE or set test.includes 1299 | 1300 | 1301 | 1302 | Some tests failed; see details above. 1303 | 1304 | 1305 | 1306 | Must select some files in the IDE or set test.class 1307 | Must select some method in the IDE or set test.method 1308 | 1309 | 1310 | 1311 | Some tests failed; see details above. 1312 | 1313 | 1314 | 1319 | 1320 | Must select one file in the IDE or set test.class 1321 | 1322 | 1323 | 1324 | Must select one file in the IDE or set test.class 1325 | Must select some method in the IDE or set test.method 1326 | 1327 | 1328 | 1329 | 1330 | 1331 | 1332 | 1333 | 1334 | 1335 | 1336 | 1337 | 1342 | 1343 | Must select one file in the IDE or set applet.url 1344 | 1345 | 1346 | 1347 | 1348 | 1349 | 1350 | 1355 | 1356 | Must select one file in the IDE or set applet.url 1357 | 1358 | 1359 | 1360 | 1361 | 1362 | 1363 | 1364 | 1369 | 1370 | 1371 | 1372 | 1373 | 1374 | 1375 | 1376 | 1377 | 1378 | 1379 | 1380 | 1381 | 1382 | 1383 | 1384 | 1385 | 1386 | 1387 | 1388 | 1389 | 1390 | 1391 | 1392 | 1393 | 1394 | 1395 | 1396 | 1397 | 1398 | 1399 | 1400 | 1401 | 1402 | 1403 | 1404 | 1405 | 1406 | 1407 | 1408 | 1409 | 1410 | 1411 | 1412 | 1413 | 1414 | --------------------------------------------------------------------------------