KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > env > BinaryFileOutputStream


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  * jgarms@bea.com - initial API and implementation
10  *
11  *******************************************************************************/

12 package org.eclipse.jdt.apt.core.internal.env;
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.Collections JavaDoc;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.jdt.apt.core.internal.AptPlugin;
24 import org.eclipse.jdt.apt.core.internal.util.FileSystemUtil;
25
26 /**
27  * Wrap output operations, caching them in memory,
28  * then writing them out at the end if the content
29  * is different than what is on disk
30  */

31 public class BinaryFileOutputStream extends ByteArrayOutputStream JavaDoc {
32
33     protected final IFile _file;
34     private final BuildEnv _env;
35     
36     public BinaryFileOutputStream(IFile file, BuildEnv env) {
37         _file = file;
38         _env = env;
39     }
40     
41     @Override JavaDoc
42     public void close() throws IOException JavaDoc {
43         super.close();
44         
45         InputStream JavaDoc contents = new ByteArrayInputStream JavaDoc(toByteArray());
46         try {
47             
48             boolean contentsChanged = true;
49             if (!_file.exists()) {
50                 saveToDisk(contents, true);
51             }
52             else {
53                 InputStream JavaDoc in = null;
54                 InputStream JavaDoc oldData = null;
55                 try {
56                     // Only write the contents if the data is different
57
in = new ByteArrayInputStream JavaDoc(toByteArray());
58                     oldData = new BufferedInputStream JavaDoc(_file.getContents());
59                     if (FileSystemUtil.compareStreams(in, oldData)) {
60                         contentsChanged = false;
61                     }
62                 }
63                 catch (CoreException ce) {
64                     // Ignore -- couldn't read the old data, so assume it's different
65
contentsChanged = true;
66                 }
67                 finally {
68                     closeInputStream(in);
69                     closeInputStream(oldData);
70                 }
71                 if (contentsChanged) {
72                     contents.reset();
73                     saveToDisk(contents, false);
74                 }
75             }
76         }
77         finally {
78             closeInputStream(contents);
79         }
80             
81         IFile parentFile = _env.getFile();
82         if (parentFile != null) {
83             _env.getAptProject().getGeneratedFileManager().addGeneratedFileDependency(Collections.singleton(parentFile), _file);
84             _env.addGeneratedNonSourceFile(_file);
85         }
86     }
87     
88     private void closeInputStream(InputStream JavaDoc stream) {
89         if (stream != null) {
90             try {
91                 stream.close();
92             }
93             catch (IOException JavaDoc ioe) {}
94         }
95     }
96     
97     private void saveToDisk(InputStream JavaDoc toSave, boolean create) throws IOException JavaDoc{
98         try {
99             FileSystemUtil.makeDerivedParentFolders(_file.getParent());
100             if (create) {
101                 _file.create(toSave, true, null);
102                 _file.setDerived(true);
103             }
104             else {
105                 _file.setContents(toSave, true, false, null);
106             }
107         }
108         catch (CoreException ce) {
109             if (_file.exists()) {
110                 // Do nothing. This is a case-insensitive file system mismatch,
111
// and the underlying platform has saved the contents already.
112
}
113             else {
114                 AptPlugin.log(ce, "Could not create generated file"); //$NON-NLS-1$
115
throw new IOException JavaDoc(ce.getMessage());
116             }
117         }
118     }
119
120 }
121
Popular Tags