Forums > Java

mxj issue throwing IOexception where Java from CLI is not

June 19, 2006 | 9:50 pm

Hello, the following code works in the command line properly:

[code]

import java.net.*;
import java.io.*;

class test
{
public static void main(String[] args)
{
bang();
}

public static void bang()
{
System.out.println("Attempting to download");

try
{
URL test = new URL("http://www.yahoo.com");

InputStreamReader UrlInputStream= new InputStreamReader(test.openStream());

BufferedReader in = new BufferedReader(UrlInputStream);
String inputLine = "blah";
while((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();

}
catch (MalformedURLException e)
{

System.out.println("It appears as though your URL is malformed");
}
catch(IOException e)
{
e.printStackTrace();
System.out.println("Network error - cannot connect to URL");
}

}
}
[/code]

However, when making an mxj object with similar code, I get this error:

Attempting to download http://www.yahoo.com
? error: java.io.IOException: Stream closed
? error: at java.io.BufferedReader.ensureOpen(BufferedReader.java:97)
? error: at java.io.BufferedReader.readLine(BufferedReader.java:293)
? error: at java.io.BufferedReader.readLine(BufferedReader.java:362)
? error: at imagedownloader.bang(imagedownloader.java:42)
Network error – cannot connect to URL
asfd

the MXJ code:

[code]
import java.net.*;
import java.io.*;
import com.cycling74.max.*;

/**
* @author bbn
*
* shows the basic transaction between the Max patcher and an
* mxj object that wants to process a matrix of data
*/
public class imagedownloader extends MaxObject
{

String UrlString = "http://www.yahoo.com";

public void setUrl(String newUrl)
{
UrlString = newUrl;
}

public void getUrl()
{
post(UrlString);
}

public void bang()
{
post("Attempting to download " + UrlString);

try
{
URL test = new URL(UrlString);

InputStreamReader UrlInputStream= new InputStreamReader(test.openStream());

BufferedReader in = new BufferedReader(UrlInputStream);
String inputLine = "blah";
while((inputLine = in.readLine()) != null)
// post(inputLine);
in.close();

}
catch (MalformedURLException e)
{

post("It appears as though your URL is malformed");
}
catch(IOException e)
{
e.printStackTrace();
post("Network error - cannot connect to URL");
post("asfd");
}
;
}

}
[/code]

Anyone have any ideas?


June 19, 2006 | 10:00 pm

> Anyone have any ideas?

everything looks kosher at first glance, although you will probably
eventually want to start a separate thread to retrieve the data. you
might try that right away to see if retrieiving in a separate thread
eliminates the IOException. (don’t know why it would, but that’s what
I would try first.)

Does the error message show up right away, or is there a delay?

Ben


June 19, 2006 | 10:04 pm

Ben – thanks for the quick reply. There seems to be no delay. I must admit, Im currently at a network im not familiar with. I dont believe I am behind a proxy, but I will try it from home, where I know whats going on network wise – and I am unfiltered. However I see no indication OS wise / or in any other app that I am filtered/proxied.

RE: threads – exactly my goal. However I am very new to java, so thats a bit above my current level of understanding.

Any chance someone could point me in the right direction? The goal of this exercise is to make a threaded image downloader to alleviate the horrible stutters I get with jit.uldl.

Thanks for any insight.


June 19, 2006 | 10:12 pm

To learn about Java and threads I recommend studying one of the
hundreds of Java dev books or one of the thousands of Java dev sites.
For a quick start, here’s a simple pattern that will create and run a
new thread.

Thread t = new Thread( new Runnable() {
public void run()
{
//put your code here
}
} );

t.start();

Sorry about the lousy formatting of the email client code!
Ben

On 6/19/06, vade wrote:
>
> Ben – thanks for the quick reply. There seems to be no delay. I must admit, Im currently at a network im not familiar with. I dont believe I am behind a proxy, but I will try it from home, where I know whats going on network wise – and I am unfiltered. However I see no indication OS wise / or in any other app that I am filtered/proxied.
>
> RE: threads – exactly my goal. However I am very new to java, so thats a bit above my current level of understanding.
>
> Any chance someone could point me in the right direction? The goal of this exercise is to make a threaded image downloader to alleviate the horrible stutters I get with jit.uldl.
>
> Thanks for any insight.
>


June 19, 2006 | 10:15 pm

wow. I suspected it would be much harder than that. Ill give it a go now. Thanks again Ben.


June 19, 2006 | 10:28 pm

On 19 Jun 2006, at 23:12, Ben Nevile wrote:

> For a quick start, here’s a simple pattern that will create and run a
> new thread.

To add to Ben’s comments: an important habit to get into is to
inherit from notifyDeleted() and make sure you clean up your MXJ
object when its instance is deleted from the patcher. Since a single
JVM runs to support all your MXJ objects, you don’t want to leave any
threads in the background if their owning MXJ instance goes away.

(It’s entirely possible that this is benign, but my understanding of
Max’s threading model and the way it interacts with the JNI calls
into Java suggests that you should tidy things up carefully.)

– N.

nick rothwell — composition, systems, performance — http://
http://www.cassiel.com


June 19, 2006 | 10:40 pm

Thanks Nick. I’ll be doing some more in depth reading of the Java docs tonight so hopefully ill get somewhere soon. Thanks again.


June 19, 2006 | 10:58 pm

good point nick.
if you maintained a reference to a potentially running thread you
could do a Thread.join() on it in your notifyDeleted() method.
it might result in a spinning ball depending on the situation but at
least you will be assured that the spawned thread is done executing
before you get rid of the mxj instance.
topher

On Jun 19, 2006, at 15:28 PM, Nick Rothwell wrote:

>
> On 19 Jun 2006, at 23:12, Ben Nevile wrote:
>
>> For a quick start, here’s a simple pattern that will create and run a
>> new thread.
>
> To add to Ben’s comments: an important habit to get into is to
> inherit from notifyDeleted() and make sure you clean up your MXJ
> object when its instance is deleted from the patcher. Since a
> single JVM runs to support all your MXJ objects, you don’t want to
> leave any threads in the background if their owning MXJ instance
> goes away.
>
> (It’s entirely possible that this is benign, but my understanding
> of Max’s threading model and the way it interacts with the JNI
> calls into Java suggests that you should tidy things up carefully.)
>
> – N.
>
>
> nick rothwell — composition, systems, performance — http://
> http://www.cassiel.com
>
>
>


June 20, 2006 | 4:37 am

Hi. It seems the issue was that I was using post() in my try{}.

This code will print out the contents of the URL via System.out.println. I am assuming this has something to due with scheduling, but I have no idea. I tried it on a random hunch, as I wasnt getting anywhere with the threading. Ill have to approach that later and thread it once I get most of my code working as id like.

Thanks again. Ill be back no doubt with other questions.

[code]
public class imagedownloader extends MaxObject
{

String UrlString = "http://www.yahoo.com";

public void setUrl(String newUrl)
{
UrlString = newUrl;
}

public void getUrl()
{
post(UrlString);
}

public void bang()
{
post("Attempting to download " + UrlString);

try
{
URL test = new URL(UrlString);

URLConnection testConnection = test.openConnection();

BufferedReader in = new BufferedReader(
new InputStreamReader(
testConnection.getInputStream()));

String inputLine;

while((inputLine = in.readLine()) != null)
{
// post(inputLine);
System.out.println(inputLine);
}
in.close();

}
catch (MalformedURLException e)
{
post("It appears as though your URL is malformed");
}
catch(IOException e)
{
e.printStackTrace();
post("Network error - cannot connect to URL");
post("asfd");
}
}
}
[/code]


June 20, 2006 | 9:57 am

> Hi. It seems the issue was that I was using post() in my try{}.

try {} just sets up an exception block, so I don’t see why it should
have any effect on posting to the Max world (unlike, say, semaphore
locks which could well cause problems).

Are you sending in a bang from a metro (in overdrive) or manually?

– N.

nick rothwell — composition, systems, performance — http://
http://www.cassiel.com


June 20, 2006 | 12:12 pm

Hi Nick – Im simply banging once, manually, with [button] and overdrive is off.

Could this be a java issue? Here is my version, under OS X 10.4.6.

vade$ java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-112)
Java HotSpot(TM) Client VM (build 1.5.0_06-64, mixed mode, sharing)


June 20, 2006 | 12:45 pm

If you have Jitter and the goal of your patch is actually to download
an image, take a look in the jitter java examples at
jitwebimage-example. Also shows a nice simple example of threading.

If your goal is to grab pages of HTML, you might find Jakarta-Commons
httpClient to be of use. The sample code on its tutorial web pages
shows how very simple it can be to use, and it will hanlde error
checking and transactions with less hard work on your part.

http://jakarta.apache.org/commons/index.html

http://jakarta.apache.org/commons/httpclient/

– Paul

On 6/20/06, vade wrote:
>
> Hi Nick – Im simply banging once, manually, with [button] and overdrive is off.
>
> Could this be a java issue? Here is my version, under OS X 10.4.6.
>
> vade$ java -version
> java version "1.5.0_06"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-112)
> Java HotSpot(TM) Client VM (build 1.5.0_06-64, mixed mode, sharing)
>
>


—– |(*,+,#,=)(#,=,*,+)(=,#,+,*)(+,*,=,#)| —–


June 20, 2006 | 1:51 pm

Ignotus: Thanks – I somehow missed the jitterwebimage.java file. I will look through it with a fine toothed comb. Thanks for saving me some work!


Viewing 13 posts - 1 through 13 (of 13 total)