SmartFileObject.java revision 2674:e284f560acf6
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 java.nio.file.NoSuchFileException; 31 32import javax.lang.model.element.Modifier; 33import javax.lang.model.element.NestingKind; 34import javax.tools.JavaFileObject; 35 36import com.sun.tools.javac.util.DefinedBy; 37import 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 */ 49public class SmartFileObject implements JavaFileObject { 50 51 JavaFileObject file; 52 PrintWriter stdout; 53 54 public SmartFileObject(JavaFileObject r, PrintWriter pw) { 55 file = r; 56 stdout = pw; 57 } 58 59 @Override 60 public boolean equals(Object other) { 61 return file.equals(other); 62 } 63 64 @Override 65 public int hashCode() { 66 return file.hashCode(); 67 } 68 69 @DefinedBy(Api.COMPILER) 70 public Kind getKind() { 71 return file.getKind(); 72 } 73 74 @DefinedBy(Api.COMPILER) 75 public boolean isNameCompatible(String simpleName, Kind kind) { 76 return file.isNameCompatible(simpleName, kind); 77 } 78 79 @DefinedBy(Api.COMPILER) 80 public URI toUri() { 81 return file.toUri(); 82 } 83 84 @DefinedBy(Api.COMPILER) 85 public String getName() { 86 return file.getName(); 87 } 88 89 @DefinedBy(Api.COMPILER) 90 public InputStream openInputStream() throws IOException { 91 return file.openInputStream(); 92 } 93 94 @DefinedBy(Api.COMPILER) 95 public OutputStream openOutputStream() throws IOException { 96 return file.openOutputStream(); 97 } 98 99 @DefinedBy(Api.COMPILER) 100 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 101 return file.getCharContent(ignoreEncodingErrors); 102 } 103 104 static String lineseparator = System.getProperty("line.separator"); 105 106 @DefinedBy(Api.COMPILER) 107 public Writer openWriter() throws IOException { 108 StringBuilder s = new StringBuilder(); 109 try (BufferedReader r = new BufferedReader(file.openReader(true))) { 110 while (r.ready()) { 111 s.append(r.readLine()+lineseparator); 112 } 113 } catch (FileNotFoundException | NoSuchFileException e) { 114 // Perfectly ok. 115 } 116 return new SmartWriter(file, s.toString(), file.getName(), stdout); 117 } 118 119 @DefinedBy(Api.COMPILER) 120 public long getLastModified() { 121 return file.getLastModified(); 122 } 123 124 @DefinedBy(Api.COMPILER) 125 public boolean delete() { 126 return file.delete(); 127 } 128 129 @DefinedBy(Api.COMPILER) 130 public Modifier getAccessLevel() { 131 return file.getAccessLevel(); 132 } 133 134 @DefinedBy(Api.COMPILER) 135 public NestingKind getNestingKind() { 136 return file.getNestingKind(); 137 } 138 139 @DefinedBy(Api.COMPILER) 140 public Reader openReader(boolean ignoreEncodingErrors) throws IOException { 141 return file.openReader(ignoreEncodingErrors); 142 } 143 144} 145