473,394 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How can I get unbuffered InputStream from Process?

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
5 8508
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Max Rabkin | last post by:
While java's System.in is, according to the javadocs, an InputStream, it is in fact an instance of java.io.BufferedInputStream. Is there any way to have unbuffered access to it without hacking the...
5
by: Rich | last post by:
Does anyone know the correct way of opening an unbuffered output file using STL? I attempted: ofstream myfile("fname.txt", ios::binary); myfile.setbuf(NULL, 0); and I was informed that...
1
by: | last post by:
Is this even possible? I've found some references to specific "unbuffered" type methods that exist in older incarnations of basic_streambuf but not in newer ones. Info please. :P
11
by: Lonnie Princehouse | last post by:
>From the cmd shell on both Windows 2k and XP, I'm getting this weird syntax error in conjunction with the unbuffered flag. It works fine without -u. Has anyone else encountered it? This didn't...
4
by: pank7 | last post by:
hi everyone, I have a program here to test the file IO(actually output) with buffer turned on and off. What I want to see is that there will be obvious differece in time. Here I have an input...
1
by: samtilden | last post by:
I want to print out some tracing messages from Global.asax.cs/Application_BeginRequest(). I easily got: System.Web.HttpContext.Current.Request.RawUrl, and...
4
by: max | last post by:
hey folks, coming from java, new to python. i'm trying to port a java app to python that writes an inputstream to a buffer, then parses the buffer (specifically, i'm getting ID3 tags from mp3s...
1
by: Yang Zhang | last post by:
Hi, is there any way to get unbuffered stdout/stderr without relying on the -u flag to python or calling .flush() on each print (including indirect hacks like replacing sys.stdout with a wrapper...
8
by: zeeshan708 | last post by:
what is the difference between the buffered and unbuffered stream ??(e.g we say that cout is buffered and cerr is unbuffered stream) does that mean that the output sent to buffered stream have to go...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.