Skip to content

Commit

Permalink
Java 8: Files.newBufferedWriter instead of new FileWriter
Browse files Browse the repository at this point in the history
Provides potentially better performance.

Signed-off-by: Markus KARG <markus@headcrashing.eu>
  • Loading branch information
mkarg committed Dec 23, 2020
1 parent 4bc0050 commit 6ca093b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Expand Up @@ -60,7 +60,6 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -2253,7 +2252,7 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
if ( encoding == null || encoding.length() < 1 )
{
fileReader = Files.newBufferedReader( from.toPath() );
fileWriter = new FileWriter( to );
fileWriter = Files.newBufferedWriter( to.toPath() );
}
else
{
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/codehaus/plexus/util/WriterFactory.java
Expand Up @@ -17,8 +17,6 @@
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
Expand Down Expand Up @@ -145,7 +143,7 @@ public static Writer newPlatformWriter( OutputStream out )
public static Writer newPlatformWriter( File file )
throws IOException
{
return new FileWriter( file );
return Files.newBufferedWriter( file.toPath() );
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/org/codehaus/plexus/util/IOUtilTest.java
Expand Up @@ -22,7 +22,6 @@

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -178,7 +177,7 @@ public void testInputStreamToWriter()
{
File destination = newFile( "copy2.txt" );
InputStream fin = Files.newInputStream( testFile.toPath() );
FileWriter fout = new FileWriter( destination );
Writer fout = Files.newBufferedWriter( destination.toPath() );

IOUtil.copy( fin, fout );

Expand Down Expand Up @@ -232,7 +231,7 @@ public void testReaderToWriter()
{
File destination = newFile( "copy4.txt" );
Reader fin = Files.newBufferedReader( testFile.toPath() );
FileWriter fout = new FileWriter( destination );
Writer fout = Files.newBufferedWriter( destination.toPath() );
IOUtil.copy( fin, fout );

fout.flush();
Expand Down Expand Up @@ -286,7 +285,7 @@ public void testStringToWriter()
Reader fin = Files.newBufferedReader( testFile.toPath() );
// Create our String. Rely on testReaderToString() to make sure this is valid.
String str = IOUtil.toString( fin );
FileWriter fout = new FileWriter( destination );
Writer fout = Files.newBufferedWriter( destination.toPath() );
IOUtil.copy( str, fout );
fout.flush();

Expand Down Expand Up @@ -330,7 +329,7 @@ public void testByteArrayToWriter()
throws Exception
{
File destination = newFile( "copy7.txt" );
FileWriter fout = new FileWriter( destination );
Writer fout = Files.newBufferedWriter( destination.toPath() );
InputStream fin = Files.newInputStream( testFile.toPath() );

// Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
Expand Down
Expand Up @@ -21,9 +21,10 @@
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.Os;
Expand Down Expand Up @@ -443,10 +444,10 @@ public void testDollarSignInArgumentPath()
assertTrue( "Can't create dir:" + dir.getAbsolutePath(), dir.mkdirs() );
}

FileWriter writer = null;
Writer writer = null;
try
{
writer = new FileWriter( new File( dir, "test$1.txt" ) );
writer = Files.newBufferedWriter( dir.toPath().resolve( "test$1.txt" ) );
IOUtil.copy( "Success", writer );
}
finally
Expand Down Expand Up @@ -568,7 +569,7 @@ private static void createAndCallScript( File dir, String content )
bat = new File( dir, "echo" );
}

Writer w = new FileWriter( bat );
Writer w = Files.newBufferedWriter( bat.toPath() );
try
{
IOUtil.copy( content, w );
Expand Down

0 comments on commit 6ca093b

Please sign in to comment.