1 17 18 package org.apache.geronimo.system.url.file; 19 20 import java.io.BufferedInputStream ; 21 import java.io.BufferedOutputStream ; 22 import java.io.File ; 23 import java.io.FileDescriptor ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.FileOutputStream ; 27 import java.io.FilePermission ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.SyncFailedException ; 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 import java.net.URLConnection ; 35 import java.security.Permission ; 36 import java.util.ArrayList ; 37 import java.util.Collections ; 38 import java.util.HashMap ; 39 import java.util.List ; 40 import java.util.Map ; 41 42 import sun.net.www.ParseUtil; 43 44 51 public class FileURLConnection extends URLConnection { 52 private static final boolean IS_OS_WINDOWS = System.getProperty("os.name").startsWith("Windows"); 53 private File file; 54 private FileDescriptor fd; 55 56 public FileURLConnection(final URL url, final File file) throws MalformedURLException , IOException { 57 super(url); 58 59 if (file == null) { 60 throw new IllegalArgumentException ("file is null"); 61 } 62 63 this.file = file; 64 } 65 66 public File getFile() { 67 return file; 68 } 69 70 public void connect() throws IOException { 71 if (connected) { 72 return; 73 } 74 75 if (!file.exists()) { 76 throw new FileNotFoundException (file.toString()); 77 } 78 79 connected = true; 80 } 81 82 89 public InputStream getInputStream() throws IOException { 90 if (!connected) { 91 connect(); 92 } 93 94 FileInputStream fis = new FileInputStream (file); 95 fd = fis.getFD(); 96 97 return new BufferedInputStream (fis); 98 } 99 100 107 public OutputStream getOutputStream() throws IOException { 108 if (!connected) { 109 connect(); 110 } 111 112 FileOutputStream fos = new FileOutputStream (file); 113 fd = fos.getFD(); 114 115 return new BufferedOutputStream (fos); 116 } 117 118 124 public Permission getPermission() throws IOException { 125 String perms = null; 127 128 if (file.canRead()) { 129 perms = "read"; 130 } 131 if (file.canWrite()) { 132 if (perms != null) { 133 perms += ",write"; 134 } else { 135 perms = "write"; 136 } 137 } 138 139 String filename = ParseUtil.decode(url.getPath()); 141 if (File.separatorChar != '/') { 142 filename.replace('/', File.separatorChar); 143 } 144 145 return new FilePermission (filename, perms); 146 } 147 148 152 private void maybeSync() { 153 if (fd != null && fd.valid()) { 154 if (IS_OS_WINDOWS) { 155 try { 156 fd.sync(); 157 } catch (SyncFailedException e) { 158 } 160 } 161 } 162 } 163 164 170 public long getLastModified() { 171 maybeSync(); 172 return file.lastModified(); 173 } 174 175 178 public long getDate() { 179 return getLastModified(); 180 } 181 182 186 public int getContentLength() { 187 maybeSync(); 188 189 final long value = file.length(); 190 if (value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) { 191 throw new IllegalStateException ("Can not safly convert to int: " + value); 192 } 193 194 return (int) value; 195 } 196 197 200 public String getContentType() { 201 return getFileNameMap().getContentTypeFor(file.getName()); 202 } 203 204 205 209 225 public String getHeaderField(final String name) { 226 if (name == null) { 227 throw new IllegalArgumentException ("name is null"); 228 } 229 230 String headerName = name.toLowerCase(); 231 232 if (headerName.equals("last-modified")) { 233 return String.valueOf(getLastModified()); 234 } else if (headerName.equals("content-length")) { 235 return String.valueOf(getContentLength()); 236 } else if (headerName.equals("content-type")) { 237 return getContentType(); 238 } else if (headerName.equals("date")) { 239 return String.valueOf(getDate()); 240 } 241 242 return super.getHeaderField(name); 243 } 244 245 250 public Map getHeaderFields() { 251 Map headers = new HashMap (); 252 String [] headerNames = { 253 "last-modified", 254 "content-length", 255 "content-type", 256 "date" 257 }; 258 259 for (int i = 0; i < headerNames.length; i++) { 260 List list = new ArrayList (1); 261 list.add(getHeaderField(headerNames[i])); 262 headers.put(headerNames[i], Collections.unmodifiableList(list)); 263 } 264 265 return Collections.unmodifiableMap(headers); 266 } 267 268 272 public String getHeaderFieldKey(final int n) { 273 return getHeaderFieldKey(n); 274 } 275 276 public String getHeaderField(final int n) { 277 return getHeaderField(n); 278 } 279 } 280 | Popular Tags |