Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6aa1b6f

Browse filesBrowse files
mkargmichael-o
authored andcommitted
Using Files#readString()/#writeString() on Java 11+
This closes #233
1 parent c415448 commit 6aa1b6f
Copy full SHA for 6aa1b6f

File tree

Expand file treeCollapse file tree

5 files changed

+89
-22
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+89
-22
lines changed

‎pom.xml

Copy file name to clipboardExpand all lines: pom.xml
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,36 @@ limitations under the License.
218218
</pluginManagement>
219219
</build>
220220
</profile>
221+
<profile>
222+
<id>jdk11+</id>
223+
<activation>
224+
<jdk>[11,)</jdk>
225+
</activation>
226+
<build>
227+
<pluginManagement>
228+
<plugins>
229+
<plugin>
230+
<artifactId>maven-compiler-plugin</artifactId>
231+
<executions>
232+
<execution>
233+
<id>compile-java-11</id>
234+
<goals>
235+
<goal>compile</goal>
236+
</goals>
237+
<configuration>
238+
<release>11</release>
239+
<compileSourceRoots>
240+
<compileSourceRoot>${project.basedir}/src/main/java11</compileSourceRoot>
241+
</compileSourceRoots>
242+
<multiReleaseOutput>true</multiReleaseOutput>
243+
</configuration>
244+
</execution>
245+
</executions>
246+
</plugin>
247+
</plugins>
248+
</pluginManagement>
249+
</build>
250+
</profile>
221251
<profile>
222252
<id>plexus-release</id>
223253
<build>
+23Lines 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

Copy file name to clipboardExpand all lines: src/main/java/org/codehaus/plexus/util/FileUtils.java
+3-17Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
* @author <a href="mailto:jefft@codehaus.org">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.
@@ -330,7 +330,7 @@ public static String fileRead( String file )
330330
public static String fileRead( String file, String encoding )
331331
throws IOException
332332
{
333-
return fileRead( new File( file ), encoding );
333+
return fileRead( Paths.get( file ), encoding );
334334
}
335335

336336
/**
@@ -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
@@ -433,7 +426,7 @@ public static void fileWrite( String fileName, String data )
433426
public static void fileWrite( String fileName, String encoding, String data )
434427
throws IOException
435428
{
436-
File file = ( fileName == null ) ? null : new File( fileName );
429+
Path file = ( fileName == null ) ? null : Paths.get( fileName );
437430
fileWrite( file, encoding, data );
438431
}
439432

@@ -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
*
+29Lines 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+
}

‎src/test/java/org/codehaus/plexus/util/FileUtilsTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/codehaus/plexus/util/FileUtilsTest.java
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.io.Writer;
3434
import java.net.URL;
3535
import java.nio.file.Files;
36+
import java.nio.file.Paths;
3637
import java.util.Properties;
3738

3839
import org.junit.Before;
@@ -191,7 +192,7 @@ public void testToURLs()
191192

192193
for ( int i = 0; i < urls.length; i++ )
193194
{
194-
assertEquals( files[i].toURL(), urls[i] );
195+
assertEquals( files[i].toURI().toURL(), urls[i] );
195196
}
196197
}
197198

@@ -885,14 +886,12 @@ public void testFileUtils()
885886
final URL url = this.getClass().getResource( path );
886887
assertNotNull( path + " was not found.", url );
887888

888-
String filename = url.getFile();
889-
// The following line applies a fix for spaces in a path
890-
filename = replaceAll( filename, "%20", " " );
889+
final String filename = Paths.get(url.toURI()).toString();
891890
final String filename2 = "test2.txt";
892891

893892
assertTrue( "test.txt extension == \"txt\"", FileUtils.getExtension( filename ).equals( "txt" ) );
894893

895-
assertTrue( "Test file does not exist: " + filename, FileUtils.fileExists( filename ) );
894+
assertTrue( "Test file does exist: " + filename, FileUtils.fileExists( filename ) );
896895

897896
assertTrue( "Second test file does not exist", !FileUtils.fileExists( filename2 ) );
898897

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.