Source for gnu.java.net.protocol.jar.Connection

   1: /* Connection - jar url connection for java.net
   2:    Copyright (C) 1999, 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.net.protocol.jar;
  40: 
  41: import java.io.File;
  42: import java.io.FileNotFoundException;
  43: import java.io.FileOutputStream;
  44: import java.io.IOException;
  45: import java.io.InputStream;
  46: import java.net.JarURLConnection;
  47: import java.net.MalformedURLException;
  48: import java.net.ProtocolException;
  49: import java.net.URL;
  50: import java.net.URLConnection;
  51: import java.text.SimpleDateFormat;
  52: import java.util.Date;
  53: import java.util.Hashtable;
  54: import java.util.Locale;
  55: import java.util.jar.JarEntry;
  56: import java.util.jar.JarFile;
  57: import java.util.zip.ZipFile;
  58: 
  59: /**
  60:  * This subclass of java.net.JarURLConnection models a URLConnection via
  61:  * the "jar" protocol.
  62:  *
  63:  * @author Kresten Krab Thorup (krab@gnu.org)
  64:  */
  65: public final class Connection extends JarURLConnection
  66: {
  67:   /**
  68:    * HTTP-style DateFormat, used to format the last-modified header.
  69:    * Lazy initialized since jar files are used during bootstrapping.
  70:    */
  71:   private static SimpleDateFormat dateFormat;
  72: 
  73:   private JarFile jar_file;
  74:   private JarEntry jar_entry;
  75:   private URL jar_url;
  76: 
  77:   public static class JarFileCache
  78:   {
  79:     private static Hashtable<URL, JarFile> cache
  80:       = new Hashtable<URL, JarFile>();
  81:     private static final int READBUFSIZE = 4*1024;
  82: 
  83:     public static synchronized JarFile get (URL url, boolean useCaches)
  84:        throws IOException
  85:     {
  86:       JarFile jf;
  87:       if (useCaches)
  88:         {
  89:           jf = cache.get (url);
  90:           if (jf != null)
  91:             return jf;
  92:         }
  93: 
  94:       if ("file".equals (url.getProtocol()))
  95:         {
  96:           String fn = url.getFile();
  97:           fn = gnu.java.net.protocol.file.Connection.unquote(fn);
  98:           File f = new File (fn);
  99:           jf = new JarFile (f, true, ZipFile.OPEN_READ);
 100:         }
 101:       else
 102:         {
 103:           URLConnection urlconn = url.openConnection();
 104:           InputStream is = urlconn.getInputStream();
 105:           byte[] buf = new byte [READBUFSIZE];
 106:           File f = File.createTempFile ("cache", "jar");
 107:           FileOutputStream fos = new FileOutputStream (f);
 108:           int len = 0;
 109: 
 110:           while ((len = is.read (buf)) != -1)
 111:             {
 112:               fos.write (buf, 0, len);
 113:             }
 114: 
 115:           fos.close();
 116:           // Always verify the Manifest, open read only and delete when done.
 117:           jf = new JarFile (f, true,
 118:                             ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
 119:         }
 120: 
 121:       if (useCaches)
 122:         cache.put (url, jf);
 123: 
 124:       return jf;
 125:     }
 126:   }
 127: 
 128:   protected Connection(URL url)
 129:     throws MalformedURLException
 130:   {
 131:     super(url);
 132:   }
 133: 
 134:   public synchronized void connect() throws IOException
 135:   {
 136:     // Call is ignored if already connected.
 137:     if (connected)
 138:       return;
 139: 
 140:     jar_url = getJarFileURL();
 141:     jar_file = JarFileCache.get (jar_url, useCaches);
 142:     String entry_name = getEntryName();
 143: 
 144:     if (entry_name != null
 145:         && !entry_name.equals (""))
 146:       {
 147:         jar_entry = (JarEntry) jar_file.getEntry (entry_name);
 148: 
 149:         if(jar_entry == null)
 150:           throw new FileNotFoundException("No entry for " + entry_name + " exists.");
 151:       }
 152: 
 153:     connected = true;
 154:   }
 155: 
 156:   public InputStream getInputStream() throws IOException
 157:   {
 158:     if (!connected)
 159:       connect();
 160: 
 161:     if (! doInput)
 162:       throw new ProtocolException("Can't open InputStream if doInput is false");
 163: 
 164:     return jar_file.getInputStream (jar_entry);
 165:   }
 166: 
 167:   public synchronized JarFile getJarFile() throws IOException
 168:   {
 169:     if (!connected)
 170:       connect();
 171: 
 172:     if (! doInput)
 173:       throw new ProtocolException("Can't open JarFile if doInput is false");
 174: 
 175:     return jar_file;
 176:   }
 177: 
 178:   public String getHeaderField(String field)
 179:   {
 180:     try
 181:       {
 182:         if (!connected)
 183:           connect();
 184: 
 185:         if (field.equals("content-type"))
 186:           return guessContentTypeFromName(getJarEntry().getName());
 187:         else if (field.equals("content-length"))
 188:           return Long.toString(getJarEntry().getSize());
 189:         else if (field.equals("last-modified"))
 190:           {
 191:             // Both creating and manipulating dateFormat need synchronization.
 192:             synchronized (Connection.class)
 193:               {
 194:                 if (dateFormat == null)
 195:                   dateFormat = new SimpleDateFormat
 196:                     ("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
 197:                      new Locale ("En", "Us", "Unix"));
 198: 
 199:                 return dateFormat.format(new Date(getJarEntry().getTime()));
 200:               }
 201:           }
 202:       }
 203:     catch (IOException e)
 204:       {
 205:         // Fall through.
 206:       }
 207:     return null;
 208:   }
 209: 
 210:   public int getContentLength()
 211:   {
 212:     if (!connected)
 213:       return -1;
 214: 
 215:     return (int) jar_entry.getSize();
 216:   }
 217: 
 218:   public long getLastModified()
 219:   {
 220:     if (!connected)
 221:       return -1;
 222: 
 223:     try
 224:       {
 225:         return getJarEntry().getTime();
 226:       }
 227:     catch (IOException e)
 228:       {
 229:         return -1;
 230:       }
 231:   }
 232: }