1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.URL ; 17 import java.net.URLConnection ; 18 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry; 19 import org.eclipse.osgi.internal.baseadaptor.AdaptorMsg; 20 import org.eclipse.osgi.util.NLS; 21 22 25 26 public class BundleURLConnection extends URLConnection { 27 28 protected final BundleEntry bundleEntry; 29 30 31 protected InputStream in; 32 33 34 protected String contentType; 35 36 42 public BundleURLConnection(URL url, BundleEntry bundleEntry) { 43 super(url); 44 45 this.bundleEntry = bundleEntry; 46 this.in = null; 47 this.contentType = null; 48 } 49 50 public synchronized void connect() throws IOException { 51 if (!connected) { 52 if (bundleEntry != null) { 53 in = bundleEntry.getInputStream(); 54 connected = true; 55 } else { 56 throw new IOException (NLS.bind(AdaptorMsg.RESOURCE_NOT_FOUND_EXCEPTION, url)); 57 } 58 } 59 } 60 61 public int getContentLength() { 62 return ((int) bundleEntry.getSize()); 63 } 64 65 public String getContentType() { 66 if (contentType == null) { 67 contentType = guessContentTypeFromName(bundleEntry.getName()); 68 69 if (contentType == null) { 70 if (!connected) { 71 try { 72 connect(); 73 } catch (IOException e) { 74 return (null); 75 } 76 } 77 try { 78 if (in.markSupported()) 79 contentType = guessContentTypeFromStream(in); 80 } catch (IOException e) { 81 } 83 } 84 } 85 86 return (contentType); 87 } 88 89 public boolean getDoInput() { 90 return (true); 91 } 92 93 public boolean getDoOutput() { 94 return (false); 95 } 96 97 public InputStream getInputStream() throws IOException { 98 if (!connected) { 99 connect(); 100 } 101 102 return (in); 103 } 104 105 public long getLastModified() { 106 long lastModified = bundleEntry.getTime(); 107 108 if (lastModified == -1) { 109 return (0); 110 } 111 112 return (lastModified); 113 } 114 115 119 public URL getLocalURL() { 120 return bundleEntry.getLocalURL(); 121 } 122 123 128 public URL getFileURL() { 129 return bundleEntry.getFileURL(); 130 } 131 } 132 | Popular Tags |