[jsword-svn] common/java/core/org/crosswire/common/util s

jswordcvs at crosswire.org jswordcvs at crosswire.org
Sun Oct 10 15:12:19 MST 2004


Update of /cvs/jsword/common/java/core/org/crosswire/common/util
In directory www.crosswire.org:/tmp/cvs-serv25322/java/core/org/crosswire/common/util

Modified Files:
	NetUtil.java 
Added Files:
	IOUtil.java 
Log Message:
basics of downloadable indexers

Index: NetUtil.java
===================================================================
RCS file: /cvs/jsword/common/java/core/org/crosswire/common/util/NetUtil.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** NetUtil.java	2 Oct 2004 14:01:35 -0000	1.13
--- NetUtil.java	10 Oct 2004 22:12:17 -0000	1.14
***************
*** 636,639 ****
--- 636,669 ----
  
      /**
+      * Get a URL version of the given file.
+      * @param file The File to turn into a URL
+      * @return a URL for the given file
+      */
+     public static URL getURL(File file)
+     {
+         try
+         {
+             return new URL(PROTOCOL_FILE, null, -1, file.getCanonicalPath());
+         }
+         catch (IOException ex)
+         {
+             log.error("Failed to create URL", ex); //$NON-NLS-1$
+             assert false;
+             throw new IllegalArgumentException();
+         }
+     }
+ 
+     /**
+      * A URL version of <code>File.createTempFile()</code>
+      * @return A new temporary URL
+      * @throws IOException If something goes wrong creating the temp URL
+      */
+     public static URL getTemporaryURL(String prefix, String suffix) throws IOException
+     {
+         File tempFile = File.createTempFile(prefix, suffix);
+         return getURL(tempFile);
+     }
+ 
+     /**
       * Check that the directories in the version directory really
       * represent versions.

--- NEW FILE: IOUtil.java ---
package org.crosswire.common.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * .
 * 
 * <p><table border='1' cellPadding='3' cellSpacing='0'>
 * <tr><td bgColor='white' class='TableRowColor'><font size='-7'>
 *
 * Distribution Licence:<br />
 * JSword is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License,
 * version 2 as published by the Free Software Foundation.<br />
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.<br />
 * The License is available on the internet
 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, or by writing to:
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA<br />
 * The copyright to this program is held by it's authors.
 * </font></td></tr></table>
 * @see gnu.gpl.Licence
 * @author Joe Walker [joe at eireneh dot com]
 * @version $Id: IOUtil.java,v 1.1 2004/10/10 22:12:17 joe Exp $
 */
public class IOUtil
{
    /**
     * Prevent instansiation
     */
    private IOUtil()
    {
    }

    /**
     * Unpack a zip file to a given directory
     * @param f The zip file to download
     * @param destdir The directory to unpack up
     * @throws IOException If there is an file error
     */
    public static void unpackZip(File f, URL destdir) throws IOException
    {
        // unpack the zip.
        byte[] dbuf = new byte[4096];
        ZipFile zf = new ZipFile(f);
        Enumeration entries = zf.entries();
        while (entries.hasMoreElements())
        {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            String entrypath = entry.getName();
            String filename = entrypath.substring(entrypath.lastIndexOf('/') + 1);
            URL child = NetUtil.lengthenURL(destdir, filename);
    
            OutputStream dataOut = NetUtil.getOutputStream(child);
            InputStream dataIn = zf.getInputStream(entry);
            for (int count = 0; -1 != (count = dataIn.read(dbuf)); )
            {
                dataOut.write(dbuf, 0, count);
            }
            dataOut.close();
        }
    }

    /**
     * Close the stream whatever without complaining
     * @param out The stream to close
     */
    public static void close(OutputStream out)
    {
        if (null != out)
        {
            try
            {
                out.close();
            }
            catch (IOException ex)
            {
                log.error("close", ex); //$NON-NLS-1$
            }
        }
    }

    /**
     * Close the stream whatever without complaining
     * @param in The stream to close
     */
    public static void close(InputStream in)
    {
        if (null != in)
        {
            try
            {
                in.close();
            }
            catch (IOException ex)
            {
                log.error("close", ex); //$NON-NLS-1$
            }
        }
    }

    /**
     * The log stream
     */
    private static final Logger log = Logger.getLogger(IOUtil.class);
}



More information about the jsword-svn mailing list