Skip to content

Using Java 11's readString and writeString on modern JREs #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,36 @@ limitations under the License.
</pluginManagement>
</build>
</profile>
<profile>
<id>jdk11+</id>
<activation>
<jdk>[11,)</jdk>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile-java-11</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>11</release>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/main/java11</compileSourceRoot>
</compileSourceRoots>
<multiReleaseOutput>true</multiReleaseOutput>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
<profile>
<id>plexus-release</id>
<build>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/codehaus/plexus/util/BaseFileUtils.java
Original file line number Diff line number Diff line change
@@ -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 );
}
}
20 changes: 3 additions & 17 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
* @author <a href="mailto:[email protected]">Jeff Turner</a>
*
*/
public class FileUtils
public class FileUtils extends BaseFileUtils
{
/**
* The number of bytes in a kilobyte.
Expand Down Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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.
*
Expand Down
29 changes: 29 additions & 0 deletions src/main/java11/org/codehaus/plexus/util/BaseFileUtils.java
Original file line number Diff line number Diff line change
@@ -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 );
}
}
}
9 changes: 4 additions & 5 deletions src/test/java/org/codehaus/plexus/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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] );
}
}

Expand Down Expand Up @@ -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 ) );

Expand Down