CopyFile.java revision 3265:b7583d50f67d
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    {
75        boolean rc = true;
76        String dest_filename;
77        File dest;
78
79        for (String pkgName : pkgSrcs.keySet()) {
80            String pkgNameF = Util.toFileSystemPath(pkgName);
81            for (URI u : pkgSrcs.get(pkgName)) {
82                File src = new File(u);
83                File destDir;
84                destDir = new File(destRoot.getPath()+File.separator+pkgNameF);
85                dest_filename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();
86                dest = new File(dest_filename);
87
88                if (!destDir.isDirectory()) {
89                    if (!destDir.mkdirs()) {
90                       Log.error("Error: The copier could not create the directory "+
91                                           destDir.getPath());
92                        return false;
93                    }
94                }
95
96                Set<URI> as = packageArtifacts.get(pkgName);
97                if (as == null) {
98                    as = new HashSet<>();
99                    packageArtifacts.put(pkgName, as);
100                }
101                as.add(dest.toURI());
102
103                if (dest.exists() && dest.lastModified() > src.lastModified()) {
104                    // A copied file exists, and its timestamp is newer than the source.
105                    continue;
106                }
107
108                Log.info("Copying "+pkgNameF+File.separator+src.getName());
109
110                try (InputStream fin = new FileInputStream(src);
111                     OutputStream fout = new FileOutputStream(dest)) {
112                    byte[] buf = new byte[1024];
113                    int len;
114                    while ((len = fin.read(buf)) > 0){
115                        fout.write(buf, 0, len);
116                    }
117                }
118                catch(IOException e){
119                    Log.error("Could not copy the file "+src.getPath()+" to "+dest.getPath());
120                    rc = false;
121                }
122            }
123        }
124        return rc;
125    }
126}
127