Reading Javadocs from inside the original zip file

JDK 1.5 Javadocs zip file is 45MB compressed, and 235MB uncompressed. If you’d like to browse its content without uncompress it, you can use following servlet:

 1public class JavadocSampleServlet extends HttpServlet {
 2    private static final String JAVADOC_LOCATION =
 3        "D:/Java/docs/jdk-1_5_0-doc.zip";
 4    protected void doGet(HttpServletRequest req, HttpServletResponse res)
 5        throws ServletException, IOException {
 6        String resource = req.getPathInfo();
 7        if (resource == null || "/".equals(resource))
 8            resource = "/index.html";
 9        URL url = new URL("jar:file:///" + JAVADOC_LOCATION +
10                          "!/docs" + resource);
11        URLConnection conn = url.openConnection();
12        InputStream input = null;
13        OutputStream output = null;
14        try {
15            input = conn.getInputStream();
16            output = res.getOutputStream();
17            int c;
18            while ((c = input.read()) != -1) output.write(c);
19            output.flush();
20        } finally {
21            if (input != null) input.close();
22            if (output != null) output.close();
23        }
24    }
25}

Set up the constant JAVADOC_LOCATION with proper javadoc file path on your disk.
Here’s the servlet mapping:

 1<?xml version="1.0" encoding="UTF-8"?>
 2<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
 3<web-app>
 4    <servlet>
 5        <servlet-name>javadoc</servlet-name>
 6        <servlet-class>
 7            it.newinstance.sample.JavadocSampleServlet
 8        </servlet-class>
 9    </servlet>
10    <servlet-mapping>
11        <servlet-name>javadoc</servlet-name>
12        <url-pattern>/javadoc/*</url-pattern>
13    </servlet-mapping>
14</web-app>

Configure it in your tomcat and go for the URL:
http://localhost:8080/${yourContextName}/javadoc/
and enjoy.

You’ve saved 190MB of disk space ;-)

Notice the new feature of colored sources on my blog: I used this post to test if it works correctly. This weekend I wrote my parser to convert java sources to html. Seems good, isn’t that?


2 Responses to “Reading Javadocs from inside the original zip file”  

  1. 1 Alexandre Rafalovitch

    Or you could try ClassFinder.

    It is open source and serves docs and source (and on-the-fly decompiles) with a very small code base.

    It has been developed a couple of years ago, but I still haven’t found anything to beat it.

  2. 2 luigi

    Perché non facciamo un sito per il browse di codice sorgente?????

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



Calendar

November 2006
M T W T F S S
« Oct   Dec »
 12345
6789101112
13141516171819
20212223242526
27282930  

Follow me

twitter flickr LinkedIn feed

Subscribe by email

Enter your email address:

Archives


Categories

Tag Cloud


Listening