1 11 12 package org.eclipse.jdt.internal.apt.pluggable.core.filer; 13 14 import java.io.BufferedInputStream ; 15 import java.io.ByteArrayInputStream ; 16 import java.io.ByteArrayOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.util.Collection ; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.jdt.apt.core.internal.env.BinaryFileOutputStream; 24 import org.eclipse.jdt.apt.core.internal.util.FileSystemUtil; 25 import org.eclipse.jdt.internal.apt.pluggable.core.Apt6Plugin; 26 import org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeProcessingEnvImpl; 27 28 31 public class IdeNonSourceOutputStream extends ByteArrayOutputStream 32 { 33 private final IdeProcessingEnvImpl _env; 34 private final IFile _file; 35 private final Collection <IFile> _parentFiles; 36 37 public IdeNonSourceOutputStream(IdeProcessingEnvImpl env, IFile file, Collection <IFile> parentFiles) { 38 _env = env; 39 _file = file; 40 _parentFiles = parentFiles; 41 } 42 43 @Override 44 public void close() throws IOException { 45 super.close(); 46 47 InputStream contents = new ByteArrayInputStream (toByteArray()); 48 try { 49 50 boolean contentsChanged = true; 51 if (!_file.exists()) { 52 saveToDisk(contents, true); 53 } 54 else { 55 InputStream in = null; 56 InputStream oldData = null; 57 try { 58 in = new ByteArrayInputStream (toByteArray()); 60 oldData = new BufferedInputStream (_file.getContents()); 61 if (FileSystemUtil.compareStreams(in, oldData)) { 62 contentsChanged = false; 63 } 64 } 65 catch (CoreException ce) { 66 contentsChanged = true; 68 } 69 finally { 70 closeInputStream(in); 71 closeInputStream(oldData); 72 } 73 if (contentsChanged) { 74 contents.reset(); 75 saveToDisk(contents, false); 76 } 77 } 78 } 79 finally { 80 closeInputStream(contents); 81 } 82 83 if (_parentFiles != null && !_parentFiles.isEmpty()) { 85 _env.getAptProject().getGeneratedFileManager().addGeneratedFileDependency(_parentFiles, _file); 86 _env.addNewResource(_file); 87 } 88 } 89 90 private void closeInputStream(InputStream stream) { 91 if (stream != null) { 92 try { 93 stream.close(); 94 } 95 catch (IOException ioe) {} 96 } 97 } 98 99 private void saveToDisk(InputStream toSave, boolean create) throws IOException { 100 try { 101 FileSystemUtil.makeDerivedParentFolders(_file.getParent()); 102 if (create) { 103 _file.create(toSave, true, null); 104 _file.setDerived(true); 105 } 106 else { 107 _file.setContents(toSave, true, false, null); 108 } 109 } 110 catch (CoreException ce) { 111 if (_file.exists()) { 112 } 115 else { 116 Apt6Plugin.log(ce, "Could not create generated non-Java file " + _file.getName()); IOException ioe = new IOException (); 118 ioe.initCause(ce); 119 throw ioe; 120 } 121 } 122 } 123 124 } 125 | Popular Tags |