Buffer error...or is it!

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
awilliamsirl
Posts: 4
Joined: Sun Sep 09, 2007 3:43 pm

Buffer error...or is it!

Post by awilliamsirl » Sun Sep 09, 2007 4:04 pm

Hi all,
I'm pretty new to JFreeCharts but I've managed to code a line chart that reads it's values from an XML feed...I'm real proud :-)

Anyways, the method in which I show the chart is I use the buffer stream, i.e.

Code: Select all

response.setContentType("image/png");
...
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 370, 130);
I then refer to the chart from my .html file as:

Code: Select all

<img src="...blah..blah../jfreechart/test"/>
The chart is displayed fine but I'm receiving this error in the logs:

Code: Select all

- java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.setBufferSize(ResponseFacade.java:229)
...
...
Does anyone know what this might be?
On doing a google search for "java.lang.illegalstateexception" "org.apache.catalina.connector.ResponseFacade.setBufferSize"
I found little to help, except maybe the third hit might give an idea of what's happening (I can't paste the url here, I'm new & so this post won't allow it).

I also kept increasing the buffersize of the response, until I stopped at:
response.setBufferSize(100000000);
but still the error shows :-(

Does anyone know a solution or even have a grasp of what's happening here?
Best,
Al

Taqua
JFreeReport Project Leader
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm
Contact:

Post by Taqua » Sun Sep 09, 2007 5:07 pm

In your chart-servlet, make sure that you set the buffer-size before you write any content to the output stream. Once you've written to the output stream, you cannot modify the response-headers or important properties of the response object (like the buffer-size) anymore.

* Depending on the implementation, it may be even illegal to set the buffer-size after ServletResponse.getOutputStream() has been called (no matter of whether something has been written to the stream itself.

** I assume you use a real Servlet for writing the chart and not some javacode embedded in a JSP page. For JSP pages, the rules change and response.getOutputStream may not be called at all.

Have fun,
said Thomas

awilliamsirl
Posts: 4
Joined: Sun Sep 09, 2007 3:43 pm

Error still there :-(

Post by awilliamsirl » Thu Sep 20, 2007 3:31 pm

Hi Taqua,
Thanks for the reply.
Yes I am using a proper servlet, .java file an' all :-)

Just before I call the

Code: Select all

ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 370, 130);
I increased the buffer size,

Code: Select all

response.setBufferSize(15000);
I tried a lot of different values up & beyond 1,000,000, but still had no luck!
No one in my office seems any wiser, is there anything else that might get rid of the exception?

Thanks!!!
Al

jwenting
Posts: 157
Joined: Sat Jul 15, 2006 7:46 am

Post by jwenting » Fri Sep 21, 2007 11:54 am

We use a different mechanism that works quite well:
1) convert the image to a byte array
2) write that byte array to the outputstream

simplified:

Code: Select all

            ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
            encodeToOutputStream(image, baos);
            byte[] content = baos.toByteArray();
            res.setContentType(mimeType);
            res.getOutputStream().write(content);
encodeToOutputStream encodes the BufferedImage to a Stream in the requested bitmap format (JPG, PNG, etc.).
for example (simplified)

Code: Select all

    protected void encodeToOutputStream(BufferedImage image, OutputStream os) throws IOException {
        ImageIO.write(image, PNG, os)
    }
No mucking around with manually managing output buffers and things like that.

awilliamsirl
Posts: 4
Joined: Sun Sep 09, 2007 3:43 pm

Post by awilliamsirl » Fri Sep 28, 2007 7:13 pm

Hi all,
Thanks for the help!!!
Unfortunately it's still not working.
I do not wish to directly write to the output stream (I'm using Velocity templates) and only want to write out the PNG file from my pages (i.e. from my .html pages, not through the .java servlet...but that's not really the problem, I have loads of code working correctly in that manner!).

I'm trying the following:

Code: Select all

            ...
            ByteArrayOutputStream baos = new ByteArrayOutputStream(104096);
            BufferedImage bImage = chart.createBufferedImage(370,130);
            result.put("bimage", bImage);
            ChartUtilities.writeChartAsPNG(baos, chart, 370, 130);
            result.put("baos", baos);
                                                                                    sLog.info("###### baos put in result OK!");
            byte[] content = baos.toByteArray();
                                                                                    sLog.info("###### 3 baos.size()="+baos.size());
                                                                                    sLog.info("###### content.toString().length()="+content.toString().length());
                                                                                    sLog.info("###### content="+content);
            result.put("content", content);
                                                                                    sLog.info("###### content put in result OK!");
        } catch (Exception e) {
            sLog.error(e);
        }
The debugs that are given are:

Code: Select all

###### baos put in result OK!
###### 3 baos.size()=4331
###### content.toString().length()=10
###### content=[B@124e935
###### content put in result OK!
In my pages I have code which outputs the PNG to the browser:

Code: Select all

${result.content}
Where result is a way of accessing the hashmap items in the java code.
Anyway I guess that's not important.

So I'm getting very close with the call to display the contants of baos, when I view the browser pages source I can see about 4K of PNG image characters.
The contents seem to be just a full PNG images characters (i.e. there's no HTML messing it up).
But I'm getting an error in the browser:

Code: Select all

The image “http://pfdl-m08.test.aol.com:48101/cannae/.module/jfreechart/pfweb/quotes/twx” cannot be displayed, because it contains errors.
You will see from the debugs that the

Code: Select all

byte[] content = baos.toByteArray();
seems to not work correctly!

So is this error because I'm not converting the characters correctly?

THANKS AGAIN FOR ALL THE HELP!
Best,
Al

Taqua
JFreeReport Project Leader
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm
Contact:

Post by Taqua » Fri Sep 28, 2007 9:01 pm

well, as the toArray() method is provided by the JDK, chances are rather high that that method is well tested.

To me your problem sounds as if you do not close the file-stream later on. If your physical file-size that is written to the disk does not have the same size as the byte-array (as reported by your debug-statements), then your file-writing code contains errors.

A PNG file is a binary file. If you attempt to convert characters on it, then you are messing up a perfectly good byte-array for no gain. Binary data is no text and running a character-encoding on it will corrupt it.

Have fun,
said Thomas

nishantk04
Posts: 1
Joined: Tue Mar 17, 2015 5:29 am
antibot: No, of course not.

Re: Buffer error...or is it!

Post by nishantk04 » Tue Mar 17, 2015 5:31 am

Hi,
I am facing the same issue. I hope your issue has been resolved. Please help!!!!

Locked