menubar
asked on
slow
I am reading standard output from a child process using getInputStream. The standard output is empty. However, it spends a lot of time reading it (30-40 seconds). What is the problem and How do I fix it?
Thanks.
------------
Process p=Runtime.getRuntime().exe c(cmd);
InputStream is = p.getInputStream();
int c;
while((c=is.read())!=-1)Sy stem.out.p rint((char )c);
......
Thanks.
------------
Process p=Runtime.getRuntime().exe
InputStream is = p.getInputStream();
int c;
while((c=is.read())!=-1)Sy
......
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Real class of the object returned from getInputStream() is not java.io.InputStream, but subclass which implements read() in some way--in this case in a way that makes program output available. Typically you don't need to subclass InputStream--there are plenty of subclasses to choose from. And you don't need to know precise class at compile time--what code will be executed in this case determined at runtime, because method is virtual (like most java instance methods).
ASKER
Sorry, I still dont understand.
getInputStream() is a method of Class Process. The doc says:
public abstract InputStream getInputStream()
The link of InputStream leads to java.io.InputStream.
>And you don't
need to know precise class at compile time--what code will be executed in this case determined at runtime,
because method is virtual (like most java instance methods).
Can you explain more? I'll up the points to 50. Thanks.
getInputStream() is a method of Class Process. The doc says:
public abstract InputStream getInputStream()
The link of InputStream leads to java.io.InputStream.
>And you don't
need to know precise class at compile time--what code will be executed in this case determined at runtime,
because method is virtual (like most java instance methods).
Can you explain more? I'll up the points to 50. Thanks.
OK.
It's an example of polymorphism, one of the basic notions of OO programming. When in your code you have variable of base class type (InputStream), it may hold (and will hold, because InputStream is abstract class) a reference to an instance of subclass (e.g. FileInputStream). When you call read(), sequence of bytecodes to be executed determined by actual class of that instance (FileInputStream). Situation is same with function result. You need not know precise type of getInputStream() result. All you need to know is that is subclass of InputStream and must implement method read(). Try to add the following line somewhere after getInputStream() call:
System.out.println(is.getC lass());
Does that make question more clear?
Regards,
Igor Bazarny
It's an example of polymorphism, one of the basic notions of OO programming. When in your code you have variable of base class type (InputStream), it may hold (and will hold, because InputStream is abstract class) a reference to an instance of subclass (e.g. FileInputStream). When you call read(), sequence of bytecodes to be executed determined by actual class of that instance (FileInputStream). Situation is same with function result. You need not know precise type of getInputStream() result. All you need to know is that is subclass of InputStream and must implement method read(). Try to add the following line somewhere after getInputStream() call:
System.out.println(is.getC
Does that make question more clear?
Regards,
Igor Bazarny
ASKER
One last clafication:
what determines the instance of which subclass to be returned by getInputStream()? (Since InputStream has many subclasses, which I suppose can have different methods of their own.)
what determines the instance of which subclass to be returned by getInputStream()? (Since InputStream has many subclasses, which I suppose can have different methods of their own.)
getInputStream() implementation is free to choose which subclass to return.
ASKER
Can you also answer me this question?
The method "read()" is an abstract method inside the abstract class InputStream. API doc says the abstract method has to be implemented in a subclass.
However, I never sub-class InputStream. All I did is
InputStream is = p.getInputStream();
.. is.read()...
Who implemented "read()", how does this work?