|
| 1 | +package org.fugerit.java.core.jvfs; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.fugerit.java.core.io.StreamIO; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +public class JFileCopyFacade { |
| 10 | + |
| 11 | + private final static Logger logger = LoggerFactory.getLogger( JFileCopyFacade.class ); |
| 12 | + |
| 13 | + public static int copyFile( JFile from, JFile to ) throws IOException { |
| 14 | + return copyFile(from, to, false); |
| 15 | + } |
| 16 | + |
| 17 | + public static int copyFile( JFile from, JFile to, boolean recurse ) throws IOException { |
| 18 | + return copyFile(from, to, recurse, false); |
| 19 | + } |
| 20 | + |
| 21 | + public static int copyFile( JFile from, JFile to, boolean recurse, boolean force ) throws IOException { |
| 22 | + return copyFile(from, to, recurse, force, false); |
| 23 | + } |
| 24 | + |
| 25 | + public static int copyFileRecurse( JFile from, JFile to ) throws IOException { |
| 26 | + return copyFile(from, to, true ); |
| 27 | + } |
| 28 | + |
| 29 | + public static int copyFileRecurseForce( JFile from, JFile to ) throws IOException { |
| 30 | + return copyFile(from, to, true, true ); |
| 31 | + } |
| 32 | + |
| 33 | + public static int copyFileRecurseForceVerbose( JFile from, JFile to ) throws IOException { |
| 34 | + return copyFile(from, to, true, true, true ); |
| 35 | + } |
| 36 | + |
| 37 | + public static int copyFile( JFile from, JFile to, boolean recurse, boolean force, boolean verbose ) throws IOException { |
| 38 | + int res = 1; |
| 39 | + if ( verbose ) { |
| 40 | + logger.info( "copyFile( recurse:"+recurse+", force:"+force+" ) {} -> {}", from, to ); |
| 41 | + } |
| 42 | + if ( to.exists() && !force ) { |
| 43 | + throw new IOException( "You can overwrite existing files only with the 'force' flag" ); |
| 44 | + } else { |
| 45 | + if ( from.isFile() && to.isFile() ) { |
| 46 | + StreamIO.pipeStream( from.getInputStream() , to.getOutputStream(), StreamIO.MODE_CLOSE_BOTH ); |
| 47 | + } else if ( from.isDirectory() && to.isDirectory() ) { |
| 48 | + if ( recurse ) { |
| 49 | + to.mkdir(); |
| 50 | + JFile[] list = from.listFiles(); |
| 51 | + for ( int k=0; k<list.length; k++ ) { |
| 52 | + JFile kidFrom = list[k]; |
| 53 | + JFile kidTo = to.getChild( kidFrom.getName() ); |
| 54 | + res+= copyFile( kidFrom, kidTo, recurse, force, verbose); |
| 55 | + } |
| 56 | + } else { |
| 57 | + throw new IOException( "Directories can only be copied recursively, from:("+from+") to:("+to+")" ); |
| 58 | + } |
| 59 | + } else { |
| 60 | + if ( verbose ) { |
| 61 | + logger.warn( "from {} -> isDirectory:{}", from, from.isDirectory() ); |
| 62 | + logger.warn( "to {} -> isDirectory:{}", to, to.isDirectory() ); |
| 63 | + } |
| 64 | + throw new IOException( "You can only copy a two files or two directories, from:("+from+") to:("+to+")" ); |
| 65 | + } |
| 66 | + } |
| 67 | + return res; |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments