1 /*
2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.tools.sjavac.comp;
27 
28 import java.io.*;
29 import java.net.URI;
30 import java.nio.file.NoSuchFileException;
31 
32 import javax.lang.model.element.Modifier;
33 import javax.lang.model.element.NestingKind;
34 import javax.tools.JavaFileObject;
35 
36 import com.sun.tools.javac.util.DefinedBy;
37 import com.sun.tools.javac.util.DefinedBy.Api;
38 
39 /**
40  * The SmartFileObject will return an outputstream that cache the written data
41  * and compare the new content with the old content on disk. Only if they differ,
42  * will the file be updated.
43  *
44  *  <p><b>This is NOT part of any supported API.
45  *  If you write code that depends on this, you do so at your own risk.
46  *  This code and its internal interfaces are subject to change or
47  *  deletion without notice.</b>
48  */
49 public class SmartFileObject implements JavaFileObject {
50 
51     JavaFileObject file;
52 
SmartFileObject(JavaFileObject r)53     public SmartFileObject(JavaFileObject r) {
54         file = r;
55     }
56 
57     @Override
equals(Object other)58     public boolean equals(Object other) {
59         return file.equals(other);
60     }
61 
62     @Override
hashCode()63     public int hashCode() {
64         return file.hashCode();
65     }
66 
67     @DefinedBy(Api.COMPILER)
getKind()68     public Kind getKind() {
69         return file.getKind();
70     }
71 
72     @DefinedBy(Api.COMPILER)
isNameCompatible(String simpleName, Kind kind)73     public boolean isNameCompatible(String simpleName, Kind kind) {
74         return file.isNameCompatible(simpleName, kind);
75     }
76 
77     @DefinedBy(Api.COMPILER)
toUri()78     public URI toUri() {
79         return file.toUri();
80     }
81 
82     @DefinedBy(Api.COMPILER)
getName()83     public String getName() {
84         return file.getName();
85     }
86 
87     @DefinedBy(Api.COMPILER)
openInputStream()88     public InputStream openInputStream() throws IOException {
89         return file.openInputStream();
90     }
91 
92     @DefinedBy(Api.COMPILER)
openOutputStream()93     public OutputStream openOutputStream() throws IOException {
94         return file.openOutputStream();
95     }
96 
97     @DefinedBy(Api.COMPILER)
getCharContent(boolean ignoreEncodingErrors)98     public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
99         return file.getCharContent(ignoreEncodingErrors);
100     }
101 
102     static String lineseparator = System.getProperty("line.separator");
103 
104     @DefinedBy(Api.COMPILER)
openWriter()105     public Writer openWriter() throws IOException {
106         StringBuilder s = new StringBuilder();
107         try (BufferedReader r = new BufferedReader(file.openReader(true))) {
108             while (r.ready()) {
109                 s.append(r.readLine()+lineseparator);
110             }
111         } catch (FileNotFoundException | NoSuchFileException e) {
112             // Perfectly ok.
113         }
114         return new SmartWriter(file, s.toString(), file.getName());
115     }
116 
117     @DefinedBy(Api.COMPILER)
getLastModified()118     public long getLastModified() {
119         return file.getLastModified();
120     }
121 
122     @DefinedBy(Api.COMPILER)
delete()123     public boolean delete() {
124         return file.delete();
125     }
126 
127     @DefinedBy(Api.COMPILER)
getAccessLevel()128     public Modifier getAccessLevel() {
129         return file.getAccessLevel();
130     }
131 
132     @DefinedBy(Api.COMPILER)
getNestingKind()133     public NestingKind getNestingKind() {
134         return file.getNestingKind();
135     }
136 
137     @DefinedBy(Api.COMPILER)
openReader(boolean ignoreEncodingErrors)138     public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
139         return file.openReader(ignoreEncodingErrors);
140     }
141 
142 }
143