Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 2.82 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Java:Stop/Start network Connection
  2. public String download(LatestVersionInfo info) throws FileNotFoundException, XMLStreamException,NoSuchAlgorithmException, MalformedURLException {      
  3.         boolean isCached = checkCache(info, location);
  4.         String filePath = null;
  5.         RandomAccessFile randomAccessFile = null;
  6.         InputStream inputStream = null;
  7.         if (!isCached) {
  8.             try {
  9.                 createLocation();
  10.                 int responseCode = 0;
  11.                 HttpsURLConnection connection = getSSLCertificate(info
  12.                         .getLatestVersionUrl().toString());
  13.                 connection.setRequestMethod("GET");
  14.                 connection.setRequestProperty("Range", "bytes=" + downloaded
  15.                         + "-");
  16.                 connection.connect();
  17.                 responseCode = connection.getResponseCode();
  18.                 if (responseCode / 100 != 2) {
  19.                     error();
  20.                 }
  21.                 int contentLength = connection.getContentLength();
  22.                 if (contentLength < 1) {
  23.                     error();
  24.                 }
  25.                 if (size == -1) {
  26.                     size = contentLength;
  27.                     stateChanged();
  28.                 }
  29.                 randomAccessFile = new RandomAccessFile(
  30.                         getDownLoadPath(info.getLatestVersionUrl()), "rw");
  31.                 randomAccessFile.seek(downloaded);
  32.                 inputStream = connection.getInputStream();
  33.                 while (status == DOWNLOADING) {
  34.                     byte buffer[];
  35.                     float finalSize = size - downloaded;
  36.                     if (finalSize > MAX_BUFFER_SIZE) {
  37.                         buffer = new byte[(int) finalSize];
  38.                     } else {
  39.                         buffer = new byte[MAX_BUFFER_SIZE];
  40.                     }
  41.                     int read = inputStream.read(buffer);
  42.                     if (read == -1)
  43.                         break;
  44.  
  45.                     randomAccessFile.write(buffer, 0, read);
  46.                     downloaded += read;
  47.                     stateChanged();
  48.                 }
  49.  
  50.                 if (status == DOWNLOADING) {
  51.                     status = COMPLETE;
  52.                     stateChanged();
  53.                 }
  54.  
  55.             } catch (Exception e) {
  56.                 e.printStackTrace();
  57.             } finally {
  58.                 try {
  59.                     if (randomAccessFile != null)
  60.                         randomAccessFile.close();
  61.                 } catch (IOException e) {
  62.                     e.printStackTrace();
  63.                 }
  64.                 try {
  65.                     if (inputStream != null)
  66.                         inputStream.close();
  67.                 } catch (IOException e) {
  68.                     e.printStackTrace();
  69.                 }
  70.  
  71.             }          
  72.         }
  73.         return filePath;
  74.     }