CopyFile.java revision 3191:e9a7033b2546
1/*
2 * Copyright (c) 2012, 2016, 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 */
25
26package com.sun.tools.sjavac;
27
28import java.io.File;
29import java.io.FileInputStream;
30import java.io.FileOutputStream;
31import java.io.IOException;
32import java.io.InputStream;
33import java.io.OutputStream;
34import java.io.Writer;
35import java.net.URI;
36import java.util.HashSet;
37import java.util.Map;
38import java.util.Set;
39
40import com.sun.tools.sjavac.comp.CompilationService;
41import com.sun.tools.sjavac.options.Options;
42import com.sun.tools.sjavac.pubapi.PubApi;
43
44/**
45 * The copy file transform simply copies a matching file from -src to -d .
46 * Such files are typically images, xml documents and other data files.
47 *
48 *  <p><b>This is NOT part of any supported API.
49 *  If you write code that depends on this, you do so at your own risk.
50 *  This code and its internal interfaces are subject to change or
51 *  deletion without notice.</b>
52 */
53public class CopyFile implements Transformer {
54
55    public void setExtra(String e) {
56    }
57
58    public void setExtra(Options a) {
59    }
60
61    public boolean transform(CompilationService compilationService,
62                             Map<String,Set<URI>> pkgSrcs,
63                             Set<URI> visibleSrcs,
64                             Map<String,Set<String>> oldPackageDependents,
65                             URI destRoot,
66                             Map<String,Set<URI>>    packageArtifacts,
67                             Map<String,Map<String, Set<String>>> packageDependencies,
68                             Map<String,Map<String, Set<String>>> packageCpDependencies,
69                             Map<String, PubApi> packagePubapis,
70                             Map<String, PubApi> dependencyPubapis,
71                             int debugLevel,
72                             boolean incremental,
73                             int numCores,
74                             Writer out,
75                             Writer err)
76    {
77        boolean rc = true;
78        String dest_filename;
79        File dest;
80
81        for (String pkgName : pkgSrcs.keySet()) {
82            String pkgNameF = Util.toFileSystemPath(pkgName);
83            for (URI u : pkgSrcs.get(pkgName)) {
84                File src = new File(u);
85                File destDir;
86                destDir = new File(destRoot.getPath()+File.separator+pkgNameF);
87                dest_filename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();
88                dest = new File(dest_filename);
89
90                if (!destDir.isDirectory()) {
91                    if (!destDir.mkdirs()) {
92                       Log.error("Error: The copier could not create the directory "+
93                                           destDir.getPath());
94                        return false;
95                    }
96                }
97
98                Set<URI> as = packageArtifacts.get(pkgName);
99                if (as == null) {
100                    as = new HashSet<>();
101                    packageArtifacts.put(pkgName, as);
102                }
103                as.add(dest.toURI());
104
105                if (dest.exists() && dest.lastModified() > src.lastModified()) {
106                    // A copied file exists, and its timestamp is newer than the source.
107                    continue;
108                }
109
110                Log.info("Copying "+pkgNameF+File.separator+src.getName());
111
112                try (InputStream fin = new FileInputStream(src);
113                     OutputStream fout = new FileOutputStream(dest)) {
114                    byte[] buf = new byte[1024];
115                    int len;
116                    while ((len = fin.read(buf)) > 0){
117                        fout.write(buf, 0, len);
118                    }
119                }
120                catch(IOException e){
121                    Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath());
122                    rc = false;
123                }
124            }
125        }
126        return rc;
127    }
128}
129