diff --git a/pom.xml b/pom.xml index 1f69f863..79f4bef1 100644 --- a/pom.xml +++ b/pom.xml @@ -218,6 +218,36 @@ limitations under the License. + + jdk11+ + + [11,) + + + + + + maven-compiler-plugin + + + compile-java-11 + + compile + + + 11 + + ${project.basedir}/src/main/java11 + + true + + + + + + + + plexus-release diff --git a/src/main/java/org/codehaus/plexus/util/BaseFileUtils.java b/src/main/java/org/codehaus/plexus/util/BaseFileUtils.java new file mode 100644 index 00000000..f7c7fa52 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/util/BaseFileUtils.java @@ -0,0 +1,23 @@ +package org.codehaus.plexus.util; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +/** + * Implementation specific to Java SE 8 version. + */ +abstract class BaseFileUtils +{ + static String fileRead( Path path, String encoding ) throws IOException + { + byte[] bytes = Files.readAllBytes( path ); + return encoding != null ? new String( bytes, encoding ) : new String( bytes ); + } + + static void fileWrite( Path path, String encoding, String data ) throws IOException + { + byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes(); + Files.write( path, bytes ); + } +} diff --git a/src/main/java/org/codehaus/plexus/util/FileUtils.java b/src/main/java/org/codehaus/plexus/util/FileUtils.java index d7e61f83..42999307 100644 --- a/src/main/java/org/codehaus/plexus/util/FileUtils.java +++ b/src/main/java/org/codehaus/plexus/util/FileUtils.java @@ -114,7 +114,7 @@ * @author Jeff Turner * */ -public class FileUtils +public class FileUtils extends BaseFileUtils { /** * The number of bytes in a kilobyte. @@ -330,7 +330,7 @@ public static String fileRead( String file ) public static String fileRead( String file, String encoding ) throws IOException { - return fileRead( new File( file ), encoding ); + return fileRead( Paths.get( file ), encoding ); } /** @@ -358,13 +358,6 @@ public static String fileRead( File file, String encoding ) return fileRead( file.toPath(), encoding ); } - private static String fileRead( Path path, String encoding ) - throws IOException - { - byte[] bytes = Files.readAllBytes( path ); - return encoding != null ? new String( bytes, encoding ) : new String( bytes ); - } - /** * Appends data to a file. The file will be created if it does not exist. Note: the data is written with platform * encoding @@ -433,7 +426,7 @@ public static void fileWrite( String fileName, String data ) public static void fileWrite( String fileName, String encoding, String data ) throws IOException { - File file = ( fileName == null ) ? null : new File( fileName ); + Path file = ( fileName == null ) ? null : Paths.get( fileName ); fileWrite( file, encoding, data ); } @@ -467,13 +460,6 @@ public static void fileWrite( File file, String encoding, String data ) fileWrite( file.toPath(), encoding, data ); } - private static void fileWrite( Path path, String encoding, String data ) - throws IOException - { - byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes(); - Files.write( path, bytes ); - } - /** * Deletes a file. * diff --git a/src/main/java11/org/codehaus/plexus/util/BaseFileUtils.java b/src/main/java11/org/codehaus/plexus/util/BaseFileUtils.java new file mode 100644 index 00000000..50e46a9e --- /dev/null +++ b/src/main/java11/org/codehaus/plexus/util/BaseFileUtils.java @@ -0,0 +1,29 @@ +package org.codehaus.plexus.util; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; + +/** + * Implementation specific to Java SE 11 version. + */ +abstract class BaseFileUtils +{ + static String fileRead( Path path, String encoding ) throws IOException + { + return encoding != null ? Files.readString( path, Charset.forName( encoding ) ) : Files.readString( path ); + } + + static void fileWrite( Path path, String encoding, String data ) throws IOException + { + if ( encoding != null ) + { + Files.writeString( path, data, Charset.forName( encoding ) ); + } + else + { + Files.writeString( path, data ); + } + } +} diff --git a/src/test/java/org/codehaus/plexus/util/FileUtilsTest.java b/src/test/java/org/codehaus/plexus/util/FileUtilsTest.java index 4e9093d0..4d63957c 100644 --- a/src/test/java/org/codehaus/plexus/util/FileUtilsTest.java +++ b/src/test/java/org/codehaus/plexus/util/FileUtilsTest.java @@ -33,6 +33,7 @@ import java.io.Writer; import java.net.URL; import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Properties; import org.junit.Before; @@ -191,7 +192,7 @@ public void testToURLs() for ( int i = 0; i < urls.length; i++ ) { - assertEquals( files[i].toURL(), urls[i] ); + assertEquals( files[i].toURI().toURL(), urls[i] ); } } @@ -885,14 +886,12 @@ public void testFileUtils() final URL url = this.getClass().getResource( path ); assertNotNull( path + " was not found.", url ); - String filename = url.getFile(); - // The following line applies a fix for spaces in a path - filename = replaceAll( filename, "%20", " " ); + final String filename = Paths.get(url.toURI()).toString(); final String filename2 = "test2.txt"; assertTrue( "test.txt extension == \"txt\"", FileUtils.getExtension( filename ).equals( "txt" ) ); - assertTrue( "Test file does not exist: " + filename, FileUtils.fileExists( filename ) ); + assertTrue( "Test file does exist: " + filename, FileUtils.fileExists( filename ) ); assertTrue( "Second test file does not exist", !FileUtils.fileExists( filename2 ) );