Results 1 to 2 of 2
  1. #1
    peteosmith is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default SwingWorker problem

    Hi,
    I am trying to download from a URL from my website an mp3, while updating a progress bar.

    Please see below section of code - some of it may seem irrelevant such as System.out.println's but I am trying to see what it is doing.
    The local file IS created, it does read the first set of bytes from the URL but thats it - it also does not hang the application.

    The URL is correct - I have but HIDDEN on purpose for posting on this forum.

    Many Thanks!!

    Java Code:
        private static class numberBytes {
    
            private final int value;
    
            numberBytes(int inValue) {
                this.value = inValue;
            }
        }
    
        private class downloadAlbum extends SwingWorker<Void, numberBytes> {
    
            @Override
            protected Void doInBackground() throws InterruptedException {
                           
                URLConnection conn = null;
                URL url = null;
                InputStream reader = null;
    
                try {
                    url = new URL("HIDDEN");
                } catch (MalformedURLException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
    
                try {
                    url.openConnection();
                } catch (IOException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
    
    
                try {
                    reader = url.openStream();
                } catch (IOException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
    
                FileOutputStream writer;
                writer = null;
                
                try {
                    conn = url.openConnection();
                } catch (IOException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                int fileLength = conn.getContentLength();
                progressBar.setMaximum(fileLength);
    
                try {
                    writer = new FileOutputStream("c:/Users/Pete Smith/test.mp3");
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                byte[] buffer = new byte[153600];
                int totalBytesRead = 0;
                int bytesRead = 0;
    
                System.out.println("Reading mp3");
                try {
                    while (!isCancelled() && (bytesRead = reader.read(buffer)) >0){
                        writer.write(buffer, 0, bytesRead);
                        buffer = new byte[153600];
                        totalBytesRead += bytesRead;
                        System.out.println(totalBytesRead);
                        progressBar.setValue(totalBytesRead);
                        statusLabel.setText("Reading :" + totalBytesRead);
                        System.out.println("file size " + fileLength);
                        publish(new numberBytes (totalBytesRead));
                    }
                } catch (IOException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
       
                try {
                    writer.close();
                } catch (IOException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
    
                System.out.println("DONE!");
                statusLabel.setText("Download Complete!");
    
                return null;
            }
    
            @Override
            protected void process(List<numberBytes> currentBuffer) {
                System.out.println("prcoessing");
    
            }
        }
    Last edited by Fubarable; 08-16-2011 at 12:13 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I've added code tags to help allow your posted code to retain its formatting and be more readable. To learn to do this yourself, please see the link in my signature links below.

    Regarding your file reading and writing -- would you be better off wrapping your input streams in a BufferedInputStream and similar for the output stream? Do you need to flush your stream ever (not sure on this one since you are closing it)? Are you seeing any log messages?

    Regarding your SwingWorker, you are violating its contract by making Swing calls from within the doInBackground thread with your calls to update the progress bar and the statusLabel. Instead do that from the process method.
    Last edited by Fubarable; 08-16-2011 at 12:57 AM.

Similar Threads

  1. SwingWorker
    By 3.14.TR in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-14-2011, 05:53 PM
  2. SwingWorker Problem
    By Berkan in forum Threads and Synchronization
    Replies: 10
    Last Post: 03-11-2010, 04:28 AM
  3. Replies: 18
    Last Post: 10-13-2009, 04:00 PM
  4. Replies: 3
    Last Post: 10-02-2008, 06:48 AM
  5. swingworker
    By musiigedeo in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 01:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •