Solved

ULRConnection(), example reading image

Posted on 1998-07-01
9
233 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

13 Experts available now in Live!

Get 1:1 Help Now