Headless Exception Work-Around

On upgrading a piece of (1.4-dependent) server-side software from 1.4.2_01 to 1.4.2_14, I began seeing errors that looked like:

java.awt.HeadlessException
        at sun.java2d.HeadlessGraphicsEnvironment.getDefaultScreenDevice
            (HeadlessGraphicsEnvironment.java:66)
        at javax.swing.RepaintManager.getVolatileOffscreenBuffer
            (RepaintManager.java:537)
        at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4727)
        at javax.swing.JComponent.paint(JComponent.java:798)
        at edu.vt.cs.collab.cork.impl.servlet.CORKServlet.encodeComponentAsJPEG
            (CORKServlet.java:617)
        at org.isenhour.fin.inc.CEFChartWebView.writeChartImage(CEFChartWebView.java:97)
        at org.isenhour.fin.inc.CEFChartWebView.writeStaticContent(CEFChartWebView.java:84)
        at edu.vt.cs.collab.bridge.DefaultWebViewServlet.doGet(DefaultWebViewServlet.java:95)
        at edu.vt.cs.collab.cork.impl.servlet.CORKServlet.doGet(CORKServlet.java:293)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
        ....


The offending code segment was:

            img = new BufferedImage(sz.width, sz.height,
                BufferedImage.TYPE_INT_RGB);
            g = img.createGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, sz.width, sz.height);
            comp.setBounds(0, 0, sz.width, sz.height);
            comp.paint(g);


where comp was a fairly uncomplicated Swing component. This turned out to be Sun bug 6189824. The work-around noted in the bug report, adding a call to:


    javax.swing.RepaintManager.currentManager(null).setDoubleBufferingEnabled(false);


seems to have fixed the problem.

Checking out Apache’s FeedParser

In searching for a Java library to parse RSS and Atom feeds I ran across FeedParser from the Apache Jakarta Project. From a few quick experiments this looks pretty slick, but is unfortunately marked as “dormant” and shows no evidence of having been updated in a few months.

Presumable as a side-effect of domancy, no archive of the library is availalable. The source can be checked out of the SVN repository, but the URL given on the “anonymous SVN access” page only allows browsing. After a bit of trial-and-error based on other (less dormant) Jakarta projects, the correct URL for grabbing the code with svn appears to be:

http://svn.apache.org/repos/asf/jakarta/commons/dormant/feedparser/trunk/

The complete checkout command is then:

svn checkout http://svn.apache.org/repos/asf/jakarta/commons/dormant/feedparser/trunk/ feedparser

(with no line breaks).

UPDATE: And, of course, within minutes of posting this I find the answer that escaped my earlier googling.

Compressing Socket Data

The java.util.zip package includes classes for compressing and decompressing data using the ZLIB algorithms. The stream classes in the package are well-suited for accessing files and other bounded data, but do not work as well for compressing unbounded, continuous data transmitted over a socket. “Compressing data sent over a socket” describes the underlying issues and illustrates a pair of stream classes that can be used for compressed communication over a socket connection.

Lucene In-Memory Search Example

Apache’s Lucene text search library provides powerful and flexible tools for searching collections of text. While the indexes that Lucene builds are often stored on disk, the libary includes the ability to create an index in memory. This is particularly useful in restricted environments such as unsigned applets and WebStart applications. “Lucene In-Memory Text Search” shows a minimal example of creating and searching such an index.

Testing Strings for Equality

Strings are Objects in Java, but the ability to use literals, along with the String concatenation operator, make them somewhat similar to primitive types. Java also automatically “interns” String literals, meaning that a single String object will be created for a literal that is used multiple times. “String Equality and Interning” describes the effect of interning on String equality testing using the == operator and the equals(Object) method.