On Wed, Jul 28, 2010 at 6:27 PM, Tim Peierls <t...@peierls.net> wrote:

> I wrote a Restlet Filter to minify application/x-javascript resources in
> afterHandle (using JSMin now, but I'll probably switch to YUI Compressor).
>

As of 2.1-M1 (JSE edition), and using the internal connector only, I'm
getting a BufferOverflowException when I use this code in afterHandle:

Representation result =
    new WriterRepresentation(mediaType, out.size()) {
        public void write(Writer writer) throws IOException {
            out.writeTo(writer);
        }
    };
response.setEntity(result);


where "out" is a CharArrayWriter that has been written to by a YUICompressor
instance and then flushed. It seems to only happen on fairly large files, so
it might be another manifestation of the BufferOverflowException bug that
was fixed back in January or so. I was getting a different kind of exception
in 2.0.x, something about writing to closed socket, but it wasn't preventing
things from working, whereas this is.

As a workaround, I'm using this code:

Representation result =
    new StringRepresentation(out.toString());
response.setEntity(result);


which means creating a copy of the entire minified file in memory, but works
with no warnings.

Another thing that might be worth mentioning is that the Filter this code is
part of is wrapped around a Directory with this code:

    private Restlet getDirectoryOptionallyMinified(String path, boolean
minify) {
        // XXX Can't figure out how to do this with Reference construction,
        //     but it doesn't matter.
        String ref = createClapReference(getClass().getPackage()) + "/" +
path;
        Restlet directory = new Directory(getContext(), ref);
        if (minify) {
            Filter minified = new MinificationFilter();
            minified.setNext(directory);
            directory = minified;
        }
        return directory;
    }

If no one sees any obvious gaffes in this code, I'll try to turn it into a
simple test program that illustrates the problem.

--tim

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2680309

Reply via email to