langtools/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java
changeset 3995 73af8b6fb8bc
parent 3782 ae62279eeb46
child 3998 c66be272f350
equal deleted inserted replaced
3994:7df1ecd5eadb 3995:73af8b6fb8bc
    66         }
    66         }
    67         this.name = name;
    67         this.name = name;
    68         this.f = f;
    68         this.f = f;
    69     }
    69     }
    70 
    70 
       
    71     @Override
       
    72     public URI toUri() {
       
    73         return f.toURI().normalize();
       
    74     }
       
    75 
       
    76     @Override
       
    77     public String getName() {
       
    78         return f.getPath();
       
    79     }
       
    80 
       
    81     @Override
       
    82     public String getShortName() {
       
    83         return name;
       
    84     }
       
    85 
       
    86     @Override
       
    87     public JavaFileObject.Kind getKind() {
       
    88         return getKind(name);
       
    89     }
       
    90 
       
    91     @Override
    71     public InputStream openInputStream() throws IOException {
    92     public InputStream openInputStream() throws IOException {
    72         return new FileInputStream(f);
    93         return new FileInputStream(f);
    73     }
    94     }
    74 
    95 
    75     @Override
    96     @Override
    76     protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
       
    77         return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
       
    78     }
       
    79 
       
    80     public OutputStream openOutputStream() throws IOException {
    97     public OutputStream openOutputStream() throws IOException {
    81         ensureParentDirectoriesExist();
    98         ensureParentDirectoriesExist();
    82         return new FileOutputStream(f);
    99         return new FileOutputStream(f);
    83     }
   100     }
    84 
   101 
    85     public Writer openWriter() throws IOException {
   102     @Override
    86         ensureParentDirectoriesExist();
       
    87         return new OutputStreamWriter(new FileOutputStream(f), fileManager.getEncodingName());
       
    88     }
       
    89 
       
    90     @Override
       
    91     protected String inferBinaryName(Iterable<? extends File> path) {
       
    92         String fPath = f.getPath();
       
    93         //System.err.println("RegularFileObject " + file + " " +r.getPath());
       
    94         for (File dir: path) {
       
    95             //System.err.println("dir: " + dir);
       
    96             String dPath = dir.getPath();
       
    97             if (dPath.length() == 0)
       
    98                 dPath = System.getProperty("user.dir");
       
    99             if (!dPath.endsWith(File.separator))
       
   100                 dPath += File.separator;
       
   101             if (fPath.regionMatches(true, 0, dPath, 0, dPath.length())
       
   102                 && new File(fPath.substring(0, dPath.length())).equals(new File(dPath))) {
       
   103                 String relativeName = fPath.substring(dPath.length());
       
   104                 return removeExtension(relativeName).replace(File.separatorChar, '.');
       
   105             }
       
   106         }
       
   107         return null;
       
   108     }
       
   109 
       
   110     private void ensureParentDirectoriesExist() throws IOException {
       
   111         if (!hasParents) {
       
   112             File parent = f.getParentFile();
       
   113             if (parent != null && !parent.exists()) {
       
   114                 if (!parent.mkdirs()) {
       
   115                     if (!parent.exists() || !parent.isDirectory()) {
       
   116                         throw new IOException("could not create parent directories");
       
   117                     }
       
   118                 }
       
   119             }
       
   120             hasParents = true;
       
   121         }
       
   122     }
       
   123 
       
   124     @Deprecated
       
   125     public String getName() {
       
   126         return name;
       
   127     }
       
   128 
       
   129     public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) {
       
   130         cn.getClass();
       
   131         // null check
       
   132         if (kind == Kind.OTHER && getKind() != kind) {
       
   133             return false;
       
   134         }
       
   135         String n = cn + kind.extension;
       
   136         if (name.equals(n)) {
       
   137             return true;
       
   138         }
       
   139         if (name.equalsIgnoreCase(n)) {
       
   140             try {
       
   141                 // allow for Windows
       
   142                 return f.getCanonicalFile().getName().equals(n);
       
   143             } catch (IOException e) {
       
   144             }
       
   145         }
       
   146         return false;
       
   147     }
       
   148 
       
   149     @Deprecated
       
   150     @Override
       
   151     public String getPath() {
       
   152         return f.getPath();
       
   153     }
       
   154 
       
   155     public long getLastModified() {
       
   156         return f.lastModified();
       
   157     }
       
   158 
       
   159     public boolean delete() {
       
   160         return f.delete();
       
   161     }
       
   162 
       
   163     public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
   103     public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
   164         CharBuffer cb = fileManager.getCachedContent(this);
   104         CharBuffer cb = fileManager.getCachedContent(this);
   165         if (cb == null) {
   105         if (cb == null) {
   166             InputStream in = new FileInputStream(f);
   106             InputStream in = new FileInputStream(f);
   167             try {
   107             try {
   182         }
   122         }
   183         return cb;
   123         return cb;
   184     }
   124     }
   185 
   125 
   186     @Override
   126     @Override
       
   127     public Writer openWriter() throws IOException {
       
   128         ensureParentDirectoriesExist();
       
   129         return new OutputStreamWriter(new FileOutputStream(f), fileManager.getEncodingName());
       
   130     }
       
   131 
       
   132     @Override
       
   133     public long getLastModified() {
       
   134         return f.lastModified();
       
   135     }
       
   136 
       
   137     @Override
       
   138     public boolean delete() {
       
   139         return f.delete();
       
   140     }
       
   141 
       
   142     @Override
       
   143     protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
       
   144         return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
       
   145     }
       
   146 
       
   147     @Override
       
   148     protected String inferBinaryName(Iterable<? extends File> path) {
       
   149         String fPath = f.getPath();
       
   150         //System.err.println("RegularFileObject " + file + " " +r.getPath());
       
   151         for (File dir: path) {
       
   152             //System.err.println("dir: " + dir);
       
   153             String dPath = dir.getPath();
       
   154             if (dPath.length() == 0)
       
   155                 dPath = System.getProperty("user.dir");
       
   156             if (!dPath.endsWith(File.separator))
       
   157                 dPath += File.separator;
       
   158             if (fPath.regionMatches(true, 0, dPath, 0, dPath.length())
       
   159                 && new File(fPath.substring(0, dPath.length())).equals(new File(dPath))) {
       
   160                 String relativeName = fPath.substring(dPath.length());
       
   161                 return removeExtension(relativeName).replace(File.separatorChar, '.');
       
   162             }
       
   163         }
       
   164         return null;
       
   165     }
       
   166 
       
   167     @Override
       
   168     public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) {
       
   169         cn.getClass();
       
   170         // null check
       
   171         if (kind == Kind.OTHER && getKind() != kind) {
       
   172             return false;
       
   173         }
       
   174         String n = cn + kind.extension;
       
   175         if (name.equals(n)) {
       
   176             return true;
       
   177         }
       
   178         if (name.equalsIgnoreCase(n)) {
       
   179             try {
       
   180                 // allow for Windows
       
   181                 return f.getCanonicalFile().getName().equals(n);
       
   182             } catch (IOException e) {
       
   183             }
       
   184         }
       
   185         return false;
       
   186     }
       
   187 
       
   188     private void ensureParentDirectoriesExist() throws IOException {
       
   189         if (!hasParents) {
       
   190             File parent = f.getParentFile();
       
   191             if (parent != null && !parent.exists()) {
       
   192                 if (!parent.mkdirs()) {
       
   193                     if (!parent.exists() || !parent.isDirectory()) {
       
   194                         throw new IOException("could not create parent directories");
       
   195                     }
       
   196                 }
       
   197             }
       
   198             hasParents = true;
       
   199         }
       
   200     }
       
   201 
       
   202     @Override
   187     public boolean equals(Object other) {
   203     public boolean equals(Object other) {
   188         if (!(other instanceof RegularFileObject)) {
   204         if (!(other instanceof RegularFileObject)) {
   189             return false;
   205             return false;
   190         }
   206         }
   191         RegularFileObject o = (RegularFileObject) other;
   207         RegularFileObject o = (RegularFileObject) other;
   198 
   214 
   199     @Override
   215     @Override
   200     public int hashCode() {
   216     public int hashCode() {
   201         return f.hashCode();
   217         return f.hashCode();
   202     }
   218     }
   203 
       
   204     public URI toUri() {
       
   205         return f.toURI().normalize();
       
   206     }
       
   207 }
   219 }