Skip to content

Commit 33725e7

Browse files
committed
Using Files.readString / writeString on JRE 11
1 parent f2c7c6c commit 33725e7

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.codehaus.plexus.util;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
7+
/**
8+
* Implementation specific to Java SE 8 version.
9+
*/
10+
abstract class BaseFileUtils
11+
{
12+
static String fileRead( Path path, String encoding ) throws IOException
13+
{
14+
byte[] bytes = Files.readAllBytes( path );
15+
return encoding != null ? new String( bytes, encoding ) : new String( bytes );
16+
}
17+
18+
static void fileWrite( Path path, String encoding, String data ) throws IOException
19+
{
20+
byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes();
21+
Files.write( path, bytes );
22+
}
23+
}

src/main/java/org/codehaus/plexus/util/FileUtils.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
* @author <a href="mailto:[email protected]">Jeff Turner</a>
115115
*
116116
*/
117-
public class FileUtils
117+
public class FileUtils extends BaseFileUtils
118118
{
119119
/**
120120
* The number of bytes in a kilobyte.
@@ -358,13 +358,6 @@ public static String fileRead( File file, String encoding )
358358
return fileRead( file.toPath(), encoding );
359359
}
360360

361-
private static String fileRead( Path path, String encoding )
362-
throws IOException
363-
{
364-
byte[] bytes = Files.readAllBytes( path );
365-
return encoding != null ? new String( bytes, encoding ) : new String( bytes );
366-
}
367-
368361
/**
369362
* Appends data to a file. The file will be created if it does not exist. Note: the data is written with platform
370363
* encoding
@@ -467,13 +460,6 @@ public static void fileWrite( File file, String encoding, String data )
467460
fileWrite( file.toPath(), encoding, data );
468461
}
469462

470-
private static void fileWrite( Path path, String encoding, String data )
471-
throws IOException
472-
{
473-
byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes();
474-
Files.write( path, bytes );
475-
}
476-
477463
/**
478464
* Deletes a file.
479465
*
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.codehaus.plexus.util;
2+
3+
import java.io.IOException;
4+
import java.nio.charset.Charset;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
8+
/**
9+
* Implementation specific to Java SE 11 version.
10+
*/
11+
abstract class BaseFileUtils
12+
{
13+
static String fileRead( Path path, String encoding ) throws IOException
14+
{
15+
return encoding != null ? Files.readString( path, Charset.forName( encoding ) ) : Files.readString( path );
16+
}
17+
18+
static void fileWrite( Path path, String encoding, String data ) throws IOException
19+
{
20+
if ( encoding != null )
21+
{
22+
Files.writeString( path, data, Charset.forName( encoding ) );
23+
}
24+
else
25+
{
26+
Files.writeString( path, data );
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)