[OpenMap Users] JarInputReader suggestion

From: alexander sokolov <sokolov@system.ecology.su.se>
Date: Tue Nov 25 2003 - 06:11:58 EST

Hi,

I have a problem reading shape files from a jar using Java Web Start.
Actually it used to work well before the java version 1.4.2. It seems
this problem appears when the path to the jar contains spaces (as
default, the JWS allocates its cache in the directory with spaces at
least on Windows and OS X).

As a workaround we can use

inputStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream(jarEntryName);

instead of

jarFile = new JarFile(jarFileName);
JarEntry entry = jarFile.getJarEntry(jarEntryName);
inputStream = jarFile.getInputStream(entry);

What do you think?

Regards

Alexander

PS Modified JarInputReader.java is in the attachment

// **********************************************************************
//
// <copyright>
//
// BBN Technologies, a Verizon Company
// 10 Moulton Street
// Cambridge, MA 02138
// (617) 873-8000
//
// Copyright (C) BBNT Solutions LLC. All rights reserved.
//
// </copyright>
// **********************************************************************
//
// $Source: /cvs/openmap/openmap/src/openmap/com/bbn/openmap/io/JarInputReader.java,v $
// $RCSfile: JarInputReader.java,v $
// $Revision: 1.1.1.1 $
// $Date: 2003/03/03 21:58:02 $
// $Author: dietrick $
//
// **********************************************************************

package com.bbn.openmap.io;

import java.io.*;
import java.net.*;
import java.util.jar.*;
import com.bbn.openmap.util.Debug;

/**
 * An InputReader to handle entries in a Jar file.
 */
public class JarInputReader extends StreamInputReader {

    /** Where to go to hook up with a resource. */
    protected URL inputURL = null;

    protected JarFile jarFile = null;
    protected String jarFileName = null;
    protected String jarEntryName = null;

    /**
     * Create a JarInputReader win the path to a jar file, and the
     * entry name. The entry name should be a path to the entry from
     * the internal root of the jar file.
     */
    public JarInputReader(String jarFilePath, String jarEntryName)
        throws IOException {

        if (Debug.debugging("binaryfile")) {
            Debug.output("JarInputReader created for " + jarEntryName +
                         " in " + jarFilePath);
        }
        this.jarFileName = jarFilePath;
        this.jarEntryName = jarEntryName;
        reopen();
        name = jarFilePath + "!" + jarEntryName;
    }

    /**
     * Reset the InputStream to the beginning, by closing the
     * current connection and reopening it.
     */
    public void reopen() throws IOException {
        super.reopen();

        Debug.message("binaryfile", "JarInputReader: reopening jarFile " + jarFileName);
        if (jarFile != null) jarFile.close();
        jarFile = null;

// Suggestion by sokolov@ecology.su.se:
// It seems that starting from jdk 1.4.2 the original code
/*
        jarFile = new JarFile(jarFileName);
        JarEntry entry = jarFile.getJarEntry(jarEntryName);
        inputStream = jarFile.getInputStream(entry);
*/
// does not like spaces in the jarFileName (even %20).
// But we can try to get the data using getResourceAsStream,
// So, it can look like
/*
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        inputStream = classLoader.getResourceAsStream(jarEntryName);
*/
// Or we can try to be back compatible and use
        
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        try {
                jarFile = new JarFile(jarFileName);
                JarEntry entry = jarFile.getJarEntry(jarEntryName);
                inputStream = jarFile.getInputStream(entry);
        } catch (java.util.zip.ZipException zipEx) {
                inputStream = classLoader.getResourceAsStream(jarEntryName);
        }
// end of suggestion
        
        if (inputStream == null) {
            Debug.error("JarInputReader: Problem getting input stream for " +
                        jarEntryName + " in " + jarFileName);
        }
    }

    /**
     * Closes the underlying file
     *
     * @exception IOException Any IO errors encountered in accessing the file
     */
    public void close() throws IOException {
        jarFile.close();
        super.close();
    }

}

--
[To unsubscribe to this list send an email to "majdart@bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Tue Nov 25 18:51:28 2003

This archive was generated by hypermail 2.1.8 : Thu May 12 2005 - 07:18:37 EDT