src/share/classes/com/sun/tools/javac/file/RegularFileObject.java

changeset 57
aa67a5da66e3
child 144
173162d6eb1d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java	Wed Jun 18 07:23:25 2008 -0700
     1.3 @@ -0,0 +1,204 @@
     1.4 +/*
     1.5 + * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.file;
    1.30 +
    1.31 +import java.io.File;
    1.32 +import java.io.FileInputStream;
    1.33 +import java.io.FileOutputStream;
    1.34 +import java.io.IOException;
    1.35 +import java.io.InputStream;
    1.36 +import java.io.OutputStream;
    1.37 +import java.io.OutputStreamWriter;
    1.38 +import java.io.Writer;
    1.39 +import java.net.URI;
    1.40 +import java.net.URISyntaxException;
    1.41 +import java.nio.ByteBuffer;
    1.42 +import java.nio.CharBuffer;
    1.43 +import java.nio.charset.CharsetDecoder;
    1.44 +import javax.tools.JavaFileObject;
    1.45 +
    1.46 +/**
    1.47 + * A subclass of JavaFileObject representing regular files.
    1.48 + */
    1.49 +class RegularFileObject extends BaseFileObject {
    1.50 +
    1.51 +    /** Have the parent directories been created?
    1.52 +     */
    1.53 +    private boolean hasParents = false;
    1.54 +    private String name;
    1.55 +    final File f;
    1.56 +
    1.57 +    public RegularFileObject(JavacFileManager fileManager, File f) {
    1.58 +        this(fileManager, f.getName(), f);
    1.59 +    }
    1.60 +
    1.61 +    public RegularFileObject(JavacFileManager fileManager, String name, File f) {
    1.62 +        super(fileManager);
    1.63 +        if (f.isDirectory()) {
    1.64 +            throw new IllegalArgumentException("directories not supported");
    1.65 +        }
    1.66 +        this.name = name;
    1.67 +        this.f = f;
    1.68 +    }
    1.69 +
    1.70 +    public InputStream openInputStream() throws IOException {
    1.71 +        return new FileInputStream(f);
    1.72 +    }
    1.73 +
    1.74 +    protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
    1.75 +        return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
    1.76 +    }
    1.77 +
    1.78 +    public OutputStream openOutputStream() throws IOException {
    1.79 +        ensureParentDirectoriesExist();
    1.80 +        return new FileOutputStream(f);
    1.81 +    }
    1.82 +
    1.83 +    public Writer openWriter() throws IOException {
    1.84 +        ensureParentDirectoriesExist();
    1.85 +        return new OutputStreamWriter(new FileOutputStream(f), fileManager.getEncodingName());
    1.86 +    }
    1.87 +
    1.88 +    @Override
    1.89 +    protected String inferBinaryName(Iterable<? extends File> path) {
    1.90 +        String fPath = f.getPath();
    1.91 +        //System.err.println("RegularFileObject " + file + " " +r.getPath());
    1.92 +        for (File dir: path) {
    1.93 +            //System.err.println("dir: " + dir);
    1.94 +            String dPath = dir.getPath();
    1.95 +            if (!dPath.endsWith(File.separator))
    1.96 +                dPath += File.separator;
    1.97 +            if (fPath.regionMatches(true, 0, dPath, 0, dPath.length())
    1.98 +                && new File(fPath.substring(0, dPath.length())).equals(new File(dPath))) {
    1.99 +                String relativeName = fPath.substring(dPath.length());
   1.100 +                return removeExtension(relativeName).replace(File.separatorChar, '.');
   1.101 +            }
   1.102 +        }
   1.103 +        return null;
   1.104 +    }
   1.105 +
   1.106 +    private void ensureParentDirectoriesExist() throws IOException {
   1.107 +        if (!hasParents) {
   1.108 +            File parent = f.getParentFile();
   1.109 +            if (parent != null && !parent.exists()) {
   1.110 +                if (!parent.mkdirs()) {
   1.111 +                    if (!parent.exists() || !parent.isDirectory()) {
   1.112 +                        throw new IOException("could not create parent directories");
   1.113 +                    }
   1.114 +                }
   1.115 +            }
   1.116 +            hasParents = true;
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +    @Deprecated
   1.121 +    public String getName() {
   1.122 +        return name;
   1.123 +    }
   1.124 +
   1.125 +    public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) {
   1.126 +        cn.getClass();
   1.127 +        // null check
   1.128 +        if (kind == Kind.OTHER && getKind() != kind) {
   1.129 +            return false;
   1.130 +        }
   1.131 +        String n = cn + kind.extension;
   1.132 +        if (name.equals(n)) {
   1.133 +            return true;
   1.134 +        }
   1.135 +        if (name.equalsIgnoreCase(n)) {
   1.136 +            try {
   1.137 +                // allow for Windows
   1.138 +                return f.getCanonicalFile().getName().equals(n);
   1.139 +            } catch (IOException e) {
   1.140 +            }
   1.141 +        }
   1.142 +        return false;
   1.143 +    }
   1.144 +
   1.145 +    @Deprecated
   1.146 +    public String getPath() {
   1.147 +        return f.getPath();
   1.148 +    }
   1.149 +
   1.150 +    public long getLastModified() {
   1.151 +        return f.lastModified();
   1.152 +    }
   1.153 +
   1.154 +    public boolean delete() {
   1.155 +        return f.delete();
   1.156 +    }
   1.157 +
   1.158 +    public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.159 +        CharBuffer cb = fileManager.getCachedContent(this);
   1.160 +        if (cb == null) {
   1.161 +            InputStream in = new FileInputStream(f);
   1.162 +            try {
   1.163 +                ByteBuffer bb = fileManager.makeByteBuffer(in);
   1.164 +                JavaFileObject prev = fileManager.log.useSource(this);
   1.165 +                try {
   1.166 +                    cb = fileManager.decode(bb, ignoreEncodingErrors);
   1.167 +                } finally {
   1.168 +                    fileManager.log.useSource(prev);
   1.169 +                }
   1.170 +                fileManager.recycleByteBuffer(bb);
   1.171 +                if (!ignoreEncodingErrors) {
   1.172 +                    fileManager.cache(this, cb);
   1.173 +                }
   1.174 +            } finally {
   1.175 +                in.close();
   1.176 +            }
   1.177 +        }
   1.178 +        return cb;
   1.179 +    }
   1.180 +
   1.181 +    @Override
   1.182 +    public boolean equals(Object other) {
   1.183 +        if (!(other instanceof RegularFileObject)) {
   1.184 +            return false;
   1.185 +        }
   1.186 +        RegularFileObject o = (RegularFileObject) other;
   1.187 +        try {
   1.188 +            return f.equals(o.f) || f.getCanonicalFile().equals(o.f.getCanonicalFile());
   1.189 +        } catch (IOException e) {
   1.190 +            return false;
   1.191 +        }
   1.192 +    }
   1.193 +
   1.194 +    @Override
   1.195 +    public int hashCode() {
   1.196 +        return f.hashCode();
   1.197 +    }
   1.198 +
   1.199 +    public URI toUri() {
   1.200 +        try {
   1.201 +            String path = f.getAbsolutePath().replace(File.separatorChar, '/');
   1.202 +            return new URI("file://" + path).normalize();
   1.203 +        } catch (URISyntaxException ex) {
   1.204 +            return f.toURI();
   1.205 +        }
   1.206 +    }
   1.207 +}

mercurial