trouble streaming images with send_data


I'm doing some server-side graphics with Java2D and I'm trying to
stream the resulting image to the browser using send_data. When I try
to view the image that I streamed, the browser just shows the URL in
the window as a very small image (about .4 kb) when I am using jpeg
format. For png, it tells me that the image has errors and can't load

Here's what I'm doing in the controller:

def sendImage
        biFiltered =, 100, BI::TYPE_INT_RGB)
        big = biFiltered.createGraphics()
        $w = 0
        $h = 0
        filename = "#{RAILS_ROOT}/public/images/kids.jpg"
        imagefile =
        bi =
w = bi.getWidth(nil);
                h = bi.getHeight(nil);
                if bi.getType != BI.TYPE_INT_RGB
                        bi2 =, h, BI::TYPE_INT_RGB)
                        big = bi2.createGraphics()
                        $stderr.print "after getting graphics"
                        big.drawImage(biFiltered, 0, 0, nil)
                        bi = b2
                        biFiltered = bi
                $stderr.print "Couldn't load file"

java.awt.geom.AffineTransform.getScaleInstance(2, 2), nil)
        os =
                isWritten = javax.imageio.ImageIO.write(biFiltered,
"jpeg", os)
                $stderr.print "Couldn't write file"

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

And then in the HTML file, I just point to the sendImage action from
an img tag.

Does anyone have advice on this? Should I not use the
ByteArrayOutputSteam. It seems that others have used this without


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

You're just sending the string "os.toByteArray" rather than the actual
image bytes. I haven't the faintest clue what a ByteArrayOutputSteam,
but for it to work with send_date you're going to have to make it look
like a ruby string (which is just a chunk of bytes), i'm sure there's
some jruby cleverness to assist with that sort of thing.


Thank you, Fred. I will look into the ruby string. And sorry,
everyone, for posting this message twice--accidentally hit send too


According to the following information, I'd have to do some array
allocation/copying or something like that to write Java output streams
directly to the backing byte array of Ruby strings because this
doesn't seem to have been implemented yet?

Could anyone give me a hint on how to perform the workaround?


This is just a guess based on 5 minutes of looking at the jruby wiki,
  might be interesting.