By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

How can I get unbuffered InputStream from Process?

P: n/a
Hi.

Is there a way to get an unbuffered InputStream from Process?

I found out that java.lang.Win32Process and java.lang.UNIXProcess both
wraps the stdout with java.io.BufferedInputStream, but I need an
unbuffered one.
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Seong Jin, Cho wrote:
Hi.

Is there a way to get an unbuffered InputStream from Process?

I found out that java.lang.Win32Process and java.lang.UNIXProcess both
wraps the stdout with java.io.BufferedInputStream, but I need an
unbuffered one.


Sounds like you need to use JNI then and use native code.

--
XML is the programmer's duct tape.
Jul 17 '05 #2

P: n/a
On 17 Jun 2004 01:01:29 -0700, Seong Jin, Cho wrote:
Is there a way to get an unbuffered InputStream from Process?

I found out that java.lang.Win32Process and java.lang.UNIXProcess
both wraps the stdout with java.io.BufferedInputStream, but I need
an unbuffered one.


What makes you think it's buffered, and perhaps more importantly, why
should it make any difference to your java application? What actual
problem do you need to solve?

If your observation is that the output of the child process does not
reach the Java parent "immediately", then the buffering is occuring in
the child process itself, not in Java.

If that's the case, you can prevent that from happening by doing this
near the start of the child process, i.e. before writing anything to
stdout:

setvbuf(stdout,NULL,_IONBUF,0);
or
setbuf(stdout,NULL);

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Jul 17 '05 #3

P: n/a
Hello gordon.
What makes you think it's buffered, and perhaps more importantly, why
From Win32Process.java:

stdin_fd = new FileDescriptor();
stdout_fd = new FileDescriptor();
stderr_fd = new FileDescriptor();

handle = create(cmdstr, envstr, path, stdin_fd, stdout_fd, stderr_fd);
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
stdin_stream =
new BufferedOutputStream(new FileOutputStream(stdin_fd));
stdout_stream =
new BufferedInputStream(new FileInputStream(stdout_fd));
stderr_stream =
new FileInputStream(stderr_fd);
return null;
}
});

Also, from solaris version of UNIXProcess.java:

stdin_fd = new FileDescriptor();
stdout_fd = new FileDescriptor();
stderr_fd = new FileDescriptor();

pid = forkAndExec(cmdarray, env, path, stdin_fd, stdout_fd,
stderr_fd);

java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
stdin_stream
= new BufferedOutputStream(new FileOutputStream(stdin_fd));
stdout_inner_stream = new DeferredCloseInputStream(stdout_fd);
stdout_stream = new BufferedInputStream(stdout_inner_stream);
stderr_stream = new DeferredCloseInputStream(stderr_fd);
return null;
}
});

should it make any difference to your java application? What actual
problem do you need to solve?

If your observation is that the output of the child process does not
reach the Java parent "immediately", then the buffering is occuring in
the child process itself, not in Java.

If that's the case, you can prevent that from happening by doing this
near the start of the child process, i.e. before writing anything to
stdout:

setvbuf(stdout,NULL,_IONBUF,0);
or
setbuf(stdout,NULL);

/gordon
Actually, I'm doing the buffering myself, and I don't want to do the
buffering doubly. (I know BufferedInputStream is highly optimized
implementation, but I want to access the underlying byte-array
buffer.)
If your observation is that the output of the child process does not
reach the Java parent "immediately", then the buffering is occuring in
the child process itself, not in Java.


Does the output reach the java parent "immediately" even if
BufferedInputStream wraps the output? If it's the case, why
UNIXProcess and Win32Process not wrap the stderr also?

- Seong Jin
Jul 17 '05 #4

P: n/a
On 17 Jun 2004 18:46:52 -0700, Seong Jin, Cho wrote:
Does the output reach the java parent "immediately" even if
BufferedInputStream wraps the output?
I don't see why a buffer in the receiving process should delay the
reception of data. Once the data has arrived at the
BufferedInputStream it is available to the holder of the
BufferedInputStream.

The situation is different if the buffering occurs in the sending
process, i.e. data isn't sent until enough has been buffered.
If it's the case, why UNIXProcess and Win32Process not wrap the
stderr also?


You'll have to ask Sun about that!

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Jul 17 '05 #5

P: n/a
i20
Hi!

have you found a solution in order to obtain a simple InputStream fro
the Process.getInputStream()?

In fact, I've got the same problem with the BufferedInputStream whic
is return by the Process.getInputStream() method.

The main problem is to obtain the InputStream on which thi
BufferedInputStream is based on. Indeed, this InputStream is in fact
FileInputStream (like the Stream returned by Process.getErrorStream)..
and on a FileInputStream, since it's possible to get a Channel, you wil
be able to deal with NIO stream (java 1.4), in order to obtai
non-blockant threads, which could be easily stopped.

I haven't find any solution yet... and don't see how it will b
possible with the current stable version of Java (1.4.2)

i2
-
i2
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.