views:

49

answers:

1

Is there a way to easily create Java Input/Output streams for unnamed pipes created in native code?

Motivation: I need my own implementation of the Process class. The native code spawns me a new process with subprocess' IO streams redirected to unnamed pipes.

Problem: The file descriptors for correct ends of those pipes make their way to Java. At this point I get stuck as I cannot create a new FileDescriptor which I could pass to FileInput/FileOutput stream.

I have used reflection to get around the problem and got communication with a simple bouncer sub-process running. However I have a notion that it is not the cleanest way to go. Have you used this approach? Do you see any problems with this approach? (the platform will never change)

Searching around the internets revealed similar solution using native code.

Any thoughts before I dive into heavy testing of this approach are very welcome. I would like to give a shot to existing code before writing my own IO stream implementations...

Thank you.

+1  A: 

I have run into this before as well. The only way I know to create FileDescriptor objects is using reflection (or from JNI) to set the private "int" field inside the FileDescriptor class.

But this works fine and is surely how it's done elsewhere in the standard library, so I believe it's as legitimate as you could expect.

Archie