DownloadThread.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package DokeosAppShare;
  2. import java.net.*;
  3. import java.io.*;
  4. import java.util.*;
  5. /**
  6. * Summary description for DownloadThread.
  7. */
  8. public class DownloadThread extends Thread
  9. {
  10. private List listeners = new LinkedList();
  11. private URL fileURL;
  12. private File fileDest;
  13. private boolean canceling = false;
  14. public DownloadThread(URL fileURL, File fileDest)
  15. {
  16. this.fileURL = fileURL;
  17. this.fileDest = fileDest;
  18. }
  19. public void cancel()
  20. {
  21. canceling = true;
  22. }
  23. public void run()
  24. {
  25. try
  26. {
  27. fireConnecting();
  28. URLConnection connection = fileURL.openConnection();
  29. int max = connection.getContentLength();
  30. InputStream in = connection.getInputStream();
  31. OutputStream out = new FileOutputStream(fileDest);
  32. fireStarted();
  33. fireProgressChange(0, max);
  34. {
  35. int count = 0;
  36. int b = 0;
  37. int readCount = 0;
  38. byte[] buffer = new byte[1024*10];
  39. b = in.read();
  40. while (!canceling && b >= 0)
  41. {
  42. out.write(b);
  43. count += 1;
  44. readCount = in.read(buffer, 0, buffer.length);
  45. out.write(buffer, 0, readCount);
  46. count += readCount;
  47. fireProgressChange(count, max);
  48. b = in.read();
  49. }
  50. }
  51. in.close();
  52. out.close();
  53. if (!canceling)
  54. {
  55. fireDone(fileDest);
  56. }
  57. else
  58. {
  59. //fireCancel(fileDest);
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. fireException(ex);
  65. ex.printStackTrace();
  66. }
  67. }
  68. public void addDownloadProgressEventListener(DownloadProgressEventListener eventListener)
  69. {
  70. synchronized (listeners)
  71. {
  72. listeners.add(eventListener);
  73. }
  74. }
  75. public void removeDownloadProgressEventListener(DownloadProgressEventListener eventListener)
  76. {
  77. synchronized (listeners)
  78. {
  79. listeners.remove(eventListener);
  80. }
  81. }
  82. protected void fireConnecting()
  83. {
  84. Object[] ls;
  85. synchronized (listeners)
  86. {
  87. ls = listeners.toArray();
  88. }
  89. for (int i = 0; i < ls.length; i++)
  90. {
  91. ((DownloadProgressEventListener)ls[i]).connecting();
  92. }
  93. }
  94. protected void fireStarted()
  95. {
  96. Object[] ls;
  97. synchronized (listeners)
  98. {
  99. ls = listeners.toArray();
  100. }
  101. for (int i = 0; i < ls.length; i++)
  102. {
  103. ((DownloadProgressEventListener)ls[i]).started();
  104. }
  105. }
  106. protected void fireProgressChange(int progress, int max)
  107. {
  108. Object[] ls;
  109. synchronized (listeners)
  110. {
  111. ls = listeners.toArray();
  112. }
  113. for (int i = 0; i < ls.length; i++)
  114. {
  115. ((DownloadProgressEventListener)ls[i]).progressChange(progress, max);
  116. }
  117. }
  118. protected void fireDone(File fileDest) throws Exception
  119. {
  120. Object[] ls;
  121. synchronized (listeners)
  122. {
  123. ls = listeners.toArray();
  124. }
  125. for (int i = 0; i < ls.length; i++)
  126. {
  127. ((DownloadProgressEventListener)ls[i]).done(fileDest);
  128. }
  129. }
  130. protected void fireException(Exception ex)
  131. {
  132. Object[] ls;
  133. synchronized (listeners)
  134. {
  135. ls = listeners.toArray();
  136. }
  137. for (int i = 0; i < ls.length; i++)
  138. {
  139. ((DownloadProgressEventListener)ls[i]).exception(ex);
  140. }
  141. }
  142. }