src/share/classes/com/sun/tools/sjavac/comp/SmartFileObject.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2227
998b10c43157
parent 0
959103a6100f
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2012, 2013, 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  */
    26 package com.sun.tools.sjavac.comp;
    28 import java.io.*;
    29 import java.net.URI;
    30 import javax.lang.model.element.Modifier;
    31 import javax.lang.model.element.NestingKind;
    32 import javax.tools.JavaFileObject;
    34 /**
    35  * The SmartFileObject will return an outputstream that cache the written data
    36  * and compare the new content with the old content on disk. Only if they differ,
    37  * will the file be updated.
    38  *
    39  * <p><b>This is NOT part of any supported API.
    40  * If you write code that depends on this, you do so at your own
    41  * risk.  This code and its internal interfaces are subject to change
    42  * or deletion without notice.</b></p>
    43  */
    44 public class SmartFileObject implements JavaFileObject {
    46     JavaFileObject file;
    47     PrintWriter stdout;
    49     public SmartFileObject(JavaFileObject r, PrintWriter pw) {
    50         file = r;
    51         stdout = pw;
    52     }
    54     @Override
    55     public boolean equals(Object other) {
    56         return file.equals(other);
    57     }
    59     @Override
    60     public int hashCode() {
    61         return file.hashCode();
    62     }
    64     public Kind getKind() {
    65         return file.getKind();
    66     }
    68     public boolean isNameCompatible(String simpleName, Kind kind) {
    69         return file.isNameCompatible(simpleName, kind);
    70     }
    72     public URI toUri() {
    73         return file.toUri();
    74     }
    76     public String getName() {
    77         return file.getName();
    78     }
    80     public InputStream openInputStream() throws IOException {
    81         return file.openInputStream();
    82     }
    84     public OutputStream openOutputStream() throws IOException {
    85         return file.openOutputStream();
    86     }
    88     public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    89         return file.getCharContent(ignoreEncodingErrors);
    90     }
    92     static String lineseparator = System.getProperty("line.separator");
    94     public Writer openWriter() throws IOException {
    95         StringBuilder s = new StringBuilder();
    96         try (BufferedReader r = new BufferedReader(file.openReader(true))) {
    97             while (r.ready()) {
    98                 s.append(r.readLine()+lineseparator);
    99             }
   100         } catch (FileNotFoundException e) {
   101             // Perfectly ok.
   102         }
   103         return new SmartWriter(file, s.toString(), file.getName(), stdout);
   104     }
   106     public long getLastModified() {
   107         return file.getLastModified();
   108     }
   110     public boolean delete() {
   111         return file.delete();
   112     }
   114     public Modifier getAccessLevel() {
   115         return file.getAccessLevel();
   116     }
   118     public NestingKind getNestingKind() {
   119         return file.getNestingKind();
   120     }
   122     public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
   123         return file.openReader(ignoreEncodingErrors);
   124     }
   126 }

mercurial