jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • JRuby
  • JRUBY-929

Java input/output streams from Ruby string's backing byte array

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: New Feature New Feature
  • Status: Closed Closed
  • Priority: Major 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

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Charles Oliver Nutter added a comment - 22/May/07 10:46 AM

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.

Show
Charles Oliver Nutter added a comment - 22/May/07 10:46 AM 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.
Hide
Permalink
Charles Oliver Nutter added a comment - 23/Oct/07 2:53 PM

Had any more thoughts on this, Bill? It could be something loaded as an extension, providing direct IO stream read/write to JRuby users.

Show
Charles Oliver Nutter added a comment - 23/Oct/07 2:53 PM Had any more thoughts on this, Bill? It could be something loaded as an extension, providing direct IO stream read/write to JRuby users.
Hide
Permalink
Hugh Winkler added a comment - 09/Dec/07 12:03 PM

Just chiming in to support the concept. I may have more concrete feedback after I implement some stream stuff this week.

Show
Hugh Winkler added a comment - 09/Dec/07 12:03 PM Just chiming in to support the concept. I may have more concrete feedback after I implement some stream stuff this week.
Hide
Permalink
Jennifer Ball added a comment - 15/Apr/08 11:01 PM

I guess this was never implemented? This is exactly what I need right now. I'm trying to stream an image using send_data, and my image is on a Java ByteArrayOutputStream. I should get a byte array when calling toByteArray on it, which should be compatible with Ruby String, right? In any case, the following code doesn't work. What is the workaround that the other user you mentioned did to get this to work?

def scaleImage
// Do some Java2D image manipulation into a buffered image, biFiltered and then:
begin
isWritten = javax.imageio.ImageIO.write(biFiltered, "jpeg", os)
rescue
$stderr.print "Couldn't write file"
end

if isWritten
send_data os.toByteArray, :type => "image/jpeg", :disposition => "inline", :filename => "scaledkids.jpg"
end
end

Show
Jennifer Ball added a comment - 15/Apr/08 11:01 PM I guess this was never implemented? This is exactly what I need right now. I'm trying to stream an image using send_data, and my image is on a Java ByteArrayOutputStream. I should get a byte array when calling toByteArray on it, which should be compatible with Ruby String, right? In any case, the following code doesn't work. What is the workaround that the other user you mentioned did to get this to work? def scaleImage // Do some Java2D image manipulation into a buffered image, biFiltered and then: begin isWritten = javax.imageio.ImageIO.write(biFiltered, "jpeg", os) rescue $stderr.print "Couldn't write file" end if isWritten send_data os.toByteArray, :type => "image/jpeg", :disposition => "inline", :filename => "scaledkids.jpg" end end
Hide
Permalink
Charles Oliver Nutter added a comment - 12/May/08 1:24 PM

Jennifer Ball: There is String.from_java_bytes to allow you to turn a byte[] into a Ruby string...that may be the workaround you're looking for, yes?

Show
Charles Oliver Nutter added a comment - 12/May/08 1:24 PM Jennifer Ball: There is String.from_java_bytes to allow you to turn a byte[] into a Ruby string...that may be the workaround you're looking for, yes?
Hide
Permalink
Charles Oliver Nutter added a comment - 04/Feb/09 4:54 AM

I'm going to call this one "won't fix". You can already get acces to a byte array from the String and construct a ByteArrayInput/OutputStream from it, and giving direct access to read and write to the backing store ignores both concurrency issues and the fact that JRuby strings are copy-on-write (so the backing store could be of a different size than the String reports). Because of the messiness involved in exposing the backing store directly to user code, we won't be adding this.

Show
Charles Oliver Nutter added a comment - 04/Feb/09 4:54 AM I'm going to call this one "won't fix". You can already get acces to a byte array from the String and construct a ByteArrayInput/OutputStream from it, and giving direct access to read and write to the backing store ignores both concurrency issues and the fact that JRuby strings are copy-on-write (so the backing store could be of a different size than the String reports). Because of the messiness involved in exposing the backing store directly to user code, we won't be adding this.

People

  • Assignee:
    Charles Oliver Nutter
    Reporter:
    Bill Dortch
Vote (1)
Watch (2)

Dates

  • Created:
    11/May/07 12:48 AM
    Updated:
    27/Oct/09 1:48 PM
    Resolved:
    04/Feb/09 4:54 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.