1 11 package org.eclipse.jdt.apt.core.internal.util; 12 13 import java.io.BufferedOutputStream ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.ByteArrayOutputStream ; 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileOutputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 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 35 public final class FileSystemUtil 36 { 37 private FileSystemUtil() {} 38 39 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 else 76 return false; 77 } 78 79 82 private static void deleteResource(IResource resource) { 83 try { 84 resource.delete(true, null); 85 } catch (CoreException e) { 86 if (resource.exists()) { 88 AptPlugin.log(e, "Unable to delete derived resource " + resource); } 90 } 91 } 92 93 public static void mkdirs( File parent ) 94 { 95 if ( parent == null ) 96 return; 97 98 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 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 if (!container.exists()) { 120 throw e; 121 } 122 } 123 container.setDerived(true); 124 } 125 } 126 127 130 public static String getContentsOfIFile(IFile file) throws IOException , CoreException { 131 return getContents(file.getContents(true)); 132 } 133 134 public static String getContentsOfFile(File file) throws IOException { 135 return getContents(new FileInputStream (file)); 136 } 137 138 private static String getContents(InputStream in) throws IOException { 139 try { 140 ByteArrayOutputStream out = new ByteArrayOutputStream (); 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 s = new String (out.toByteArray(), "UTF8"); return s; 149 } 150 finally { 151 try {in.close();} catch (IOException ioe) {} 152 } 153 } 154 155 160 public static void writeStringToIFile(IFile file, String contents) throws IOException , CoreException { 161 byte[] data = contents.getBytes("UTF8"); ByteArrayInputStream input = new ByteArrayInputStream (data); 163 if (file.exists()) { 164 if (file.isReadOnly()) { 165 ResourcesPlugin.getWorkspace().validateEdit(new IFile[]{file}, null); 167 } 168 file.setContents(input, true, false, null); 169 } 170 else { 171 file.create(input, IResource.FORCE, null); 173 } 174 } 175 176 181 public static void writeStringToFile(File file, String contents) throws IOException { 182 byte[] data = contents.getBytes("UTF8"); OutputStream out = new BufferedOutputStream (new FileOutputStream (file)); 184 try { 185 for (byte b : data) { 186 out.write(b); 187 } 188 } 189 finally { 190 try {out.close();} catch (IOException ioe) {} 191 } 192 } 193 194 198 public static boolean compareStreams(InputStream is1, InputStream 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 ioe) { 216 return false; 217 } 218 } 219 } 220 | Popular Tags |