1 11 package org.eclipse.core.internal.utils; 12 13 import java.io.*; 14 import java.net.URI ; 15 import org.eclipse.core.filesystem.*; 16 import org.eclipse.core.internal.resources.ResourceException; 17 import org.eclipse.core.internal.resources.Workspace; 18 import org.eclipse.core.resources.IResourceStatus; 19 import org.eclipse.core.resources.ResourceAttributes; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.osgi.util.NLS; 22 23 26 public class FileUtil { 27 32 private static final byte[] buffer = new byte[8192]; 33 34 39 public static IFileInfo attributesToFileInfo(ResourceAttributes attributes) { 40 IFileInfo fileInfo = EFS.createFileInfo(); 41 fileInfo.setAttribute(EFS.ATTRIBUTE_READ_ONLY, attributes.isReadOnly()); 42 fileInfo.setAttribute(EFS.ATTRIBUTE_EXECUTABLE, attributes.isExecutable()); 43 fileInfo.setAttribute(EFS.ATTRIBUTE_ARCHIVE, attributes.isArchive()); 44 fileInfo.setAttribute(EFS.ATTRIBUTE_HIDDEN, attributes.isHidden()); 45 return fileInfo; 46 } 47 48 51 public static IPath canonicalPath(IPath path) { 52 if (path == null) 53 return null; 54 try { 55 final String pathString = path.toOSString(); 56 final String canonicalPath = new java.io.File (pathString).getCanonicalPath(); 57 if (canonicalPath.equals(pathString)) 59 return path; 60 return new Path(canonicalPath); 61 } catch (IOException e) { 62 return path; 63 } 64 } 65 66 69 public static URI canonicalURI(URI uri) { 70 if (uri == null) 71 return null; 72 if (EFS.SCHEME_FILE.equals(uri.getScheme())) { 73 final IPath inputPath = URIUtil.toPath(uri); 75 final IPath canonicalPath = canonicalPath(inputPath); 76 if (inputPath == canonicalPath) 77 return uri; 78 return URIUtil.toURI(canonicalPath); 79 } 80 return uri; 81 } 82 83 90 private static boolean computeOverlap(IPath location1, IPath location2, boolean bothDirections) { 91 IPath one = location1; 92 IPath two = location2; 93 if (!Workspace.caseSensitive) { 95 one = new Path(location1.toOSString().toLowerCase()); 96 two = new Path(location2.toOSString().toLowerCase()); 97 } 98 return one.isPrefixOf(two) || (bothDirections && two.isPrefixOf(one)); 99 } 100 101 107 private static boolean computeOverlap(URI location1, URI location2, boolean bothDirections) { 108 if (location1.equals(location2)) 109 return true; 110 String scheme1 = location1.getScheme(); 111 String scheme2 = location2.getScheme(); 112 if (scheme1 == null ? scheme2 != null : !scheme1.equals(scheme2)) 113 return false; 114 if (EFS.SCHEME_FILE.equals(scheme1) && EFS.SCHEME_FILE.equals(scheme2)) 115 return computeOverlap(URIUtil.toPath(location1), URIUtil.toPath(location2), bothDirections); 116 IFileSystem system = null; 117 try { 118 system = EFS.getFileSystem(scheme1); 119 } catch (CoreException e) { 120 } 122 if (system == null) { 123 String string1 = location1.toString(); 125 String string2 = location2.toString(); 126 return string1.startsWith(string2) || (bothDirections && string2.startsWith(string1)); 127 } 128 IFileStore store1 = system.getStore(location1); 129 IFileStore store2 = system.getStore(location2); 130 return store1.equals(store2) || store1.isParentOf(store2) || (bothDirections && store2.isParentOf(store1)); 131 } 132 133 138 public static ResourceAttributes fileInfoToAttributes(IFileInfo fileInfo) { 139 ResourceAttributes attributes = new ResourceAttributes(); 140 attributes.setReadOnly(fileInfo.getAttribute(EFS.ATTRIBUTE_READ_ONLY)); 141 attributes.setArchive(fileInfo.getAttribute(EFS.ATTRIBUTE_ARCHIVE)); 142 attributes.setExecutable(fileInfo.getAttribute(EFS.ATTRIBUTE_EXECUTABLE)); 143 attributes.setHidden(fileInfo.getAttribute(EFS.ATTRIBUTE_HIDDEN)); 144 return attributes; 145 } 146 147 151 public static boolean isOverlapping(URI location1, URI location2) { 152 return computeOverlap(location1, location2, true); 153 } 154 155 159 public static boolean isPrefixOf(IPath location1, IPath location2) { 160 return computeOverlap(location1, location2, false); 161 } 162 163 167 public static boolean isPrefixOf(URI location1, URI location2) { 168 return computeOverlap(location1, location2, false); 169 } 170 171 176 public static void safeClose(InputStream in) { 177 try { 178 if (in != null) 179 in.close(); 180 } catch (IOException e) { 181 } 183 } 184 185 190 public static void safeClose(OutputStream out) { 191 try { 192 if (out != null) 193 out.close(); 194 } catch (IOException e) { 195 } 197 } 198 199 206 public static IPath toPath(URI uri) { 207 if (uri == null) 208 return null; 209 final String scheme = uri.getScheme(); 210 if (scheme == null || EFS.SCHEME_FILE.equals(scheme)) 212 return new Path(uri.getSchemeSpecificPart()); 213 return null; 214 } 215 216 public static final void transferStreams(InputStream source, OutputStream destination, String path, IProgressMonitor monitor) throws CoreException { 217 monitor = Policy.monitorFor(monitor); 218 try { 219 224 synchronized (buffer) { 225 while (true) { 226 int bytesRead = -1; 227 try { 228 bytesRead = source.read(buffer); 229 } catch (IOException e) { 230 String msg = NLS.bind(Messages.localstore_failedReadDuringWrite, path); 231 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, new Path(path), msg, e); 232 } 233 if (bytesRead == -1) 234 break; 235 try { 236 destination.write(buffer, 0, bytesRead); 237 } catch (IOException e) { 238 String msg = NLS.bind(Messages.localstore_couldNotWrite, path); 239 throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, new Path(path), msg, e); 240 } 241 monitor.worked(1); 242 } 243 } 244 } finally { 245 safeClose(source); 246 safeClose(destination); 247 } 248 } 249 250 253 private FileUtil() { 254 super(); 255 } 256 } | Popular Tags |