KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > apt > pluggable > core > filer > IdeNonSourceOutputStream


1 /*******************************************************************************
2  * Copyright (c) 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  * wharley@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.apt.pluggable.core.filer;
13
14 import java.io.BufferedInputStream JavaDoc;
15 import java.io.ByteArrayInputStream JavaDoc;
16 import java.io.ByteArrayOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.util.Collection JavaDoc;
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 /**
29  * @see BinaryFileOutputStream
30  */

31 public class IdeNonSourceOutputStream extends ByteArrayOutputStream JavaDoc
32 {
33     private final IdeProcessingEnvImpl _env;
34     private final IFile _file;
35     private final Collection JavaDoc<IFile> _parentFiles;
36     
37     public IdeNonSourceOutputStream(IdeProcessingEnvImpl env, IFile file, Collection JavaDoc<IFile> parentFiles) {
38         _env = env;
39         _file = file;
40         _parentFiles = parentFiles;
41     }
42
43     @Override JavaDoc
44     public void close() throws IOException JavaDoc {
45         super.close();
46         
47         InputStream JavaDoc contents = new ByteArrayInputStream JavaDoc(toByteArray());
48         try {
49             
50             boolean contentsChanged = true;
51             if (!_file.exists()) {
52                 saveToDisk(contents, true);
53             }
54             else {
55                 InputStream JavaDoc in = null;
56                 InputStream JavaDoc oldData = null;
57                 try {
58                     // Only write the contents if the data is different
59
in = new ByteArrayInputStream JavaDoc(toByteArray());
60                     oldData = new BufferedInputStream JavaDoc(_file.getContents());
61                     if (FileSystemUtil.compareStreams(in, oldData)) {
62                         contentsChanged = false;
63                     }
64                 }
65                 catch (CoreException ce) {
66                     // Ignore -- couldn't read the old data, so assume it's different
67
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 there are no parents, we don't need to track dependencies
84
if (_parentFiles != null && !_parentFiles.isEmpty()) {
85             _env.getAptProject().getGeneratedFileManager().addGeneratedFileDependency(_parentFiles, _file);
86             _env.addNewResource(_file);
87         }
88     }
89     
90     private void closeInputStream(InputStream JavaDoc stream) {
91         if (stream != null) {
92             try {
93                 stream.close();
94             }
95             catch (IOException JavaDoc ioe) {}
96         }
97     }
98     
99     private void saveToDisk(InputStream JavaDoc toSave, boolean create) throws IOException JavaDoc{
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                 // Do nothing. This is a case-insensitive file system mismatch,
113
// and the underlying platform has saved the contents already.
114
}
115             else {
116                 Apt6Plugin.log(ce, "Could not create generated non-Java file " + _file.getName()); //$NON-NLS-1$
117
IOException JavaDoc ioe = new IOException JavaDoc();
118                 ioe.initCause(ce);
119                 throw ioe;
120             }
121         }
122     }
123
124 }
125
Popular Tags