Forum: JRuby redirecting java stdout to a JRuby socket

7ad5f543b9592aa423771338c057d7e5?d=identicon&s=25 Gergely Nagy (Guest)
on 2013-11-04 10:39
(Received via mailing list)
Hi Gurus,

Is there a way to redirect java.lang.System.out to a RubySocket's
iostream?

I was trying this with a simple tcp server, where I first had a
$stdout = socket

After which a plain puts() worked and dumped stuff on the other side of
the
socket.
Then, tried to do the same with some java code, calling
java.lang.System.setOut() with these:

a) new_java_out =  java.io.PrintStream.new($stdout.to_outputstream,
true)

b) new_java_out = JRuby.runtime.getOutputStream()

# so that this would work:
System.out.println("hi there")

The problem was I didn't see anything on the other side, debugging into
this a bit, I see some IOException "not opened for writing", with the
following trace:

ChannelStream.bufferedWrite(ByteBuffer) line: 1112
ChannelStream.access$700(ChannelStream, ByteBuffer) line: 62
ChannelStream$OutputStreamAdapter.write(byte[], int, int) line: 1640
PrintStream.write(byte[], int, int) line: 480


Note that a doing the above with a file in place of socket worked fine.
Here's the full source: http://pastie.org/private/rnqqahjb9qeobkcl4ax6a.

Ideas are welcome. Is this a bug in my code or jruby? I was surprised to
see an output stream "not opened for writing".

Thank you.
Gergo
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.