Changeset 604 for AE/installer2/src/net/oni2/aeinstaller/backend/network
- Timestamp:
- Jan 12, 2013, 11:48:33 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
AE/installer2/src/net/oni2/aeinstaller/backend/network/FileDownloader.java
r591 r604 94 94 t.start(); 95 95 state = EState.RUNNING; 96 updateStatus(downloaded, size); 96 97 } 97 98 } … … 107 108 else 108 109 state = EState.RUNNING; 110 updateStatus(downloaded, size); 109 111 } 110 112 } … … 114 116 */ 115 117 public synchronized void stop() { 116 state = EState.INTERRUPTED; 117 try { 118 t.join(); 119 } catch (InterruptedException e) { 120 // TODO Auto-generated catch block 121 e.printStackTrace(); 122 } 123 updateStatus(0, 1); 124 t = null; 125 if (target.exists()) 126 target.delete(); 118 if (state != EState.FINISHED) { 119 state = EState.INTERRUPTED; 120 if (t != null) { 121 try { 122 t.join(); 123 } catch (InterruptedException e) { 124 // TODO Auto-generated catch block 125 e.printStackTrace(); 126 } 127 t = null; 128 } 129 updateStatus(0, 1); 130 if (target.exists()) 131 target.delete(); 132 } 127 133 } 128 134 … … 147 153 e1.printStackTrace(); 148 154 state = EState.ERROR; 155 updateStatus(downloaded, fileLength); 149 156 return; 150 157 } 151 158 152 while (downloaded < fileLength) { 153 switch (state) { 154 case ERROR: 155 updateStatus(downloaded, fileLength); 156 return; 157 case PAUSED: 158 try { 159 Thread.sleep(100); 160 } catch (InterruptedException e) { 161 e.printStackTrace(); 162 } 163 break; 164 case INTERRUPTED: 165 return; 166 case RUNNING: 167 BufferedInputStream input = null; 168 try { 169 URLConnection connection = url.openConnection(); 170 if (downloaded == 0) { 171 connection.connect(); 172 strLastModified = connection 173 .getHeaderField("Last-Modified"); 174 strEtag = connection.getHeaderField("ETag"); 175 fileLength = connection.getContentLength(); 176 } else { 177 connection.setRequestProperty("Range", "bytes=" 178 + downloaded + "-"); 179 if (strEtag != null) 180 connection.setRequestProperty("If-Range", 181 strEtag); 182 else 183 connection.setRequestProperty("If-Range", 184 strLastModified); 185 connection.connect(); 159 try { 160 while (downloaded < fileLength) { 161 switch (state) { 162 case ERROR: 163 updateStatus(downloaded, fileLength); 164 return; 165 case PAUSED: 166 try { 167 Thread.sleep(100); 168 } catch (InterruptedException e) { 169 e.printStackTrace(); 186 170 } 187 188 // Setup streams and buffers. 189 input = new BufferedInputStream( 190 connection.getInputStream(), 8192); 191 if (downloaded > 0) 192 outFile.seek(downloaded); 193 byte data[] = new byte[1024]; 194 195 // Download file. 196 int dataRead = 0; 197 int i = 0; 198 while (((dataRead = input.read(data, 0, 1024)) != -1) 199 && (state == EState.RUNNING)) { 200 outFile.write(data, 0, dataRead); 201 downloaded += dataRead; 202 if (downloaded >= fileLength) 203 break; 204 205 i++; 206 if ((i % 10) == 0) 207 updateStatus(downloaded, fileLength); 171 break; 172 case INTERRUPTED: 173 return; 174 case RUNNING: 175 BufferedInputStream input = null; 176 try { 177 URLConnection connection = url.openConnection(); 178 if (downloaded == 0) { 179 connection.connect(); 180 strLastModified = connection 181 .getHeaderField("Last-Modified"); 182 strEtag = connection.getHeaderField("ETag"); 183 fileLength = connection.getContentLength(); 184 } else { 185 connection.setRequestProperty("Range", "bytes=" 186 + downloaded + "-"); 187 if (strEtag != null) 188 connection.setRequestProperty("If-Range", 189 strEtag); 190 else 191 connection.setRequestProperty("If-Range", 192 strLastModified); 193 connection.connect(); 194 } 195 196 // Setup streams and buffers. 197 input = new BufferedInputStream( 198 connection.getInputStream(), 8192); 199 if (downloaded > 0) 200 outFile.seek(downloaded); 201 byte data[] = new byte[1024]; 202 203 // Download file. 204 int dataRead = 0; 205 int i = 0; 206 while (((dataRead = input.read(data, 0, 1024)) != -1) 207 && (state == EState.RUNNING)) { 208 outFile.write(data, 0, dataRead); 209 downloaded += dataRead; 210 if (downloaded >= fileLength) 211 break; 212 213 i++; 214 if ((i % 10) == 0) 215 updateStatus(downloaded, fileLength); 216 } 217 input.close(); 218 } catch (IOException e) { 219 // TODO Auto-generated catch block 220 e.printStackTrace(); 221 try { 222 if (input != null) 223 input.close(); 224 } catch (IOException e2) { 225 e2.printStackTrace(); 226 } 208 227 } 209 input.close(); 210 } catch (IOException e) { 211 // TODO Auto-generated catch block 212 e.printStackTrace(); 213 try { 214 if (input != null) 215 input.close(); 216 } catch (IOException e2) { 217 e2.printStackTrace(); 218 } 219 } 220 break; 221 default: 222 break; 228 break; 229 default: 230 break; 231 } 223 232 } 224 } 225 226 try {227 // Close streams.228 outFile.close();229 } catch (IOException e) {230 e.printStackTrace();233 } finally { 234 try { 235 // Close streams. 236 outFile.close(); 237 } catch (IOException e) { 238 e.printStackTrace(); 239 } 231 240 } 232 241
Note:
See TracChangeset
for help on using the changeset viewer.