langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/SmartWriter.java
changeset 36162 e5c4e9ab3cdd
parent 34752 9c262a013456
equal deleted inserted replaced
36161:a025c0619f25 36162:e5c4e9ab3cdd
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package com.sun.tools.sjavac.comp;
    26 package com.sun.tools.sjavac.comp;
    27 
    27 
       
    28 import com.sun.tools.sjavac.Log;
       
    29 
    28 import java.io.*;
    30 import java.io.*;
    29 import javax.tools.JavaFileObject;
    31 import javax.tools.JavaFileObject;
    30 
    32 
    31 /**
    33 /**
    32  * The SmartWriter will cache the written data and when the writer is closed,
    34  * The SmartWriter will cache the written data and when the writer is closed,
    43 
    45 
    44     String name;
    46     String name;
    45     JavaFileObject file;
    47     JavaFileObject file;
    46     String oldContent;
    48     String oldContent;
    47     StringWriter newContent = new StringWriter();
    49     StringWriter newContent = new StringWriter();
    48     PrintWriter stdout;
       
    49     boolean closed;
    50     boolean closed;
    50     public SmartWriter(JavaFileObject f, String s, String n, PrintWriter pw) {
    51 
       
    52     public SmartWriter(JavaFileObject f, String s, String n) {
    51         name = n;
    53         name = n;
    52         file = f;
    54         file = f;
    53         oldContent = s;
    55         oldContent = s;
    54         newContent = new StringWriter();
    56         newContent = new StringWriter();
    55         stdout = pw;
       
    56         closed = false;
    57         closed = false;
    57     }
    58     }
    58 
    59 
    59     public void write(char[] chars, int i, int i1)
    60     public void write(char[] chars, int i, int i1) {
    60     {
       
    61         newContent.write(chars, i, i1);
    61         newContent.write(chars, i, i1);
    62     }
    62     }
    63 
    63 
    64     public void close() throws IOException {
    64     public void close() throws IOException {
    65         if (closed) return;
    65         if (closed) return;
    68         if (!oldContent.equals(s)) {
    68         if (!oldContent.equals(s)) {
    69             int p = file.getName().lastIndexOf(File.separatorChar);
    69             int p = file.getName().lastIndexOf(File.separatorChar);
    70             try (Writer writer = file.openWriter()) {
    70             try (Writer writer = file.openWriter()) {
    71                 writer.write(s);
    71                 writer.write(s);
    72             }
    72             }
    73             stdout.println("Writing "+file.getName().substring(p+1));
    73             Log.debug("Writing " + file.getName().substring(p + 1));
    74         }
    74         }
    75     }
    75     }
    76 
    76 
    77     public void flush() throws IOException {
    77     public void flush() throws IOException {
    78     }
    78     }