KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > utils > FileUtil


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.utils;
12
13 import java.io.*;
14 import java.net.URI JavaDoc;
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 /**
24  * Static utility methods for manipulating Files and URIs.
25  */

26 public class FileUtil {
27     /**
28      * Singleton buffer created to prevent buffer creations in the
29      * transferStreams method. Used as an optimization, based on the assumption
30      * that multiple writes won't happen in a given instance of FileStore.
31      */

32     private static final byte[] buffer = new byte[8192];
33
34     /**
35      * Converts a ResourceAttributes object into an IFileInfo object.
36      * @param attributes The resource attributes
37      * @return The file info
38      */

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     /**
49      * Converts an IPath into its canonical form for the local file system.
50      */

51     public static IPath canonicalPath(IPath path) {
52         if (path == null)
53             return null;
54         try {
55             final String JavaDoc pathString = path.toOSString();
56             final String JavaDoc canonicalPath = new java.io.File JavaDoc(pathString).getCanonicalPath();
57             //only create a new path if necessary
58
if (canonicalPath.equals(pathString))
59                 return path;
60             return new Path(canonicalPath);
61         } catch (IOException e) {
62             return path;
63         }
64     }
65
66     /**
67      * Converts a URI into its canonical form.
68      */

69     public static URI JavaDoc canonicalURI(URI JavaDoc uri) {
70         if (uri == null)
71             return null;
72         if (EFS.SCHEME_FILE.equals(uri.getScheme())) {
73             //only create a new URI if it is different
74
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     /**
84      * Returns true if the given file system locations overlap. If "bothDirections" is true,
85      * this means they are the same, or one is a proper prefix of the other. If "bothDirections"
86      * is false, this method only returns true if the locations are the same, or the first location
87      * is a prefix of the second. Returns false if the locations do not overlap
88      * Does the right thing with respect to case insensitive platforms.
89      */

90     private static boolean computeOverlap(IPath location1, IPath location2, boolean bothDirections) {
91         IPath one = location1;
92         IPath two = location2;
93         // If we are on a case-insensitive file system then convert to all lower case.
94
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     /**
102      * Returns true if the given file system locations overlap. If "bothDirections" is true,
103      * this means they are the same, or one is a proper prefix of the other. If "bothDirections"
104      * is false, this method only returns true if the locations are the same, or the first location
105      * is a prefix of the second. Returns false if the locations do not overlap
106      */

107     private static boolean computeOverlap(URI JavaDoc location1, URI JavaDoc location2, boolean bothDirections) {
108         if (location1.equals(location2))
109             return true;
110         String JavaDoc scheme1 = location1.getScheme();
111         String JavaDoc 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             //handled below
121
}
122         if (system == null) {
123             //we are stuck with string comparison
124
String JavaDoc string1 = location1.toString();
125             String JavaDoc 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     /**
134      * Converts an IFileInfo object into a ResourceAttributes object.
135      * @param fileInfo The file info
136      * @return The resource attributes
137      */

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     /**
148      * Returns true if the given file system locations overlap, and false otherwise.
149      * Overlap means the locations are the same, or one is a proper prefix of the other.
150      */

151     public static boolean isOverlapping(URI JavaDoc location1, URI JavaDoc location2) {
152         return computeOverlap(location1, location2, true);
153     }
154
155     /**
156      * Returns true if location1 is the same as, or a proper prefix of, location2.
157      * Returns false otherwise.
158      */

159     public static boolean isPrefixOf(IPath location1, IPath location2) {
160         return computeOverlap(location1, location2, false);
161     }
162
163     /**
164      * Returns true if location1 is the same as, or a proper prefix of, location2.
165      * Returns false otherwise.
166      */

167     public static boolean isPrefixOf(URI JavaDoc location1, URI JavaDoc location2) {
168         return computeOverlap(location1, location2, false);
169     }
170
171     /**
172      * Closes a stream and ignores any resulting exception. This is useful
173      * when doing stream cleanup in a finally block where secondary exceptions
174      * are not worth logging.
175      */

176     public static void safeClose(InputStream in) {
177         try {
178             if (in != null)
179                 in.close();
180         } catch (IOException e) {
181             //ignore
182
}
183     }
184
185     /**
186      * Closes a stream and ignores any resulting exception. This is useful
187      * when doing stream cleanup in a finally block where secondary exceptions
188      * are not worth logging.
189      */

190     public static void safeClose(OutputStream out) {
191         try {
192             if (out != null)
193                 out.close();
194         } catch (IOException e) {
195             //ignore
196
}
197     }
198
199     /**
200      * Converts a URI to an IPath. Returns null if the URI cannot be represented
201      * as an IPath.
202      * <p>
203      * Note this method differs from URIUtil in its handling of relative URIs
204      * as being relative to path variables.
205      */

206     public static IPath toPath(URI JavaDoc uri) {
207         if (uri == null)
208             return null;
209         final String JavaDoc scheme = uri.getScheme();
210         // null scheme represents path variable
211
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 JavaDoc path, IProgressMonitor monitor) throws CoreException {
217         monitor = Policy.monitorFor(monitor);
218         try {
219             /*
220              * Note: although synchronizing on the buffer is thread-safe,
221              * it may result in slower performance in the future if we want
222              * to allow concurrent writes.
223              */

224             synchronized (buffer) {
225                 while (true) {
226                     int bytesRead = -1;
227                     try {
228                         bytesRead = source.read(buffer);
229                     } catch (IOException e) {
230                         String JavaDoc 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 JavaDoc 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     /**
251      * Not intended for instantiation.
252      */

253     private FileUtil() {
254         super();
255     }
256 }
Popular Tags