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 cache = new Hashtable();
  80:     private static final int READBUFSIZE = 4*1024;
  81:     
  82:     public static synchronized JarFile get (URL url, boolean useCaches)
  83:        throws IOException
  84:     {
  85:       JarFile jf;
  86:       if (useCaches)
  87:         {
  88:           jf = (JarFile) cache.get (url);
  89:           if (jf != null)
  90:             return jf;
  91:         }
  92: 
  93:       if ("file".equals (url.getProtocol()))
  94:     {
  95:       String fn = url.getFile();
  96:       fn = gnu.java.net.protocol.file.Connection.unquote(fn);
  97:       File f = new File (fn);
  98:       jf = new JarFile (f, true, ZipFile.OPEN_READ);
  99:     }
 100:       else
 101:     {
 102:       URLConnection urlconn = url.openConnection();
 103:       InputStream is = urlconn.getInputStream();
 104:       byte[] buf = new byte [READBUFSIZE];
 105:       File f = File.createTempFile ("cache", "jar");
 106:       FileOutputStream fos = new FileOutputStream (f); 
 107:       int len = 0;
 108:       
 109:       while ((len = is.read (buf)) != -1)
 110:         {
 111:           fos.write (buf, 0, len);
 112:         }
 113:       
 114:       fos.close();
 115:       // Always verify the Manifest, open read only and delete when done.
 116:       jf = new JarFile (f, true,
 117:                 ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
 118:     }
 119: 
 120:       if (useCaches)
 121:         cache.put (url, jf);
 122: 
 123:       return jf;
 124:     }
 125:   }
 126: 
 127:   protected Connection(URL url)
 128:     throws MalformedURLException
 129:   {
 130:     super(url);
 131:   }
 132: 
 133:   public synchronized void connect() throws IOException
 134:   {
 135:     // Call is ignored if already connected.
 136:     if (connected)
 137:       return;
 138: 
 139:     jar_url = getJarFileURL();
 140:     jar_file = JarFileCache.get (jar_url, useCaches);
 141:     String entry_name = getEntryName();
 142:     
 143:     if (entry_name != null
 144:         && !entry_name.equals (""))
 145:       {
 146:         jar_entry = (JarEntry) jar_file.getEntry (entry_name);
 147: 
 148:         if(jar_entry == null)
 149:           throw new FileNotFoundException("No entry for " + entry_name + " exists.");
 150:       }
 151: 
 152:     connected = true;
 153:   }
 154: 
 155:   public InputStream getInputStream() throws IOException
 156:   {
 157:     if (!connected)
 158:       connect();
 159: 
 160:     if (! doInput)
 161:       throw new ProtocolException("Can't open InputStream if doInput is false");
 162:     
 163:     return jar_file.getInputStream (jar_entry);
 164:   }
 165: 
 166:   public synchronized JarFile getJarFile() throws IOException
 167:   {
 168:     if (!connected)
 169:       connect();
 170: 
 171:     if (! doInput)
 172:       throw new ProtocolException("Can't open JarFile if doInput is false");
 173: 
 174:     return jar_file;
 175:   }
 176: 
 177:   public String getHeaderField(String field)
 178:   {
 179:     try
 180:       {
 181:     if (!connected)
 182:       connect();
 183: 
 184:     if (field.equals("content-type"))
 185:           return guessContentTypeFromName(getJarEntry().getName());
 186:     else if (field.equals("content-length"))
 187:           return Long.toString(getJarEntry().getSize());
 188:     else if (field.equals("last-modified"))
 189:       {
 190:         // Both creating and manipulating dateFormat need synchronization.
 191:         synchronized (Connection.class)
 192:           {
 193:         if (dateFormat == null)
 194:           dateFormat = new SimpleDateFormat
 195:             ("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
 196:              new Locale ("En", "Us", "Unix"));
 197: 
 198:             return dateFormat.format(new Date(getJarEntry().getTime()));
 199:           }
 200:       }
 201:       }
 202:     catch (IOException e)
 203:       {
 204:         // Fall through.
 205:       }
 206:     return null;
 207:   }
 208: 
 209:   public int getContentLength()
 210:   {
 211:     if (!connected)
 212:       return -1;
 213: 
 214:     return (int) jar_entry.getSize();
 215:   }
 216: 
 217:   public long getLastModified()
 218:   {
 219:     if (!connected)
 220:       return -1;
 221: 
 222:     try
 223:       {
 224:     return getJarEntry().getTime();
 225:       }
 226:     catch (IOException e)
 227:       {
 228:     return -1;
 229:       }
 230:   }
 231: }