1 11 package org.eclipse.update.core; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.net.MalformedURLException ; 18 import java.net.URL ; 19 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.update.internal.core.FatalIOException; 22 import org.eclipse.update.internal.core.Messages; 23 import org.eclipse.update.internal.core.URLEncoder; 24 import org.eclipse.update.internal.core.UpdateManagerUtils; 25 import org.eclipse.update.internal.core.connection.ConnectionFactory; 26 import org.eclipse.update.internal.core.connection.HttpResponse; 27 import org.eclipse.update.internal.core.connection.IResponse; 28 29 47 public class ContentReference { 48 49 53 public static final long UNKNOWN_SIZE = -1; 54 55 61 public static final int DEFAULT_EXECUTABLE_PERMISSION = -1; 62 63 private static final String FILE_URL_PROTOCOL = "file"; 65 private String id; 66 private URL url; private File file; private IResponse response; 69 private int permission; 70 private long length; 71 72 private boolean tempLocal = false; 74 75 private long lastModified; 76 77 84 public ContentReference(String id, URL url) { 85 this.id = (id == null ? "" : id); this.url = url; this.file = null; 88 } 89 90 97 public ContentReference(String id, File file) { 98 this.id = (id == null ? "" : id); this.file = file; this.url = null; 101 } 102 103 112 public ContentReference createContentReference(String id, File file) { 113 return new ContentReference(id, file,true); 114 } 115 118 private ContentReference(String id, File file, boolean b) { 119 this(id,file); 120 setTempLocal(b); 121 } 122 123 129 public String getIdentifier() { 130 return id; 131 } 132 133 140 public InputStream getInputStream() throws IOException { 141 if (file != null) 142 return new FileInputStream (file); 143 else if (url != null) { 144 if (response == null) { 145 URL resolvedURL = URLEncoder.encode(url); 146 response = ConnectionFactory.get(resolvedURL); 147 UpdateManagerUtils.checkConnectionResult(response,resolvedURL); 148 } 149 InputStream is=response.getInputStream(); 150 length=response.getContentLength(); 151 return is; 152 } else 153 throw new FatalIOException(NLS.bind(Messages.ContentReference_UnableToCreateInputStream, (new String [] { this.toString() }))); 154 } 155 162 InputStream getPartialInputStream(long offset) throws IOException { 163 if (url != null && "http".equals(url.getProtocol())) { URL resolvedURL = URLEncoder.encode(url); 165 response = ConnectionFactory.get(resolvedURL); 166 if(response instanceof HttpResponse) 167 ((HttpResponse)response).setOffset(offset); 168 UpdateManagerUtils.checkConnectionResult(response,resolvedURL); 169 InputStream is = response.getInputStream(); 170 length=offset + response.getContentLength(); 171 return is; 172 } else 173 throw new FatalIOException(NLS.bind(Messages.ContentReference_UnableToCreateInputStream, (new String [] { this.toString() }))); 174 } 175 176 182 public long getInputSize() throws IOException { 183 if (length>0) 184 return length; 185 if (file != null) 186 return file.length(); 187 else if (url != null) { 188 if (response == null) { 189 URL resolvedURL = null; 190 try { 191 resolvedURL = URLEncoder.encode(url); 192 response = ConnectionFactory.get(resolvedURL); 193 } catch (IOException e) { 194 return ContentReference.UNKNOWN_SIZE; 195 } 196 UpdateManagerUtils.checkConnectionResult(response,resolvedURL); 197 } 198 long size = response.getContentLength(); 199 return size == -1 ? ContentReference.UNKNOWN_SIZE : size; 200 } else 201 return ContentReference.UNKNOWN_SIZE; 202 } 203 204 211 public boolean isLocalReference() { 212 218 return tempLocal; 220 } 221 222 231 public File asFile() throws IOException { 232 if (file != null) 233 return file; 234 235 if (url != null && FILE_URL_PROTOCOL.equals(url.getProtocol())) { 236 File result = new File (url.getFile()); 237 if (result.exists()) 238 return result; 239 else 240 throw new IOException (NLS.bind(Messages.ContentReference_FileDoesNotExist, (new String [] { this.toString() }))); 241 } 242 243 throw new IOException (NLS.bind(Messages.ContentReference_UnableToReturnReferenceAsFile, (new String [] { this.toString() }))); 244 } 245 246 253 public URL asURL() throws IOException { 254 if (url != null) 255 return url; 256 257 if (file != null) 258 return file.toURL(); 259 260 throw new FatalIOException(NLS.bind(Messages.ContentReference_UnableToReturnReferenceAsURL, (new String [] { this.toString() }))); 261 } 262 263 269 public String toString() { 270 if (file != null) 271 return file.getAbsolutePath(); 272 else 273 return url.toExternalForm(); 274 } 275 282 public int getPermission() { 283 return permission; 284 } 285 286 291 public void setPermission(int permission) { 292 this.permission = permission; 293 } 294 295 300 protected void setTempLocal(boolean tempLocal) { 301 this.tempLocal = tempLocal; 302 } 303 304 309 public void setLastModified(long timestamp) { 310 this.lastModified = timestamp; 311 } 312 313 318 public long getLastModified() { 319 if (lastModified == 0) { 320 if (file != null) 321 lastModified = file.lastModified(); 322 else if (url != null) { 323 if (response == null) { 324 try { 325 URL resolvedURL = URLEncoder.encode(url); 326 response = ConnectionFactory.get(resolvedURL); 327 } catch (MalformedURLException e) { 328 } catch (IOException e) { 330 } 332 } 333 lastModified = response.getLastModified(); 334 } 335 } 336 return lastModified; 337 } 338 } 339 | Popular Tags |