ohrstrom@1504: /* ohrstrom@1504: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. ohrstrom@1504: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohrstrom@1504: * ohrstrom@1504: * This code is free software; you can redistribute it and/or modify it ohrstrom@1504: * under the terms of the GNU General Public License version 2 only, as ohrstrom@1504: * published by the Free Software Foundation. Oracle designates this ohrstrom@1504: * particular file as subject to the "Classpath" exception as provided ohrstrom@1504: * by Oracle in the LICENSE file that accompanied this code. ohrstrom@1504: * ohrstrom@1504: * This code is distributed in the hope that it will be useful, but WITHOUT ohrstrom@1504: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohrstrom@1504: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohrstrom@1504: * version 2 for more details (a copy is included in the LICENSE file that ohrstrom@1504: * accompanied this code). ohrstrom@1504: * ohrstrom@1504: * You should have received a copy of the GNU General Public License version ohrstrom@1504: * 2 along with this work; if not, write to the Free Software Foundation, ohrstrom@1504: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohrstrom@1504: * ohrstrom@1504: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohrstrom@1504: * or visit www.oracle.com if you need additional information or have any ohrstrom@1504: * questions. ohrstrom@1504: */ ohrstrom@1504: package com.sun.tools.sjavac.comp; ohrstrom@1504: ohrstrom@1504: import java.io.*; ohrstrom@1504: import javax.tools.JavaFileObject; ohrstrom@1504: ohrstrom@1504: /** ohrstrom@1504: * The SmartWriter will cache the written data and when the writer is closed, ohrstrom@1504: * then it will compare the cached data with the old_content string. ohrstrom@1504: * If different, then it will write all the new content to the file. ohrstrom@1504: * If not, the file is not touched. ohrstrom@1504: * ohrstrom@1504: *

This is NOT part of any supported API. ohrstrom@1504: * If you write code that depends on this, you do so at your own ohrstrom@1504: * risk. This code and its internal interfaces are subject to change ohrstrom@1504: * or deletion without notice.

ohrstrom@1504: */ ohrstrom@1504: public class SmartWriter extends Writer { ohrstrom@1504: ohrstrom@1504: String name; ohrstrom@1504: JavaFileObject file; ohrstrom@1504: String oldContent; ohrstrom@1504: StringWriter newContent = new StringWriter(); ohrstrom@1504: PrintWriter stdout; ohrstrom@1504: boolean closed; ohrstrom@1504: public SmartWriter(JavaFileObject f, String s, String n, PrintWriter pw) { ohrstrom@1504: name = n; ohrstrom@1504: file = f; ohrstrom@1504: oldContent = s; ohrstrom@1504: newContent = new StringWriter(); ohrstrom@1504: stdout = pw; ohrstrom@1504: closed = false; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public void write(char[] chars, int i, int i1) ohrstrom@1504: { ohrstrom@1504: newContent.write(chars, i, i1); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public void close() throws IOException { ohrstrom@1504: if (closed) return; ohrstrom@1504: closed = true; ohrstrom@1504: String s = newContent.toString(); ohrstrom@1504: if (!oldContent.equals(s)) { ohrstrom@1504: int p = file.getName().lastIndexOf(File.separatorChar); ohrstrom@1504: try (Writer writer = file.openWriter()) { ohrstrom@1504: writer.write(s); ohrstrom@1504: } ohrstrom@1504: stdout.println("Writing "+file.getName().substring(p+1)); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: public void flush() throws IOException { ohrstrom@1504: } ohrstrom@1504: }