10 Replies Latest reply: Aug 7, 2009 11:54 AM by DrClap RSS

    Does this make sense? url.openStream

    807588
      Does it make sense to get a picture and convert it to an image this way? Is there a way to see if it exists before I pull down the file? It seems like this isn't working as intended. The backup picture does not show up, and I get a lot of red. "org.eclipse.swt.SWTException: Failed to execute runnable (org.eclipse.swt.SWTException: Unsupported or unrecognized format)" I added another catch:
      catch (Exception e) {
                     
                     imageArray[i] = backupImage;
                     e.printStackTrace();
                }
      But ideally I want it to be more graceful and not throw a hard error if the picture doesn't exist.
      private void getRecentPictures()
           {
                int count = recentNames.size();
                imageArray = new Image[count];
                int i = 0;
                
                while(i<count)
                {
                     URL url;
                     try {
                          String sURL = "http://zservices.nwie.net/photos/"+recentNames.elementAt(i).toString()+".jpg";
                          System.out.println(sURL);
                          url = new URL(sURL);
                          InputStream is = url.openStream();
                          Image image = new Image(Display.getCurrent(), is);
                          imageArray[i] = image;
                          
                     } catch (MalformedURLException e) {
                          
                          imageArray[i] = backupImage;
                          e.printStackTrace();
                     }
                 catch (IOException e) {
                     
                     imageArray[i] = backupImage;
                     e.printStackTrace();
                }     
                 i++;
                }
                
                
                
                
           }
      Edited by: sarcasteak on Aug 7, 2009 9:04 AM

      Edited by: sarcasteak on Aug 7, 2009 9:05 AM
        • 1. Re: Does this make sense? url.openStream
          abillconsl
          Is there a way to see if it exists before I pull down the file? It seems like this isn't working as intended.
          URL url;
          try {
               String sURL = "http://zservices.nwie.net/photos/"+recentNames.elementAt(i).toString()+".jpg";
                System.out.println(sURL);
                url = new URL(sURL);
                  System.out.println("Got the URL!");  // added
                InputStream is = url.openStream();
                  System.out.println("Got the stream open");  // added
                Image image = new Image(Display.getCurrent(), is);
                imageArray[i] = image;
           } catch (MalformedURLException e) {
          The backup picture does not show up, and I get a lot of red.
          So you did get the image - it's just not right?
          • 2. Re: Does this make sense? url.openStream
            807588
            Sorry, by red I meant error. It just throws an exception that the file is not in the expected format. I was just wondering if there was an easier way to deal with non-existing files with url.openString then surrounding it in a try/catch and waiting for it to blow up. I get it to work by using a general Exception and setting the picture to a default picture, but I was wondering if there was an easier way to deal with it.

            Edited by: sarcasteak on Aug 7, 2009 9:12 AM
            • 3. Re: Does this make sense? url.openStream
              796447
              sarcasteak wrote:
              Sorry, by red I meant error. It just throws an exception that the file is not in the expected format. I was just wondering if there was an easier way to deal with non-existing files...
              It doesn't sound like it was non-existing, it sounds like it existed, but just like the exception says, it is not the expected format. So I suppose you'll need to catch and handle that exception too (SWTException, or whatever it was).

              Are you sure that resource is of the right format that the API can handle?

              P.S. Don't start acting like a complete dweeb again like you did last month:
              [http://forums.sun.com/thread.jspa?threadID=5396840]
              • 4. Re: Does this make sense? url.openStream
                DrClap
                If you're getting data across the network then you should expect to have to deal with exceptions. Networks are just exceptions waiting to happen. So if your question is just aimed at finding something which doesn't involve catching exceptions, you aren't going to find anything.
                • 5. Re: Does this make sense? url.openStream
                  807588
                  warnerja wrote:
                  sarcasteak wrote:
                  Sorry, by red I meant error. It just throws an exception that the file is not in the expected format. I was just wondering if there was an easier way to deal with non-existing files...
                  It doesn't sound like it was non-existing, it sounds like it existed, but just like the exception says, it is not the expected format. So I suppose you'll need to catch and handle that exception too (SWTException, or whatever it was).

                  Are you sure that resource is of the right format that the API can handle?
                  It seems that the error is occurring because of these lines
                  InputStream is = url.openStream();
                                      Image image = new Image(Display.getCurrent(), is);
                  I am attempting to assign a new image to the input stream, and because it contains a value that is not a picture (nothing), it throws the exception. So basically I just need to do a check on the input stream to see whether or not it has a valid value, if not I will assign it to the other picture. This was the information I was looking for, I guess I was assuming that the error was occuring in a different location.

                  So I guess the big question I have is, how can I tell that the input stream has the right value in it before assigning it as an image?

                  Edited by: sarcasteak on Aug 7, 2009 10:15 AM
                  • 6. Re: Does this make sense? url.openStream
                    DrClap
                    sarcasteak wrote:
                    So I guess the big question I have is, how can I tell that the input stream has the right value in it before assigning it as an image?
                    Why do you want to do that? You already have something built in which is throwing an exception if it isn't a valid image (or whatever the problem is). There's no reason to attempt to duplicate that logic in your own code.
                    • 7. Re: Does this make sense? url.openStream
                      807588
                      DrClap wrote:
                      sarcasteak wrote:
                      So I guess the big question I have is, how can I tell that the input stream has the right value in it before assigning it as an image?
                      Why do you want to do that? You already have something built in which is throwing an exception if it isn't a valid image (or whatever the problem is). There's no reason to attempt to duplicate that logic in your own code.
                      I was hoping that it would be more graceful to do something like
                      if (inputStream.isOfTypeImageData)
                      {
                      image = new Image(display, inputStream);
                      }
                      else
                      {
                      image = imageBackup;
                      }
                      So instead of waiting for it to blow up and fixing the damage, I could avoid it in the first place. I am not really sure what the affect on the program would be if I could do one over the other, though. I am assuming that you should avoid exceptions when you can, and only use them where they are necessary.

                      Am I looking at it wrong?

                      Edited by: sarcasteak on Aug 7, 2009 11:23 AM
                      • 8. Re: Does this make sense? url.openStream
                        DrClap
                        I think you're looking at it wrong. All you have is an InputStream, so of course there isn't any "isThisAValidImageThatSuchAndSuchAComponentWillHandle" method on it. You have to ask the component itself whether it can handle the contents of the stream.

                        That might involve reading the whole contents of the stream, which could be very large. And then you couldn't pass the InputStream to the component because you would have consumed it already, so you would have to download it again. Or store it somewhere for reuse. Both would be a waste of resources.

                        So yeah, avoid exceptions when you can. In this case I don't think you can or should. Just feed the InputStream to the component and if it throws an exception, deal with it.
                        • 9. Re: Does this make sense? url.openStream
                          807588
                          DrClap wrote:
                          I think you're looking at it wrong. All you have is an InputStream, so of course there isn't any "isThisAValidImageThatSuchAndSuchAComponentWillHandle" method on it. You have to ask the component itself whether it can handle the contents of the stream.

                          That might involve reading the whole contents of the stream, which could be very large. And then you couldn't pass the InputStream to the component because you would have consumed it already, so you would have to download it again. Or store it somewhere for reuse. Both would be a waste of resources.

                          So yeah, avoid exceptions when you can. In this case I don't think you can or should. Just feed the InputStream to the component and if it throws an exception, deal with it.
                          Okay, thank you.
                          • 10. Re: Does this make sense? url.openStream
                            abillconsl
                            I don't think that I can add anything meaningful to what was already stated. Except that, after more and more posts, it appears to me that you have been looking at all Exceptions as ERROR conditions. This is not the case - some are and some are not. While there are those who use exceptional conditions to do the work that logical conditioning can often do, neither this extreme, nor the other extreme of thinking that all Exceptions are ERRORS, so to speak, is really correct. Joshua Bloch "Effective Java", has a section of proper use of Exceptions, that is worthwhile reading.

                            ~Bill