KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > util > FileSystemUtil


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
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  * tyeung@bea.com - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.apt.core.internal.util;
12
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 import org.eclipse.core.resources.IContainer;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IFolder;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.jdt.apt.core.internal.AptPlugin;
30
31 /**
32  * Simple utility class to encapsulate an mkdirs() that avoids a timing issue
33  * in the jdk.
34  */

35 public final class FileSystemUtil
36 {
37     private FileSystemUtil() {}
38     
39     /**
40      * If the given resource is a folder, then recursively deleted all derived
41      * files and folders contained within it. Delete the folder if it becomes empty
42      * and if itself is also a derived resource.
43      * If the given resource is a file, delete it iff it is a derived resource.
44      * The resource is left untouched if it is no a folder or a file.
45      * @param resource
46      * @return <code>true</code> iff the resource has been deleted.
47      * @throws CoreException
48      */

49     public static boolean deleteDerivedResources(final IResource resource)
50         throws CoreException
51     {
52         if (null == resource) {
53             return false;
54         }
55         if( resource.getType() == IResource.FOLDER ){
56             boolean deleteFolder = resource.isDerived();
57             IResource[] members = ((IFolder)resource).members();
58             for( int i=0, len=members.length; i<len; i++ ){
59                 deleteFolder &= deleteDerivedResources(members[i]);
60             }
61             if( deleteFolder ){
62                 deleteResource(resource);
63                 return true;
64             }
65             return false;
66         }
67         else if( resource.getType() == IResource.FILE ){
68             if( resource.isDerived() ){
69                 deleteResource(resource);
70                 return true;
71             }
72             return false;
73         }
74         // will skip pass everything else.
75
else
76             return false;
77     }
78     
79     /**
80      * Delete a resource without throwing an exception.
81      */

82     private static void deleteResource(IResource resource) {
83         try {
84             resource.delete(true, null);
85         } catch (CoreException e) {
86             // might have been concurrently deleted
87
if (resource.exists()) {
88                 AptPlugin.log(e, "Unable to delete derived resource " + resource); //$NON-NLS-1$
89
}
90         }
91     }
92     
93     public static void mkdirs( File JavaDoc parent )
94     {
95         if ( parent == null )
96             return;
97         
98         // It is necessary to synchronize to prevent timing issues while creating the parent directories
99
// We can be codegening multiple files that go into the same directory at the same time.
100
synchronized (FileSystemUtil.class) {
101             if (!parent.exists()) {
102                 boolean succeed = false;
103                 for (int i = 0 ; !succeed && i < 5 ; i++)
104                     succeed = parent.mkdirs();
105             }
106         }
107     }
108   
109     public static void makeDerivedParentFolders (IContainer container) throws CoreException {
110         // synchronize the "does it exist - if not, create it" sequence.
111
if ((container instanceof IFolder) && !container.exists()) {
112             makeDerivedParentFolders(container.getParent());
113             try {
114                 ((IFolder)container).create(true, true, null);
115             }
116             catch (CoreException e) {
117                 // Ignore race condition where another thread created the folder at the
118
// same time, causing checkDoesNotExist() to throw within create().
119
if (!container.exists()) {
120                     throw e;
121                 }
122             }
123             container.setDerived(true);
124         }
125     }
126     
127     /**
128      * Returns the contents of a file as a string in UTF8 format
129      */

130     public static String JavaDoc getContentsOfIFile(IFile file) throws IOException JavaDoc, CoreException {
131         return getContents(file.getContents(true));
132     }
133     
134     public static String JavaDoc getContentsOfFile(File JavaDoc file) throws IOException JavaDoc {
135         return getContents(new FileInputStream JavaDoc(file));
136     }
137     
138     private static String JavaDoc getContents(InputStream JavaDoc in) throws IOException JavaDoc {
139         try {
140             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
141             byte[] buffer = new byte[512];
142             int len;
143             while ((len = in.read(buffer)) > 0) {
144                 out.write(buffer, 0, len);
145             }
146             out.close();
147             String JavaDoc s = new String JavaDoc(out.toByteArray(), "UTF8"); //$NON-NLS-1$
148
return s;
149         }
150         finally {
151             try {in.close();} catch (IOException JavaDoc ioe) {}
152         }
153     }
154     
155     /**
156      * Stores a string into an Eclipse file in UTF8 format. The file
157      * will be created if it does not already exist.
158      * @throws IOException, CoreException
159      */

160     public static void writeStringToIFile(IFile file, String JavaDoc contents) throws IOException JavaDoc, CoreException {
161         byte[] data = contents.getBytes("UTF8"); //$NON-NLS-1$
162
ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(data);
163         if (file.exists()) {
164             if (file.isReadOnly()) {
165                 // provide opportunity to checkout read-only .factorypath file
166
ResourcesPlugin.getWorkspace().validateEdit(new IFile[]{file}, null);
167             }
168             file.setContents(input, true, false, null);
169         }
170         else {
171             // Even with FORCE, create() will still throw if the file already exists.
172
file.create(input, IResource.FORCE, null);
173         }
174     }
175     
176     /**
177      * Stores a string into an ordinary workspace file in UTF8 format.
178      * The file will be created if it does not already exist.
179      * @throws IOException
180      */

181     public static void writeStringToFile(File JavaDoc file, String JavaDoc contents) throws IOException JavaDoc {
182         byte[] data = contents.getBytes("UTF8"); //$NON-NLS-1$
183
OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
184         try {
185             for (byte b : data) {
186                 out.write(b);
187             }
188         }
189         finally {
190             try {out.close();} catch (IOException JavaDoc ioe) {}
191         }
192     }
193     
194     /**
195      * Return true if the content of the streams is identical,
196      * false if not.
197      */

198     public static boolean compareStreams(InputStream JavaDoc is1, InputStream JavaDoc is2) {
199         try {
200             int b1 = is1.read();
201             while(b1 != -1) {
202                 int b2 = is2.read();
203                 if(b1 != b2) {
204                     return false;
205                 }
206                 b1 = is1.read();
207             }
208
209             int b2 = is2.read();
210             if(-1 != b2) {
211                 return false;
212             }
213             return true;
214         }
215         catch (IOException JavaDoc ioe) {
216             return false;
217         }
218     }
219 }
220
Popular Tags