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