Solved

ULRConnection(), example reading image

Posted on 1998-07-01
9
245 Views
Last Modified: 2010-05-18
I have got the connection to an image/jpeg type now I have to draw it.  so far

      url = new URL(getCodeBase(), "current.jpg");
      urlc = url.openConnection();
      urlc.connect();
      System.out.println("Content type is " + urlc.getContentType();

Now I have to read it and build an Image.    
Anyone have an example or something to get me started?
I don't want to use getImage(URL);
Thanks in advance.
0
Comment
Question by:hank1
  • 5
  • 4
9 Comments
 
LVL 6

Expert Comment

by:gadio
ID: 1224323
ude the folowing:

InputStream is = urlc.getInputStream();
// ....
// loop and read all the file through the input stream into a byte array buf[] .
// ....
Image res_image = Toolkit.getDefaultToolkit().createImage( buf );

Hope that helps.
G.
0
 
LVL 1

Author Comment

by:hank1
ID: 1224324
Back at work on Tuesday (6/7?).  Give it a try and award points then if successful.
Thanks.  Have a good 4th - if you're a yankee anyway. :)
0
 
LVL 1

Author Comment

by:hank1
ID: 1224325

Localized InputStream inside paint() to "implement" rewind.  However I get only
one good read. The stream is closed properly (As I understand it, when all references to
the stream are lost, the gc will close it anyway...right?) Next time into paint(),
I get a good InputStream (is not null) but my read throws an IOEx. with "read error".

Attempting to localize the URLConnection was a looping experience.  Paint never stopped.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1224326
hank1, hope you had a happy 4th. :)
You shouldn't do IO operations inside paint, since they take a long time to finish and the UI will be frozen in that time. Instead you should prapare all you need for a paint before (or in another thread. The answer I have written before should be done outside the paint, and also I think that you should use the getInputStream() only once, loop through it, and onl then close it. Give it a try.
Regards,
G.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:hank1
ID: 1224327
appletviewer runs this "fine".  Spin this up in nc4.05 and I get stream closed
exception after one successful read.  Hate to dump code but here it is, stripped to the
minimum.

  URL url;
  URLConnection urlc;
  Image eye;
  InputStream is;

  public void run() {
    while (true) {
      repaint();
      try {
        buildNoCacheImage();
        Thread.sleep(15L * 1000L);  // wait 15 seconds
      } catch (InterruptedException ie) {
        // ignore
      }
    }
  }

  public void buildNoCacheImage() {
    try {
      url = new URL(getCodeBase(), "current.jpg");
      urlc = url.openConnection();
      urlc.setUseCaches(false);
      urlc.connect();
      is = urlc.getInputStream();
      index = 0;
      read = 0;
      while ((read = is.read(b, index, 1000)) != -1) {
        index += read;
      }
      System.out.println("index/read " + index + "/" + read);
      is.close();
      eye = Toolkit.getDefaultToolkit().createImage(b);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  int index = 0;
  int read = 0;

0
 
LVL 1

Author Comment

by:hank1
ID: 1224328
gadio come get your points.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1224329
I think I know what the problem is. I'm a bit tied up at work now, I'll try and give you an answer today or tomorrow the latest.
:-)
0
 
LVL 1

Author Comment

by:hank1
ID: 1224330
No, it works ok.  I was running into server image update activity.
The other previous problem was, like most filters, closing the inputstream
closed the URLConnection.  I just open everything up again.
Thanks for your help.  Come and get'um.
0
 
LVL 6

Accepted Solution

by:
gadio earned 100 total points
ID: 1224331
OK, I'll quote both my answers again:
-------
Use:

      InputStream is = urlc.getInputStream();
      // ....
      // loop and read all the file through the input stream into a byte array buf[] .
      // ....
      Image res_image = Toolkit.getDefaultToolkit().createImage( buf );
--------------
You shouldn't do IO operations inside paint, since they take a long time to finish and the UI will be frozen in that time. Instead you should prapare all you need for a paint before (or in another thread. The answer I have written before should be done outside the paint, and also I think that you should use the getInputStream() only once, loop through it, and onl then close it. Give it a try.
Regards,
 G.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now