langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/SmartWriter.java
author vasya
Mon, 14 Dec 2015 20:18:19 +0100
changeset 34752 9c262a013456
parent 26107 a4a156a33c94
child 36162 e5c4e9ab3cdd
permissions -rw-r--r--
8145342: Some copyright notices are inconsistently and ill formatted Summary: Adjusted formatting of copyright notices. Reviewed-by: darcy, alundblad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     1
/*
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 22448
diff changeset
     2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     4
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    10
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    15
 * accompanied this code).
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    16
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    20
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    23
 * questions.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    24
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 26107
diff changeset
    25
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    26
package com.sun.tools.sjavac.comp;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    27
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    28
import java.io.*;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    29
import javax.tools.JavaFileObject;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    30
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    31
/**
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    32
 * The SmartWriter will cache the written data and when the writer is closed,
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    33
 * then it will compare the cached data with the old_content string.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    34
 * If different, then it will write all the new content to the file.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    35
 * If not, the file is not touched.
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    36
 *
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    37
 *  <p><b>This is NOT part of any supported API.
26098
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 22448
diff changeset
    38
 *  If you write code that depends on this, you do so at your own risk.
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 22448
diff changeset
    39
 *  This code and its internal interfaces are subject to change or
32588700060b 8048457: Sjavac should not use portfiles, sockets, etc if background=false
alundblad
parents: 22448
diff changeset
    40
 *  deletion without notice.</b>
15368
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    41
 */
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    42
public class SmartWriter extends Writer {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    43
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    44
    String name;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    45
    JavaFileObject file;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    46
    String oldContent;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    47
    StringWriter newContent = new StringWriter();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    48
    PrintWriter stdout;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    49
    boolean closed;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    50
    public SmartWriter(JavaFileObject f, String s, String n, PrintWriter pw) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    51
        name = n;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    52
        file = f;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    53
        oldContent = s;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    54
        newContent = new StringWriter();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    55
        stdout = pw;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    56
        closed = false;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    57
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    58
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    59
    public void write(char[] chars, int i, int i1)
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    60
    {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    61
        newContent.write(chars, i, i1);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    62
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    63
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    64
    public void close() throws IOException {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    65
        if (closed) return;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    66
        closed = true;
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    67
        String s = newContent.toString();
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    68
        if (!oldContent.equals(s)) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    69
            int p = file.getName().lastIndexOf(File.separatorChar);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    70
            try (Writer writer = file.openWriter()) {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    71
                writer.write(s);
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    72
            }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    73
            stdout.println("Writing "+file.getName().substring(p+1));
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    74
        }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    75
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    76
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    77
    public void flush() throws IOException {
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    78
    }
2577ddb7e710 8004658: Add internal smart javac wrapper to solve JEP 139
ohrstrom
parents:
diff changeset
    79
}