1 11 package org.eclipse.core.filesystem.provider; 12 13 import java.io.*; 14 import java.net.URI ; 15 import org.eclipse.core.filesystem.*; 16 import org.eclipse.core.internal.filesystem.*; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.osgi.util.NLS; 19 20 29 public abstract class FileStore extends PlatformObject implements IFileStore { 30 35 private static final byte[] buffer = new byte[8192]; 36 37 41 protected static final IFileInfo[] EMPTY_FILE_INFO_ARRAY = new IFileInfo[0]; 42 43 47 protected static final String [] EMPTY_STRING_ARRAY = new String [0]; 48 49 62 private static final void transferStreams(InputStream source, OutputStream destination, String path, IProgressMonitor monitor) throws CoreException { 63 monitor = Policy.monitorFor(monitor); 64 try { 65 70 synchronized (buffer) { 71 while (true) { 72 int bytesRead = -1; 73 try { 74 bytesRead = source.read(buffer); 75 } catch (IOException e) { 76 String msg = NLS.bind(Messages.failedReadDuringWrite, path); 77 Policy.error(EFS.ERROR_READ, msg, e); 78 } 79 if (bytesRead == -1) 80 break; 81 try { 82 destination.write(buffer, 0, bytesRead); 83 } catch (IOException e) { 84 String msg = NLS.bind(Messages.couldNotWrite, path); 85 Policy.error(EFS.ERROR_WRITE, msg, e); 86 } 87 monitor.worked(1); 88 } 89 } 90 } finally { 91 Policy.safeClose(source); 92 Policy.safeClose(destination); 93 } 94 } 95 96 102 public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException { 103 IFileStore[] childStores = childStores(options, monitor); 104 IFileInfo[] childInfos = new IFileInfo[childStores.length]; 105 for (int i = 0; i < childStores.length; i++) { 106 childInfos[i] = childStores[i].fetchInfo(); 107 } 108 return childInfos; 109 } 110 111 114 public abstract String [] childNames(int options, IProgressMonitor monitor) throws CoreException; 115 116 120 public IFileStore[] childStores(int options, IProgressMonitor monitor) throws CoreException { 121 String [] children = childNames(options, monitor); 122 IFileStore[] wrapped = new IFileStore[children.length]; 123 for (int i = 0; i < wrapped.length; i++) 124 wrapped[i] = getChild(children[i]); 125 return wrapped; 126 } 127 128 133 public void copy(IFileStore destination, int options, IProgressMonitor monitor) throws CoreException { 134 monitor = Policy.monitorFor(monitor); 135 Policy.checkCanceled(monitor); 136 final IFileInfo sourceInfo = fetchInfo(EFS.NONE, null); 137 if (sourceInfo.isDirectory()) 138 copyDirectory(sourceInfo, destination, options, monitor); 139 else 140 copyFile(sourceInfo, destination, options, monitor); 141 } 142 143 160 protected void copyDirectory(IFileInfo sourceInfo, IFileStore destination, int options, IProgressMonitor monitor) throws CoreException { 161 try { 162 IFileStore[] children = null; 163 int opWork = 1; 164 if ((options & EFS.SHALLOW) == 0) { 165 children = childStores(EFS.NONE, null); 166 opWork += children.length; 167 } 168 monitor.beginTask("", opWork); monitor.subTask(NLS.bind(Messages.copying, toString())); 170 destination.mkdir(EFS.NONE, Policy.subMonitorFor(monitor, 1)); 172 173 if (children == null) 174 return; 175 for (int i = 0; i < children.length; i++) 177 children[i].copy(destination.getChild(children[i].getName()), options, Policy.subMonitorFor(monitor, 1)); 178 } finally { 179 monitor.done(); 180 } 181 } 182 183 200 protected void copyFile(IFileInfo sourceInfo, IFileStore destination, int options, IProgressMonitor monitor) throws CoreException { 201 try { 202 if ((options & EFS.OVERWRITE) == 0 && destination.fetchInfo().exists()) 203 Policy.error(EFS.ERROR_EXISTS, NLS.bind(Messages.fileExists, destination)); 204 long length = sourceInfo.getLength(); 205 int totalWork; 206 if (length == -1) 207 totalWork = IProgressMonitor.UNKNOWN; 208 else 209 totalWork = 1 + (int) (length / buffer.length); 210 String sourcePath = toString(); 211 monitor.beginTask(NLS.bind(Messages.copying, sourcePath), totalWork); 212 InputStream in = null; 213 OutputStream out = null; 214 try { 215 in = openInputStream(EFS.NONE, Policy.subMonitorFor(monitor, 0)); 216 out = destination.openOutputStream(EFS.NONE, Policy.subMonitorFor(monitor, 0)); 217 transferStreams(in, out, sourcePath, monitor); 218 transferAttributes(sourceInfo, destination); 219 } catch (CoreException e) { 220 Policy.safeClose(in); 221 Policy.safeClose(out); 222 if (!destination.fetchInfo(0, null).exists()) 224 destination.delete(EFS.NONE, null); 225 throw e; 226 } 227 } finally { 228 monitor.done(); 229 } 230 } 231 232 242 public void delete(int options, IProgressMonitor monitor) throws CoreException { 243 Policy.error(EFS.ERROR_DELETE, NLS.bind(Messages.noImplDelete, toString())); 244 } 245 246 260 public boolean equals(Object obj) { 261 if (this == obj) 262 return true; 263 if (!(obj instanceof FileStore)) 264 return false; 265 return toURI().equals(((FileStore) obj).toURI()); 266 } 267 268 273 public IFileInfo fetchInfo() { 274 try { 275 return fetchInfo(EFS.NONE, null); 276 } catch (CoreException e) { 277 FileInfo result = new FileInfo(getName()); 279 result.setExists(false); 280 return result; 281 } 282 } 283 284 287 public abstract IFileInfo fetchInfo(int options, IProgressMonitor monitor) throws CoreException; 288 289 293 public IFileStore getChild(IPath path) { 294 IFileStore result = this; 295 for (int i = 0, imax = path.segmentCount(); i < imax; i++) 296 result = result.getChild(path.segment(i)); 297 return result; 298 } 299 300 303 public abstract IFileStore getChild(String name); 304 305 309 public IFileSystem getFileSystem() { 310 try { 311 return EFS.getFileSystem(toURI().getScheme()); 312 } catch (CoreException e) { 313 throw new RuntimeException (e); 315 } 316 } 317 318 321 public abstract String getName(); 322 323 326 public abstract IFileStore getParent(); 327 328 337 public int hashCode() { 338 return toURI().hashCode(); 339 } 340 341 350 public boolean isParentOf(IFileStore other) { 351 while (true) { 352 other = other.getParent(); 353 if (other == null) 354 return false; 355 if (this.equals(other)) 356 return true; 357 } 358 } 359 360 369 public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreException { 370 Policy.error(EFS.ERROR_WRITE, NLS.bind(Messages.noImplWrite, toString())); 371 return null; } 373 374 379 public void move(IFileStore destination, int options, IProgressMonitor monitor) throws CoreException { 380 monitor = Policy.monitorFor(monitor); 381 try { 382 monitor.beginTask(NLS.bind(Messages.moving, destination.toString()), 100); 383 copy(destination, options & EFS.OVERWRITE, Policy.subMonitorFor(monitor, 70)); 384 delete(EFS.NONE, Policy.subMonitorFor(monitor, 30)); 385 } catch (CoreException e) { 386 String message = NLS.bind(Messages.couldNotMove, toString()); 388 Policy.error(EFS.ERROR_WRITE, message, e); 389 } finally { 390 monitor.done(); 391 } 392 } 393 394 397 public abstract InputStream openInputStream(int options, IProgressMonitor monitor) throws CoreException; 398 399 408 public OutputStream openOutputStream(int options, IProgressMonitor monitor) throws CoreException { 409 Policy.error(EFS.ERROR_WRITE, NLS.bind(Messages.noImplWrite, toString())); 410 return null; } 412 413 422 public void putInfo(IFileInfo info, int options, IProgressMonitor monitor) throws CoreException { 423 Policy.error(EFS.ERROR_WRITE, NLS.bind(Messages.noImplWrite, toString())); 424 } 425 426 432 public java.io.File toLocalFile(int options, IProgressMonitor monitor) throws CoreException { 433 monitor = Policy.monitorFor(monitor); 434 if (options != EFS.CACHE) 436 return null; 437 return FileCache.getCache().cache(this, monitor); 438 } 439 440 447 public String toString() { 448 return toURI().toString(); 449 } 450 451 454 public abstract URI toURI(); 455 456 private void transferAttributes(IFileInfo sourceInfo, IFileStore destination) throws CoreException { 457 int options = EFS.SET_ATTRIBUTES | EFS.SET_LAST_MODIFIED; 458 destination.putInfo(sourceInfo, options, null); 459 } 460 } | Popular Tags |