Source Code : Make your own Java Download Manager

Make your own Java Download Manager


Source Code

<code><br>
import java<b>.</b>awt<b>.</b>BorderLayout<b>;</b><br>
import java<b>.</b>awt<b>.</b>Component<b>;</b><br>
import java<b>.</b>awt<b>.</b>event<b>.</b>ActionEvent<b>;</b><br>
import java<b>.</b>awt<b>.</b>event<b>.</b>ActionListener<b>;</b><br>
import java<b>.</b>awt<b>.</b>event<b>.</b>KeyEvent<b>;</b><br>
import java<b>.</b>awt<b>.</b>event<b>.</b>WindowAdapter<b>;</b><br>
import java<b>.</b>awt<b>.</b>event<b>.</b>WindowEvent<b>;</b><br>
import java<b>.</b>io<b>.</b>File<b>;</b><br>
import java<b>.</b>io<b>.</b>FileOutputStream<b>;</b><br>
import java<b>.</b>io<b>.</b>InputStream<b>;</b><br>
import java<b>.</b>io<b>.</b>RandomAccessFile<b>;</b><br>
import java<b>.</b>net<b>.</b>HttpURLConnection<b>;</b><br>
import java<b>.</b>net<b>.</b>URL<b>;</b><br>
import java<b>.</b>util<b>.</b>ArrayList<b>;</b><br>
import java<b>.</b>util<b>.</b>Observable<b>;</b><br>
import java<b>.</b>util<b>.</b>Observer<b>;</b><br>
import java<b>.</b>util<b>.</b>Properties<b>;</b>
<p>import javax<b>.</b>swing<b>.</b>BorderFactory<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JButton<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JFileChooser<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JFrame<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JLabel<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JMenu<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JMenuBar<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JMenuItem<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JOptionPane<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JPanel<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JProgressBar<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JScrollPane<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JTable<b>;</b><br>
import javax<b>.</b>swing<b>.</b>JTextField<b>;</b><br>
import javax<b>.</b>swing<b>.</b>ListSelectionModel<b>;</b><br>
import javax<b>.</b>swing<b>.</b>event<b>.</b>ListSelectionEvent<b>;</b><br>
import javax<b>.</b>swing<b>.</b>event<b>.</b>ListSelectionListener<b>;</b><br>
import javax<b>.</b>swing<b>.</b>table<b>.</b>AbstractTableModel<b>;</b><br>
import javax<b>.</b>swing<b>.</b>table<b>.</b>TableCellRenderer<b>;</b></p>
<p>public class DownloadManager extends JFrame implements Observer<b> {</b><br>
  private JTextField addTextField<b> =</b> new JTextField<b>(</b>30<b>);</b></p>
<p>  private DownloadsTableModel tableModel<b> =</b> new DownloadsTableModel<b>();</b></p>
<p>  private JTable table<b>;</b></p>
<p>  private JButton pauseButton<b> =</b> new JButton<b>(</b>"Pause"<b>);</b></p>
<p>  private JButton resumeButton<b> =</b> new JButton<b>(</b>"Resume"<b>);</b></p>
<p>  private JButton cancelButton<b>,</b> clearButton<b>;</b></p>
<p>  private JLabel saveFileLabel<b> =</b> new JLabel<b>();</b></p>
<p>  private Download selectedDownload<b>;</b></p>
<p>  private boolean clearing<b>;</b></p>
<p>  public DownloadManager<b>() {</b><br>
  setTitle<b>(</b>"Marvin's Download Manager"<b>);</b><br>
  setSize<b>(</b>640<b>,</b> 480<b>);</b><br>
  addWindowListener<b>(</b>new WindowAdapter<b>() {</b><br>
  public void windowClosing<b>(</b>WindowEvent e<b>) {</b><br>
  System<b>.</b>exit<b>(</b>0<b>);<br>
  }<br>
  });</b><br>
  JMenuBar menuBar<b> =</b> new JMenuBar<b>();</b><br>
  JMenu fileMenu<b> =</b> new JMenu<b>(</b>"File"<b>);</b><br>
  fileMenu<b>.</b>setMnemonic<b>(</b>KeyEvent<b>.</b>VK_F<b>);</b><br>
  JMenuItem fileExitMenuItem<b> =</b> new JMenuItem<b>(</b>"Exit"<b>,</b> KeyEvent<b>.</b>VK_X<b>);</b><br>
  fileExitMenuItem<b>.</b>addActionListener<b>(</b>new ActionListener<b>() {</b><br>
  public void actionPerformed<b>(</b>ActionEvent e<b>) {</b><br>
  System<b>.</b>exit<b>(</b>0<b>);<br>
  }<br>
  });</b><br>
  fileMenu<b>.</b>add<b>(</b>fileExitMenuItem<b>);</b><br>
  menuBar<b>.</b>add<b>(</b>fileMenu<b>);</b><br>
  setJMenuBar<b>(</b>menuBar<b>);</b></p>
<p>  // Set up add panel.<br>
  JPanel addPanel<b> =</b> new JPanel<b>(</b>new BorderLayout<b>());</b></p>
<p>  JPanel targetPanel<b> =</b> new JPanel<b>(</b>new BorderLayout<b>());</b><br>
  targetPanel<b>.</b>add<b>(</b>addTextField<b>,</b> BorderLayout<b>.</b>WEST<b>);</b><br>
  JButton addButton<b> =</b> new JButton<b>(</b>"Add Download"<b>);</b><br>
  addButton<b>.</b>addActionListener<b>(</b>new ActionListener<b>() {</b><br>
  public void actionPerformed<b>(</b>ActionEvent e<b>) {</b><br>
  actionAdd<b>();<br>
  }<br>
  });</b></p>
<p>  targetPanel<b>.</b>add<b>(</b>addButton<b>,</b> BorderLayout<b>.</b>EAST<b>);</b></p>
<p>  JPanel destinationPanel<b> =</b> new JPanel<b>(</b>new BorderLayout<b>());</b><br>
  saveFileLabel<b>.</b>setText<b>(</b>"File:"<b>);</b><br>
  destinationPanel<b>.</b>add<b>(</b>saveFileLabel<b>,</b> BorderLayout<b>.</b>WEST<b>);</b></p>
<p>  JButton saveFileButton<b> =</b> new JButton<b>(</b>"Download To"<b>);</b><br>
  saveFileButton<b>.</b>addActionListener<b>(</b>new ActionListener<b>() {</b><br>
  public void actionPerformed<b>(</b>ActionEvent e<b>) {</b><br>
  actionSaveTo<b>();<br>
  }<br>
  });</b><br>
  destinationPanel<b>.</b>add<b>(</b>saveFileButton<b>,</b> BorderLayout<b>.</b>EAST<b>);</b><br>
  addPanel<b>.</b>add<b>(</b>destinationPanel<b>,</b> BorderLayout<b>.</b>NORTH<b>);</b><br>
  addPanel<b>.</b>add<b>(</b>targetPanel<b>,</b> BorderLayout<b>.</b>SOUTH<b>);</b></p>
<p>  // Set up Downloads table.<br>
<br>
  table<b> =</b> new JTable<b>(</b>tableModel<b>);</b><br>
  table<b>.</b>getSelectionModel<b>().</b>addListSelectionListener<b>(</b>new ListSelectionListener<b>() {</b><br>
  public void valueChanged<b>(</b>ListSelectionEvent e<b>) {</b><br>
  tableSelectionChanged<b>();<br>
  }<br>
  });</b><br>
  table<b>.</b>setSelectionMode<b>(</b>ListSelectionModel<b>.</b>SINGLE_SELECTION<b>);</b></p>
<p>  ProgressRenderer renderer<b> =</b> new ProgressRenderer<b>(</b>0<b>,</b> 100<b>);</b><br>
  renderer<b>.</b>setStringPainted<b>(</b>true<b>);</b> // show progress text<br>
  table<b>.</b>setDefaultRenderer<b>(</b>JProgressBar<b>.</b>class<b>,</b> renderer<b>);</b></p>
<p>  table<b>.</b>setRowHeight<b>((</b>int<b>)</b> renderer<b>.</b>getPreferredSize<b>().</b>getHeight<b>());</b></p>
<p>  JPanel downloadsPanel<b> =</b> new JPanel<b>();</b><br>
  downloadsPanel<b>.</b>setBorder<b>(</b>BorderFactory<b>.</b>createTitledBorder<b>(</b>"Downloads"<b>));</b><br>
  downloadsPanel<b>.</b>setLayout<b>(</b>new BorderLayout<b>());</b><br>
  downloadsPanel<b>.</b>add<b>(</b>new JScrollPane<b>(</b>table<b>),</b> BorderLayout<b>.</b>CENTER<b>);</b></p>
<p>  JPanel buttonsPanel<b> =</b> new JPanel<b>();</b></p>
<p>  pauseButton<b>.</b>addActionListener<b>(</b>new ActionListener<b>() {</b><br>
  public void actionPerformed<b>(</b>ActionEvent e<b>) {</b><br>
  actionPause<b>();<br>
  }<br>
  });</b><br>
  pauseButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  buttonsPanel<b>.</b>add<b>(</b>pauseButton<b>);</b></p>
<p>  resumeButton<b>.</b>addActionListener<b>(</b>new ActionListener<b>() {</b><br>
  public void actionPerformed<b>(</b>ActionEvent e<b>) {</b><br>
  actionResume<b>();<br>
  }<br>
  });</b><br>
  resumeButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  buttonsPanel<b>.</b>add<b>(</b>resumeButton<b>);</b><br>
  cancelButton<b> =</b> new JButton<b>(</b>"Cancel"<b>);</b><br>
  cancelButton<b>.</b>addActionListener<b>(</b>new ActionListener<b>() {</b><br>
  public void actionPerformed<b>(</b>ActionEvent e<b>) {</b><br>
  actionCancel<b>();<br>
  }<br>
  });</b><br>
  cancelButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  buttonsPanel<b>.</b>add<b>(</b>cancelButton<b>);</b><br>
  clearButton<b> =</b> new JButton<b>(</b>"Clear"<b>);</b><br>
  clearButton<b>.</b>addActionListener<b>(</b>new ActionListener<b>() {</b><br>
  public void actionPerformed<b>(</b>ActionEvent e<b>) {</b><br>
  actionClear<b>();<br>
  }<br>
  });</b><br>
  clearButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  buttonsPanel<b>.</b>add<b>(</b>clearButton<b>);</b></p>
<p>  getContentPane<b>().</b>setLayout<b>(</b>new BorderLayout<b>());</b><br>
  getContentPane<b>().</b>add<b>(</b>addPanel<b>,</b> BorderLayout<b>.</b>NORTH<b>);</b><br>
  getContentPane<b>().</b>add<b>(</b>downloadsPanel<b>,</b> BorderLayout<b>.</b>CENTER<b>);</b><br>
  getContentPane<b>().</b>add<b>(</b>buttonsPanel<b>,</b> BorderLayout<b>.</b>SOUTH<b>);<br>
  }</b></p>
<p>  private void actionSaveTo<b>()<br>
  {</b></p>
<p>  JFileChooser jfchooser<b> =</b> new JFileChooser<b>();</b></p>
<p>  jfchooser<b>.</b>setApproveButtonText<b>(</b>"OK"<b>);</b><br>
  jfchooser<b>.</b>setDialogTitle<b>(</b>"Save To"<b>);</b><br>
  jfchooser<b>.</b>setFileSelectionMode<b>(</b>JFileChooser<b>.</b>DIRECTORIES_ONLY<b>);</b></p>
<p>  int result<b> =</b> jfchooser<b>.</b>showOpenDialog<b>(</b>this<b>);</b><br>
  File newZipFile<b> =</b> jfchooser<b>.</b>getSelectedFile<b>();</b><br>
  System<b>.</b>out<b>.</b>println<b>(</b>"importProfile:"<b> +</b> newZipFile<b>);</b><br>
  this<b>.</b>saveFileLabel<b>.</b>setText<b>(</b>newZipFile<b>.</b>getPath<b>());</b></p><p><b>  }</b></p>
<p>  private void actionAdd<b>() {</b><br>
  URL verifiedUrl<b> =</b> verifyUrl<b>(</b>addTextField<b>.</b>getText<b>());</b><br>
  if<b> (</b>verifiedUrl<b> !=</b> null<b>) {</b><br>
  tableModel<b>.</b>addDownload<b>(</b>new Download<b>(</b>verifiedUrl<b>,</b> saveFileLabel<b>.</b>getText<b>()));</b><br>
  addTextField<b>.</b>setText<b>(</b>""<b>);</b> // reset add text field<br>
<b>  }</b> else<b> {</b><br>
  JOptionPane<b>.</b>showMessageDialog<b>(</b>this<b>,</b> "Invalid Download URL"<b>,</b> "Error"<b>,</b><br>
  JOptionPane<b>.</b>ERROR_MESSAGE<b>);<br>
  }<br>
  }</b></p>
<p>  private URL verifyUrl<b>(</b>String url<b>) {</b><br>
  if<b> (!</b>url<b>.</b>toLowerCase<b>().</b>startsWith<b>(</b>"http://"<b>))</b><br>
  return null<b>;</b></p>
<p>  URL verifiedUrl<b> =</b> null<b>;</b><br>
  try<b> {</b><br>
  verifiedUrl<b> =</b> new URL<b>(</b>url<b>);<br>
  }</b> catch<b> (</b>Exception e<b>) {</b><br>
  return null<b>;<br>
  }</b></p>
<p>  if<b> (</b>verifiedUrl<b>.</b>getFile<b>().</b>length<b>() <</b> 2<b>)</b><br>
  return null<b>;</b></p>
<p>  return verifiedUrl<b>;<br>
  }</b></p>
<p>  private void tableSelectionChanged<b>() {</b><br>
  if<b> (</b>selectedDownload<b> !=</b> null<b>)</b><br>
  selectedDownload<b>.</b>deleteObserver<b>(</b>DownloadManager<b>.</b>this<b>);</b></p>
<p>  if<b> (!</b>clearing<b> &&</b> table<b>.</b>getSelectedRow<b>() > -</b>1<b>) {</b><br>
  selectedDownload<b> =</b> tableModel<b>.</b>getDownload<b>(</b>table<b>.</b>getSelectedRow<b>());</b><br>
  selectedDownload<b>.</b>addObserver<b>(</b>DownloadManager<b>.</b>this<b>);</b><br>
  updateButtons<b>();<br>
  }<br>
  }</b></p>
<p>  private void actionPause<b>() {</b><br>
  selectedDownload<b>.</b>pause<b>();</b><br>
  updateButtons<b>();<br>
  }</b></p>
<p>  private void actionResume<b>() {</b><br>
  selectedDownload<b>.</b>resume<b>();</b><br>
  updateButtons<b>();<br>
  }</b></p>
<p>  private void actionCancel<b>() {</b><br>
  selectedDownload<b>.</b>cancel<b>();</b><br>
  updateButtons<b>();<br>
  }</b></p>
<p>  private void actionClear<b>() {</b><br>
  clearing<b> =</b> true<b>;</b><br>
  tableModel<b>.</b>clearDownload<b>(</b>table<b>.</b>getSelectedRow<b>());</b><br>
  clearing<b> =</b> false<b>;</b><br>
  selectedDownload<b> =</b> null<b>;</b><br>
  updateButtons<b>();<br>
  }</b></p>
<p>  private void updateButtons<b>() {</b><br>
  if<b> (</b>selectedDownload<b> !=</b> null<b>) {</b><br>
  int status<b> =</b> selectedDownload<b>.</b>getStatus<b>();</b><br>
  switch<b> (</b>status<b>) {</b><br>
  case Download<b>.</b>DOWNLOADING<b>:</b><br>
  pauseButton<b>.</b>setEnabled<b>(</b>true<b>);</b><br>
  resumeButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  cancelButton<b>.</b>setEnabled<b>(</b>true<b>);</b><br>
  clearButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  break<b>;</b><br>
  case Download<b>.</b>PAUSED<b>:</b><br>
  pauseButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  resumeButton<b>.</b>setEnabled<b>(</b>true<b>);</b><br>
  cancelButton<b>.</b>setEnabled<b>(</b>true<b>);</b><br>
  clearButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  break<b>;</b><br>
  case Download<b>.</b>ERROR<b>:</b><br>
  pauseButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  resumeButton<b>.</b>setEnabled<b>(</b>true<b>);</b><br>
  cancelButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  clearButton<b>.</b>setEnabled<b>(</b>true<b>);</b><br>
  break<b>;</b><br>
  default<b>:</b> // COMPLETE or CANCELLED<br>
  pauseButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  resumeButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  cancelButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  clearButton<b>.</b>setEnabled<b>(</b>true<b>);<br>
  }<br>
  }</b> else<b> {</b><br>
  pauseButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  resumeButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  cancelButton<b>.</b>setEnabled<b>(</b>false<b>);</b><br>
  clearButton<b>.</b>setEnabled<b>(</b>false<b>);<br>
  }<br>
  }</b></p>
<p>  public void update<b>(</b>Observable o<b>,</b> Object arg<b>) {</b><br>
  // Update buttons if the selected download has changed.<br>
  if<b> (</b>selectedDownload<b> !=</b> null<b> &&</b> selectedDownload<b>.</b>equals<b>(</b>o<b>))</b><br>
  updateButtons<b>();<br>
  }</b></p>
<p>  // Run the Download Manager.<br>
  public static void main<b>(</b>String<b>[]</b> args<b>) {</b><br>
  DownloadManager manager<b> =</b> new DownloadManager<b>();</b><br>
  manager<b>.</b>setVisible<b>(</b>true<b>);<br>
  }<br>
}</b></p>
<p>class Download extends Observable implements Runnable<b> {</b><br>
  private static final int MAX_BUFFER_SIZE<b> =</b> 1024<b>;</b></p>
<p>  public static final String STATUSES<b>[] = {</b> "Downloading"<b>,</b> "Paused"<b>,</b> "Complete"<b>,</b> "Cancelled"<b>,</b><br>
  "Error"<b> };</b></p>
<p>  public static final int DOWNLOADING<b> =</b> 0<b>;</b></p>
<p>  public static final int PAUSED<b> =</b> 1<b>;</b></p>
<p>  public static final int COMPLETE<b> =</b> 2<b>;</b></p>
<p>  public static final int CANCELLED<b> =</b> 3<b>;</b></p>
<p>  public static final int ERROR<b> =</b> 4<b>;</b></p>
<p>  private URL url<b>;</b> // download URL<br>
<br>
  private String saveDir<b>;</b> // dir to save<br>
<br>
  private int size<b>;</b> // size of download in bytes<br>
<br>
  private int downloaded<b>;</b> // number of bytes downloaded<br>
<br>
  private int status<b>;</b> // current status of download</p>
<p>  // Proxy information<br>
  public static final boolean proxyRequired<b> =</b> true<b>;</b> // Change for your settings<br>
  public static final String proxyIP<b> =</b> "127.0.0.1"<b>;</b><br>
  public static final String proxyPort<b> =</b> "8080"<b>;</b><br>
  public static final String proxyUsername<b> =</b> "proxyUser"<b>;</b><br>
  public static final String proxyPassword<b> =</b> "proxyPassword"<b>;</b></p>
<p>  // Constructor for Download.<br>
  public Download<b>(</b>URL url<b>,</b> String saveDir<b>) {</b><br>
  this<b>.</b>url<b> =</b> url<b>;</b><br>
  this<b>.</b>saveDir<b> =</b> saveDir<b>;</b><br>
  size<b> = -</b>1<b>;</b><br>
  downloaded<b> =</b> 0<b>;</b><br>
  status<b> =</b> DOWNLOADING<b>;</b></p>
<p>  // Begin the download.<br>
  download<b>();<br>
  }</b></p>
<p>  // Get this download's URL.<br>
  public String getUrl<b>() {</b><br>
  return url<b>.</b>toString<b>();<br>
  }</b></p>
<p>  // Get this download's size.<br>
  public int getSize<b>() {</b><br>
  return size<b>;<br>
  }</b></p>
<p>  // Get this download's progress.<br>
  public float getProgress<b>() {</b><br>
  return<b> ((</b>float<b>)</b> downloaded<b> /</b> size<b>) *</b> 100<b>;<br>
  }</b></p>
<p>  public int getStatus<b>() {</b><br>
  return status<b>;<br>
  }</b></p>
<p>  public void pause<b>() {</b><br>
  status<b> =</b> PAUSED<b>;</b><br>
  stateChanged<b>();<br>
  }</b></p>
<p>  public void resume<b>() {</b><br>
  status<b> =</b> DOWNLOADING<b>;</b><br>
  stateChanged<b>();</b><br>
  download<b>();<br>
  }</b></p>
<p>  public void cancel<b>() {</b><br>
  status<b> =</b> CANCELLED<b>;</b><br>
  stateChanged<b>();<br>
  }</b></p>
<p>  private void error<b>() {</b><br>
  status<b> =</b> ERROR<b>;</b><br>
  stateChanged<b>();<br>
  }</b></p>
<p>  private void download<b>() {</b><br>
  Thread thread<b> =</b> new Thread<b>(</b>this<b>);</b><br>
  thread<b>.</b>start<b>();<br>
  }</b></p>
<p>  // Get file name portion of URL.<br>
  public String getFileName<b>(</b>URL url<b>) {</b><br>
  String fileName<b> =</b> url<b>.</b>getFile<b>();</b><br>
  return fileName<b>.</b>substring<b>(</b>fileName<b>.</b>lastIndexOf<b>(</b>'/'<b>) +</b> 1<b>);<br>
  }</b></p>
<p>  // Download file.<br>
  public void run<b>() {</b><br>
  RandomAccessFile file<b> =</b> null<b>;</b><br>
  InputStream stream<b> =</b> null<b>;</b><br>
  FileOutputStream out<b> =</b> null<b>;</b></p>
<p>  try<b> {</b></p>
<p>  if<b> (</b>proxyRequired<b>){</b><br>
  // This can be put in a menu, updated via interface<br>
  System<b>.</b>out<b>.</b>println<b>(</b>"Setting proxy"<b>);</b><br>
  Properties systemSettings<b> =</b> System<b>.</b>getProperties<b>();</b><br>
  systemSettings<b>.</b>put<b>(</b>"http.proxyHost"<b>,</b> proxyIP<b>);</b><br>
  systemSettings<b>.</b>put<b>(</b>"http.proxyPort"<b>,</b> proxyPort<b>);</b><br>
  System<b>.</b>setProperties<b>(</b>systemSettings<b>);<br>
  }</b></p>
<p>  // Open connection to URL.<br>
  HttpURLConnection connection<b> = (</b>HttpURLConnection<b>)</b> url<b>.</b>openConnection<b>();</b></p>
<p>  // Specify what portion of file to download.<br>
  connection<b>.</b>setRequestProperty<b>(</b>"Range"<b>,</b> "bytes="<b> +</b> downloaded<b> +</b> "-"<b>);</b></p>
<p>  if<b> (</b>proxyRequired<b>){</b><br>
  String encoded<b> =</b> new String<b>(</b>new sun<b>.</b>misc<b>.</b>BASE64Encoder<b>().</b>encode<b>(</b>new String<b>(</b> proxyUsername<b> +</b> ":"<b> +</b> proxyPassword<b>).</b>getBytes<b>()));</b><br>
  connection<b>.</b>setRequestProperty<b>(</b>"Proxy-Authorization"<b>,</b> "Basic "<b> +</b> encoded<b>);<br>
  }</b></p>
<p>  System<b>.</b>out<b>.</b>println<b>(</b>"Going to make connection"<b>);</b><br>
  // Connect to server.<br>
  connection<b>.</b>connect<b>();</b><br>
  System<b>.</b>out<b>.</b>println<b>(</b>"Connected!"<b>);</b></p>
<p>  int responseCode<b> =</b> connection<b>.</b>getResponseCode<b>();</b><br>
  System<b>.</b>out<b>.</b>println<b>(</b>"Response code from server="<b> +</b> responseCode<b>);</b></p>
<p>  // Make sure response code is in the 200 range.<br>
  // 200 - no partial download<br>
  // 206 - supports resume<br>
  //if (responseCode / 100 != 2) {<br>
  if<b> (</b>responseCode<b> ==</b> 200<b> ||</b> responseCode<b> ==</b> 206<b>) {</b><br>
  error<b>();<br>
  }</b></p>
<p>  // Check for valid content length.<br>
  System<b>.</b>out<b>.</b>println<b>(</b>"Content length="<b> +</b> connection<b>.</b>getContentLength<b>());</b><br>
  int contentLength<b> =</b> connection<b>.</b>getContentLength<b>();</b><br>
  if<b> (</b>contentLength<b> <</b> 1<b>) {</b><br>
  error<b>();<br>
  }</b></p>
<p>  /*<br>
  * Set the size for this download if it hasn't been already set.<br>
  */<br>
  if<b> (</b>size<b> == -</b>1<b>) {</b><br>
  size<b> =</b> contentLength<b>;</b><br>
  stateChanged<b>();<br>
  }</b></p>
<p>  // Open file and seek to the end of it.<br>
  file<b> =</b> new RandomAccessFile<b>(</b>getFileName<b>(</b>url<b>),</b> "rw"<b>);</b><br>
  file<b>.</b>seek<b>(</b>downloaded<b>);</b></p>
<p>  System<b>.</b>out<b>.</b>println<b>(</b>"Get InputStream"<b>);</b><br>
  stream<b> =</b> connection<b>.</b>getInputStream<b>();</b><br>
  status<b> =</b> DOWNLOADING<b>;</b><br>
  out<b> =</b> new FileOutputStream<b>(</b>saveDir<b> +</b> File<b>.</b>separator<b> +</b> this<b>.</b>getFileName<b>(</b>url<b>));</b><br>
  while<b> (</b>status<b> ==</b> DOWNLOADING<b>) {</b><br>
  /*<br>
  * Size buffer according to how much of the file is left to download.<br>
  */<br>
  byte buffer<b>[];</b><br>
  if<b> (</b>size<b> -</b> downloaded<b> ></b> MAX_BUFFER_SIZE<b>) {</b><br>
  buffer<b> =</b> new byte<b>[</b>MAX_BUFFER_SIZE<b>];<br>
  }</b> else<b> {</b><br>
  buffer<b> =</b> new byte<b>[</b>size<b> -</b> downloaded<b>];<br>
  }</b></p>
<p>  // Read from server into buffer.<br>
  int read<b> =</b> stream<b>.</b>read<b>(</b>buffer<b>);</b><br>
  if<b> (</b>read<b> == -</b>1<b>)</b><br>
  break<b>;</b></p>
<p>  // Write buffer to file.<br>
  //file.write(buffer, 0, read);<br>
  out<b>.</b>write<b>(</b>buffer<b>,</b> 0<b>,</b> read<b>);</b><br>
  downloaded<b> +=</b> read<b>;</b><br>
  stateChanged<b>();<br>
  }</b></p>
<p>  /*<br>
  * Change status to complete if this point was reached because downloading<br>
  * has finished.<br>
  */<br>
  if<b> (</b>status<b> ==</b> DOWNLOADING<b>) {</b><br>
  status<b> =</b> COMPLETE<b>;</b></p>
<p>  stateChanged<b>();<br>
  }<br>
  }</b> catch<b> (</b>Exception e<b>) {</b><br>
  System<b>.</b>out<b>.</b>println<b>(</b>"Error="<b> +</b> e<b>);</b><br>
  e<b>.</b>printStackTrace<b>();</b><br>
  error<b>();<br>
  }</b> finally<b> {</b></p>
<p>  // Close file.<br>
  if<b> (</b>file<b> !=</b> null<b>) {</b><br>
  try<b> {</b><br>
  // Complete the file<br>
  out<b>.</b>close<b>();</b><br>
  file<b>.</b>close<b>();<br>
  }</b> catch<b> (</b>Exception e<b>) {</b><br>
  e<b>.</b>printStackTrace<b>();<br>
  }<br>
  }</b></p>
<p>  // Close connection to server.<br>
  if<b> (</b>stream<b> !=</b> null<b>) {</b><br>
  try<b> {</b><br>
  stream<b>.</b>close<b>();<br>
  }</b> catch<b> (</b>Exception e<b>) {</b><br>
  e<b>.</b>printStackTrace<b>();<br>
  }<br>
  }<br>
  }<br>
  }</b></p>
<p>  private void stateChanged<b>() {</b><br>
  setChanged<b>();</b><br>
  notifyObservers<b>();<br>
  }<br>
}</b></p>
<p>class DownloadsTableModel extends AbstractTableModel implements Observer<b> {</b><br>
  private static final String<b>[]</b> columnNames<b> = {</b> "URL"<b>,</b> "Size"<b>,</b> "Progress"<b>,</b> "Status"<b> };</b></p>
<p>  private static final Class<b>[]</b> columnClasses<b> = {</b> String<b>.</b>class<b>,</b> String<b>.</b>class<b>,</b> JProgressBar<b>.</b>class<b>,</b><br>
  String<b>.</b>class<b> };</b></p>
<p>  private ArrayList<b><</b>Download<b>></b> downloadList<b> =</b> new ArrayList<b><</b>Download<b>>();</b></p>
<p>  public void addDownload<b>(</b>Download download<b>) {</b><br>
  download<b>.</b>addObserver<b>(</b>this<b>);</b><br>
  downloadList<b>.</b>add<b>(</b>download<b>);</b><br>
  fireTableRowsInserted<b>(</b>getRowCount<b>() -</b> 1<b>,</b> getRowCount<b>() -</b> 1<b>);<br>
  }</b></p>
<p>  public Download getDownload<b>(</b>int row<b>) {</b><br>
  return<b> (</b>Download<b>)</b> downloadList<b>.</b>get<b>(</b>row<b>);<br>
  }</b></p>
<p>  public void clearDownload<b>(</b>int row<b>) {</b><br>
  downloadList<b>.</b>remove<b>(</b>row<b>);</b><br>
  fireTableRowsDeleted<b>(</b>row<b>,</b> row<b>);<br>
  }</b></p>
<p>  public int getColumnCount<b>() {</b><br>
  return columnNames<b>.</b>length<b>;<br>
  }</b></p>
<p>  public String getColumnName<b>(</b>int col<b>) {</b><br>
  return columnNames<b>[</b>col<b>];<br>
  }</b></p>
<p>  public Class getColumnClass<b>(</b>int col<b>) {</b><br>
  return columnClasses<b>[</b>col<b>];<br>
  }</b></p>
<p>  public int getRowCount<b>() {</b><br>
  return downloadList<b>.</b>size<b>();<br>
  }</b></p>
<p>  public Object getValueAt<b>(</b>int row<b>,</b> int col<b>) {</b><br>
  Download download<b> =</b> downloadList<b>.</b>get<b>(</b>row<b>);</b><br>
  switch<b> (</b>col<b>) {</b><br>
  case 0<b>:</b> // URL<br>
  return download<b>.</b>getUrl<b>();</b><br>
  case 1<b>:</b> // Size<br>
  int size<b> =</b> download<b>.</b>getSize<b>();</b><br>
  return<b> (</b>size<b> == -</b>1<b>) ?</b> ""<b> :</b> Integer<b>.</b>toString<b>(</b>size<b>);</b><br>
  case 2<b>:</b> // Progress<br>
  return new Float<b>(</b>download<b>.</b>getProgress<b>());</b><br>
  case 3<b>:</b> // Status<br>
  return Download<b>.</b>STATUSES<b>[</b>download<b>.</b>getStatus<b>()];<br>
  }</b><br>
  return ""<b>;<br>
  }</b></p>
<p>  public void update<b>(</b>Observable o<b>,</b> Object arg<b>) {</b><br>
  int index<b> =</b> downloadList<b>.</b>indexOf<b>(</b>o<b>);</b><br>
  fireTableRowsUpdated<b>(</b>index<b>,</b> index<b>);<br>
  }<br>
}</b></p>
<p>class ProgressRenderer extends JProgressBar implements TableCellRenderer<b> {</b><br>
  public ProgressRenderer<b>(</b>int min<b>,</b> int max<b>) {</b><br>
  super<b>(</b>min<b>,</b> max<b>);<br>
  }</b></p>
<p>  public Component getTableCellRendererComponent<b>(</b>JTable table<b>,</b> Object value<b>,</b> boolean isSelected<b>,</b><br>
  boolean hasFocus<b>,</b> int row<b>,</b> int column<b>) {</b><br>
  setValue<b>((</b>int<b>) ((</b>Float<b>)</b> value<b>).</b>floatValue<b>());</b><br>
  return this<b>;<br>
  }</b></p></code><p><code><b>}</b></code></p>