Skip to content

Commit 3ef2e69

Browse files
committed
Predicate base copy file jvfs method
1 parent 8a8af69 commit 3ef2e69

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

fj-core-jvfs/src/main/java/org/fugerit/java/core/jvfs/util/JFileUtilCP.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.ByteArrayInputStream;
44
import java.io.IOException;
5+
import java.util.function.Predicate;
56

67
import org.fugerit.java.core.io.StreamIO;
78
import org.fugerit.java.core.jvfs.JFile;
@@ -36,8 +37,12 @@ public static int copyFileRecurseForce( JFile from, JFile to ) throws IOExceptio
3637
public static int copyFileRecurseForceVerbose( JFile from, JFile to ) throws IOException {
3738
return copyFile(from, to, true, true, true );
3839
}
39-
40-
public static int copyFile( JFile from, JFile to, boolean recurse, boolean force, boolean verbose ) throws IOException {
40+
41+
public static int copyFile(JFile from, JFile to, boolean recurse, boolean force, boolean verbose) throws IOException {
42+
return copyFile( from, to, recurse, force, verbose, ( f ) -> true );
43+
}
44+
45+
public static int copyFile(JFile from, JFile to, boolean recurse, boolean force, boolean verbose, Predicate<JFile> filter) throws IOException {
4146
int res = 1;
4247
if ( verbose ) {
4348
logger.info( "copyFile( recurse:"+recurse+", force:"+force+" ) {} -> {}", from, to );
@@ -48,19 +53,21 @@ public static int copyFile( JFile from, JFile to, boolean recurse, boolean force
4853
if ( from.isDirectory() && to.isDirectory() ) {
4954
if ( recurse ) {
5055
if ( !to.exists() ) {
51-
to.mkdir();
56+
to.mkdir();
5257
}
5358
for ( JFile kidFrom : from.lsFiles() ) {
54-
res+= copyFile(kidFrom, to.getChild( kidFrom.getName() ), recurse, force, verbose);
59+
if ( filter.test( kidFrom ) ) {
60+
res+= copyFile(kidFrom, to.getChild( kidFrom.getName() ), recurse, force, verbose, filter);
61+
}
5562
}
5663
} else {
5764
throw new IOException( "Directories can only be copied recursively, from:("+from+") to:("+to+")" );
5865
}
5966
} else {
6067
// We want to create a blank file if the from input stream is null
61-
StreamIO.pipeStream(
62-
ObjectUtils.objectWithDefault( from.getInputStream() , new ByteArrayInputStream( "".getBytes() ) ) ,
63-
to.getOutputStream(),
68+
StreamIO.pipeStream(
69+
ObjectUtils.objectWithDefault( from.getInputStream() , new ByteArrayInputStream( "".getBytes() ) ) ,
70+
to.getOutputStream(),
6471
StreamIO.MODE_CLOSE_BOTH );
6572
}
6673
}

0 commit comments

Comments
 (0)