Mark Stephens Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Annoying Java Bugs – JVM bug in BufferedImage code and custom JVM flags

1 min read

When we load up PDF images we use a number of optimisations. One of these is to down-sample the images to reduce their size if possible. So if you have a 10000×10000 pixel image in the PDF file which is displayed as a 10×10 pixel image on the PDF page, we can make it a bit smaller without anyone noticing (and save lots of memory).

To do this, we get the raw Image data from the BufferedImage using this code.

byte[] buffer=null;
int[] intBuffer=null;
int type;
DataBuffer data=ras.getDataBuffer();

The problem is that getDataBuffer() causes a JVM crash on some releases of Java for Linux (not all). I hate these JVM bugs – bugs in my code are much easier to resolve. So we need to allow users to disable this functionality.

The easiest way to do this is to add your own custom JVM flag which will allows users the option to disable this. You can add any custom flag to the JVM command line by adding a D prefix (so -Dorg.jpedal.avoidCleanupRaster=”true” gives me a custom JVM flag).

You can see in my code below we now allow the user to bypass the issue if it is a bug in their Java JVM. Note the value it passes through is ALWAYS string.

/**
* allow user to disable this function and just return raw data
*/
String avoidCleanupRaster=System.getProperty("org.jpedal.avoidCleanupRaster");
if(avoidCleanupRaster!=null && avoidCleanupRaster.toLowerCase().contains("true")){
return ras;
}
 
byte[] buffer=null;
int[] intBuffer=null;
int type;
DataBuffer data=ras.getDataBuffer();

So an annoying bug but it provides an elegant example of how to add your own custom JVM flag. What do you use custom JVM flags for?

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

Mark Stephens Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

2 Replies to “Annoying Java Bugs – JVM bug in BufferedImage code…”

  1. I am using JPedal tool to convert PDF to images. I can see following flag in my JVM which tells JPedal to avoid reducing some large images as this can crash Java – org.jpedal.avoidCleanupRaster=”true”

    In general pdf to image conversion is working, however once in a while during converting large PDF to images it causes tomcat to stop on Linux without any information or logs.It seems it’s simply an OutOfMemoryError that is not being printed. Can anyone please tell me if I select flag value as ‘false’ as below then is that a valid solution. Will it work fine without crashing ?
    org.jpedal.avoidCleanupRaster=”false”

Leave a Reply

Your email address will not be published. Required fields are marked *