Solved

ULRConnection(), example reading image

Posted on 1998-07-01
9
264 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

730 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