Details
-
Type:
New Feature
-
Status:
Closed
-
Priority:
Major
-
Resolution: Won't Fix
-
Affects Version/s: JRuby 1.x+
-
Fix Version/s: None
-
Component/s: Java Integration
-
Labels:None
Description
It would be useful to be able to read/write Java input/output streams directly from the backing byte array of Ruby strings. This would avoid unnecessary array allocation/copying (one user reports transmitting byte arrays of several hundred K derived from Ruby strings).
This functionality could be implemented as methods on String, or in some kind of helper class. Here's what the direct approach might look like:
ostream = (some kind of java.io.OutputStream) a_string.to_java_output_stream(ostream) #=> writes the backing bytes to ostream istream = a_string.to_java_input_stream #=> (some kind of java.io.InputStream) aJavaApp.readSomeBytes(istream) #=> app reads stream from backing bytes
This would be easy enough to implement, but some care would need to be taken to ensure the integrity of the data. In the second case, for example, the very easy solution would seem to be to wrap the bytes (from the string's ByteList) in a ByteArrayInputStream. But B-A-I-S exposes the underlying array as a protected field; if/when we decide how to enable protected field/method access, Ruby's lax enforcement of protected/private visibility could lead to problems. (But that's a topic for a different issue - see JRUBY-194.)
A related feature that might be useful is the ability to read in a Ruby string from a Java input stream:
istream = (some kind of java.io.InputStream)
a_string = String.from_java_input_stream(istream)
As I said, there are other ways this functionality could be exposed. But there does seem to be a need/desire for this, especially since we really can't directly expose the backing bytes of RubyString/ByteList.
I imagine this would probably all be post-1.0 functionality.
Thoughts?
-Bill
I like the idea, but this goes pretty far outside the bounds of Ruby features. So basically, it starts to look like we're forking and adding a lot of functionality to core classes that MRI doesn't support. I'd like to avoid that as much as possible.
Better to work on in the short term would be making it easy and performant to wrap a Java stream with a Ruby IO object. That's in greater need than a way to dump strings directly right now.