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

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 1504
22e417cdddee
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

ohrstrom@1504 1 /*
ksrini@2227 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504 4 *
ohrstrom@1504 5 * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504 6 * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504 7 * published by the Free Software Foundation. Oracle designates this
ohrstrom@1504 8 * particular file as subject to the "Classpath" exception as provided
ohrstrom@1504 9 * by Oracle in the LICENSE file that accompanied this code.
ohrstrom@1504 10 *
ohrstrom@1504 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504 14 * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504 15 * accompanied this code).
ohrstrom@1504 16 *
ohrstrom@1504 17 * You should have received a copy of the GNU General Public License version
ohrstrom@1504 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504 20 *
ohrstrom@1504 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504 22 * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504 23 * questions.
ohrstrom@1504 24 */
ohrstrom@1504 25
ohrstrom@1504 26 package com.sun.tools.sjavac.comp;
ohrstrom@1504 27
ohrstrom@1504 28 import java.io.*;
ohrstrom@1504 29 import java.net.URI;
ohrstrom@1504 30 import javax.lang.model.element.Modifier;
ohrstrom@1504 31 import javax.lang.model.element.NestingKind;
ohrstrom@1504 32 import javax.tools.JavaFileObject;
ohrstrom@1504 33
ohrstrom@1504 34 /**
ohrstrom@1504 35 * The SmartFileObject will return an outputstream that cache the written data
ohrstrom@1504 36 * and compare the new content with the old content on disk. Only if they differ,
ohrstrom@1504 37 * will the file be updated.
ohrstrom@1504 38 *
ohrstrom@1504 39 * <p><b>This is NOT part of any supported API.
ohrstrom@1504 40 * If you write code that depends on this, you do so at your own
ohrstrom@1504 41 * risk. This code and its internal interfaces are subject to change
ohrstrom@1504 42 * or deletion without notice.</b></p>
ohrstrom@1504 43 */
ohrstrom@1504 44 public class SmartFileObject implements JavaFileObject {
ohrstrom@1504 45
ohrstrom@1504 46 JavaFileObject file;
ohrstrom@1504 47 PrintWriter stdout;
ohrstrom@1504 48
ohrstrom@1504 49 public SmartFileObject(JavaFileObject r, PrintWriter pw) {
ohrstrom@1504 50 file = r;
ohrstrom@1504 51 stdout = pw;
ohrstrom@1504 52 }
ohrstrom@1504 53
ohrstrom@1504 54 @Override
ohrstrom@1504 55 public boolean equals(Object other) {
ohrstrom@1504 56 return file.equals(other);
ohrstrom@1504 57 }
ohrstrom@1504 58
ohrstrom@1504 59 @Override
ohrstrom@1504 60 public int hashCode() {
ohrstrom@1504 61 return file.hashCode();
ohrstrom@1504 62 }
ohrstrom@1504 63
ohrstrom@1504 64 public Kind getKind() {
ohrstrom@1504 65 return file.getKind();
ohrstrom@1504 66 }
ohrstrom@1504 67
ohrstrom@1504 68 public boolean isNameCompatible(String simpleName, Kind kind) {
ohrstrom@1504 69 return file.isNameCompatible(simpleName, kind);
ohrstrom@1504 70 }
ohrstrom@1504 71
ohrstrom@1504 72 public URI toUri() {
ohrstrom@1504 73 return file.toUri();
ohrstrom@1504 74 }
ohrstrom@1504 75
ohrstrom@1504 76 public String getName() {
ohrstrom@1504 77 return file.getName();
ohrstrom@1504 78 }
ohrstrom@1504 79
ohrstrom@1504 80 public InputStream openInputStream() throws IOException {
ohrstrom@1504 81 return file.openInputStream();
ohrstrom@1504 82 }
ohrstrom@1504 83
ohrstrom@1504 84 public OutputStream openOutputStream() throws IOException {
ohrstrom@1504 85 return file.openOutputStream();
ohrstrom@1504 86 }
ohrstrom@1504 87
ohrstrom@1504 88 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
ohrstrom@1504 89 return file.getCharContent(ignoreEncodingErrors);
ohrstrom@1504 90 }
ohrstrom@1504 91
ohrstrom@1504 92 static String lineseparator = System.getProperty("line.separator");
ohrstrom@1504 93
ohrstrom@1504 94 public Writer openWriter() throws IOException {
ohrstrom@1504 95 StringBuilder s = new StringBuilder();
ohrstrom@1504 96 try (BufferedReader r = new BufferedReader(file.openReader(true))) {
ohrstrom@1504 97 while (r.ready()) {
ohrstrom@1504 98 s.append(r.readLine()+lineseparator);
ohrstrom@1504 99 }
ohrstrom@1504 100 } catch (FileNotFoundException e) {
ohrstrom@1504 101 // Perfectly ok.
ohrstrom@1504 102 }
ohrstrom@1504 103 return new SmartWriter(file, s.toString(), file.getName(), stdout);
ohrstrom@1504 104 }
ohrstrom@1504 105
ohrstrom@1504 106 public long getLastModified() {
ohrstrom@1504 107 return file.getLastModified();
ohrstrom@1504 108 }
ohrstrom@1504 109
ohrstrom@1504 110 public boolean delete() {
ohrstrom@1504 111 return file.delete();
ohrstrom@1504 112 }
ohrstrom@1504 113
ohrstrom@1504 114 public Modifier getAccessLevel() {
ohrstrom@1504 115 return file.getAccessLevel();
ohrstrom@1504 116 }
ohrstrom@1504 117
ohrstrom@1504 118 public NestingKind getNestingKind() {
ohrstrom@1504 119 return file.getNestingKind();
ohrstrom@1504 120 }
ohrstrom@1504 121
ohrstrom@1504 122 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
ohrstrom@1504 123 return file.openReader(ignoreEncodingErrors);
ohrstrom@1504 124 }
ohrstrom@1504 125
ohrstrom@1504 126 }

mercurial