SmartFileObject.java revision 2601:8e638f046bf0
1/* 2 * Copyright (c) 2012, 2014, 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 26package com.sun.tools.sjavac.comp; 27 28import java.io.*; 29import java.net.URI; 30import javax.lang.model.element.Modifier; 31import javax.lang.model.element.NestingKind; 32import javax.tools.JavaFileObject; 33 34import com.sun.tools.javac.util.DefinedBy; 35import com.sun.tools.javac.util.DefinedBy.Api; 36 37/** 38 * The SmartFileObject will return an outputstream that cache the written data 39 * and compare the new content with the old content on disk. Only if they differ, 40 * will the file be updated. 41 * 42 * <p><b>This is NOT part of any supported API. 43 * If you write code that depends on this, you do so at your own risk. 44 * This code and its internal interfaces are subject to change or 45 * deletion without notice.</b> 46 */ 47public class SmartFileObject implements JavaFileObject { 48 49 JavaFileObject file; 50 PrintWriter stdout; 51 52 public SmartFileObject(JavaFileObject r, PrintWriter pw) { 53 file = r; 54 stdout = pw; 55 } 56 57 @Override 58 public boolean equals(Object other) { 59 return file.equals(other); 60 } 61 62 @Override 63 public int hashCode() { 64 return file.hashCode(); 65 } 66 67 @DefinedBy(Api.COMPILER) 68 public Kind getKind() { 69 return file.getKind(); 70 } 71 72 @DefinedBy(Api.COMPILER) 73 public boolean isNameCompatible(String simpleName, Kind kind) { 74 return file.isNameCompatible(simpleName, kind); 75 } 76 77 @DefinedBy(Api.COMPILER) 78 public URI toUri() { 79 return file.toUri(); 80 } 81 82 @DefinedBy(Api.COMPILER) 83 public String getName() { 84 return file.getName(); 85 } 86 87 @DefinedBy(Api.COMPILER) 88 public InputStream openInputStream() throws IOException { 89 return file.openInputStream(); 90 } 91 92 @DefinedBy(Api.COMPILER) 93 public OutputStream openOutputStream() throws IOException { 94 return file.openOutputStream(); 95 } 96 97 @DefinedBy(Api.COMPILER) 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) 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 e) { 112 // Perfectly ok. 113 } 114 return new SmartWriter(file, s.toString(), file.getName(), stdout); 115 } 116 117 @DefinedBy(Api.COMPILER) 118 public long getLastModified() { 119 return file.getLastModified(); 120 } 121 122 @DefinedBy(Api.COMPILER) 123 public boolean delete() { 124 return file.delete(); 125 } 126 127 @DefinedBy(Api.COMPILER) 128 public Modifier getAccessLevel() { 129 return file.getAccessLevel(); 130 } 131 132 @DefinedBy(Api.COMPILER) 133 public NestingKind getNestingKind() { 134 return file.getNestingKind(); 135 } 136 137 @DefinedBy(Api.COMPILER) 138 public Reader openReader(boolean ignoreEncodingErrors) throws IOException { 139 return file.openReader(ignoreEncodingErrors); 140 } 141 142} 143