# Trouble streaming images with send\_data

**URL:** <https://www.ruby-forum.com/t/trouble-streaming-images-with-send-data/134881>\
**Category:** Rails\
**Created:** [April 15, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/trouble-streaming-images-with-send-data/134881 "2008-04-15T22:55:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 15, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/trouble-streaming-images-with-send-data/134881/1 "2008-04-15T22:55:59Z")

</div>

Hi

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  
it.

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

def sendImage  
biFiltered = BI.new(100, 100, BI::TYPE\_INT\_RGB)  
big = biFiltered.createGraphics()  
$w = 0  
$h = 0  
begin  
filename = “#{RAILS\_ROOT}/public/images/kids.jpg”  
imagefile = java.io.File.new(filename)  
bi = javax.imageio.ImageIO.read(imagefile)  
w = bi.getWidth(nil);  
h = bi.getHeight(nil);  
if bi.getType != BI.TYPE\_INT\_RGB  
bi2 = BI.new(w, h, BI::TYPE\_INT\_RGB)  
big = bi2.createGraphics()  
$stderr.print “after getting graphics”  
big.drawImage(biFiltered, 0, 0, nil)  
bi = b2  
biFiltered = bi  
end  
rescue  
$stderr.print “Couldn’t load file”  
end

```
    big.drawImage(biFiltered,

```

java.awt.geom.AffineTransform.getScaleInstance(2, 2), nil)  
os = java.io.ByteArrayOutputStream.new  
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

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  
problems.

Thanks.  
JB

---

<div class="post-metadata">

**Author:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 15, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/trouble-streaming-images-with-send-data/134881/2 "2008-04-15T22:58:55Z")

</div>

On 15 Apr 2008, at 21:53, jlesleyball wrote:

> Hi  
> 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.

Fred

---

<div class="post-metadata">

**Author:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 15, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/trouble-streaming-images-with-send-data/134881/3 "2008-04-15T23:17:09Z")

</div>

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

JB

---

<div class="post-metadata">

**Author:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 16, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/trouble-streaming-images-with-send-data/134881/4 "2008-04-16T14:36:09Z")

</div>

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?

[http://jira.codehaus.org/browse/JRUBY-929](http://jira.codehaus.org/browse/JRUBY-929)

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

Thanks

---

<div class="post-metadata">

**Author:** ![jlesleyball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@jlesleyball](https://www.ruby-forum.com/u/jlesleyball)\
**Post date:** [April 16, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/trouble-streaming-images-with-send-data/134881/5 "2008-04-16T14:46:46Z")

</div>

This is just a guess based on 5 minutes of looking at the jruby wiki,  
but  
[http://wiki.jruby.org/wiki/Calling\_Java\_from\_JRuby#Ruby\_String\_to\_Java\_Bytes\_and\_back\_again](http://wiki.jruby.org/wiki/Calling_Java_from_JRuby#Ruby_String_to_Java_Bytes_and_back_again)  
might be interesting.

Fred
