src/share/classes/com/sun/tools/sjavac/CopyFile.java

changeset 1504
22e417cdddee
child 2227
998b10c43157
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/sjavac/CopyFile.java	Fri Jan 18 00:16:21 2013 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. 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.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.sjavac;
    1.30 +
    1.31 +import java.io.*;
    1.32 +import java.net.URI;
    1.33 +import java.util.Set;
    1.34 +import java.util.HashSet;
    1.35 +import java.util.Map;
    1.36 +
    1.37 +/**
    1.38 + * The copy file transform simply copies a matching file from -src to -d .
    1.39 + * Such files are typically images, xml documents and other data files.
    1.40 + *
    1.41 + * <p><b>This is NOT part of any supported API.
    1.42 + * If you write code that depends on this, you do so at your own
    1.43 + * risk.  This code and its internal interfaces are subject to change
    1.44 + * or deletion without notice.</b></p>
    1.45 + */
    1.46 +public class CopyFile implements Transformer {
    1.47 +
    1.48 +    public void setExtra(String e) {
    1.49 +    }
    1.50 +
    1.51 +    public void setExtra(String[] a) {
    1.52 +    }
    1.53 +
    1.54 +    public boolean transform(Map<String,Set<URI>> pkgSrcs,
    1.55 +                             Set<URI> visibleSrcs,
    1.56 +                             Map<URI,Set<String>> visibleClasses,
    1.57 +                             Map<String,Set<String>> oldPackageDependents,
    1.58 +                             URI destRoot,
    1.59 +                             Map<String,Set<URI>>    packageArtifacts,
    1.60 +                             Map<String,Set<String>> packageDependencies,
    1.61 +                             Map<String,String>      packagePubapis,
    1.62 +                             int debugLevel,
    1.63 +                             boolean incremental,
    1.64 +                             int numCores,
    1.65 +                             PrintStream out,
    1.66 +                             PrintStream err)
    1.67 +    {
    1.68 +        boolean rc = true;
    1.69 +        String dest_filename;
    1.70 +        File dest;
    1.71 +
    1.72 +        for (String pkgName : pkgSrcs.keySet()) {
    1.73 +            String pkgNameF = Util.toFileSystemPath(pkgName);
    1.74 +            for (URI u : pkgSrcs.get(pkgName)) {
    1.75 +                File src = new File(u);
    1.76 +                File destDir;
    1.77 +                destDir = new File(destRoot.getPath()+File.separator+pkgNameF);
    1.78 +                dest_filename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();
    1.79 +                dest = new File(dest_filename);
    1.80 +
    1.81 +                if (!destDir.isDirectory()) {
    1.82 +                    if (!destDir.mkdirs()) {
    1.83 +                       Log.error("Error: The copier could not create the directory "+
    1.84 +                                           destDir.getPath());
    1.85 +                        return false;
    1.86 +                    }
    1.87 +                }
    1.88 +
    1.89 +                Set<URI> as = packageArtifacts.get(pkgName);
    1.90 +                if (as == null) {
    1.91 +                    as = new HashSet<URI>();
    1.92 +                    packageArtifacts.put(pkgName, as);
    1.93 +                }
    1.94 +                as.add(dest.toURI());
    1.95 +
    1.96 +                if (dest.exists() && dest.lastModified() > src.lastModified()) {
    1.97 +                    // A copied file exists, and its timestamp is newer than the source.
    1.98 +                    continue;
    1.99 +                }
   1.100 +
   1.101 +                Log.info("Copying "+pkgNameF+File.separator+src.getName());
   1.102 +
   1.103 +                try (InputStream fin = new FileInputStream(src);
   1.104 +                     OutputStream fout = new FileOutputStream(dest)) {
   1.105 +                    byte[] buf = new byte[1024];
   1.106 +                    int len;
   1.107 +                    while ((len = fin.read(buf)) > 0){
   1.108 +                        fout.write(buf, 0, len);
   1.109 +                    }
   1.110 +                }
   1.111 +                catch(IOException e){
   1.112 +                    Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath());
   1.113 +                    rc = false;
   1.114 +                }
   1.115 +            }
   1.116 +        }
   1.117 +        return rc;
   1.118 +    }
   1.119 +}

mercurial