Skip to content
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

Using NIO 8 to allow better offloading / performance #111

Merged
merged 3 commits into from Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -22,7 +22,7 @@ limitations under the License.
<parent>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus</artifactId>
<version>6.5</version>
<version>7</version>
</parent>

<artifactId>plexus-utils</artifactId>
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Expand Up @@ -60,8 +60,6 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -70,6 +68,7 @@
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
Expand Down Expand Up @@ -2252,16 +2251,14 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
{
if ( encoding == null || encoding.length() < 1 )
{
fileReader = new BufferedReader( new FileReader( from ) );
fileWriter = new FileWriter( to );
fileReader = Files.newBufferedReader( from.toPath() );
fileWriter = Files.newBufferedWriter( to.toPath() );
}
else
{
InputStream instream = Files.newInputStream( from.toPath() );

OutputStream outstream = Files.newOutputStream( to.toPath() );

fileReader = new BufferedReader( new InputStreamReader( instream, encoding ) );
fileReader = Files.newBufferedReader( from.toPath(), Charset.forName( encoding ) );

fileWriter = new OutputStreamWriter( outstream, encoding );
}
Expand Down Expand Up @@ -2311,7 +2308,7 @@ public static List<String> loadFile( File file )

if ( file.exists() )
{
try ( BufferedReader reader = new BufferedReader( new FileReader( file ) ) )
try ( BufferedReader reader = Files.newBufferedReader( file.toPath() ) )
{
for ( String line = reader.readLine(); line != null; line = reader.readLine() )
{
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/codehaus/plexus/util/ReaderFactory.java
Expand Up @@ -18,7 +18,6 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -154,13 +153,13 @@ public static Reader newPlatformReader( InputStream in )
*
* @param file not null file.
* @return a reader instance for the input file using the default platform charset.
* @throws FileNotFoundException if any.
* @throws IOException if any.
* @see Charset#defaultCharset()
*/
public static Reader newPlatformReader( File file )
throws FileNotFoundException
throws IOException
{
return new FileReader( file );
return Files.newBufferedReader( file.toPath() );
}

/**
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
22 changes: 10 additions & 12 deletions src/test/java/org/codehaus/plexus/util/IOUtilTest.java
Expand Up @@ -22,8 +22,6 @@

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -179,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 @@ -210,7 +208,7 @@ public void testReaderToOutputStream()
throws Exception
{
File destination = newFile( "copy3.txt" );
FileReader fin = new FileReader( testFile );
Reader fin = Files.newBufferedReader( testFile.toPath() );
OutputStream fout = Files.newOutputStream( destination.toPath() );
IOUtil.copy( fin, fout );
// Note: this method *does* flush. It is equivalent to:
Expand All @@ -232,8 +230,8 @@ public void testReaderToWriter()
throws Exception
{
File destination = newFile( "copy4.txt" );
FileReader fin = new FileReader( testFile );
FileWriter fout = new FileWriter( destination );
Reader fin = Files.newBufferedReader( testFile.toPath() );
Writer fout = Files.newBufferedWriter( destination.toPath() );
IOUtil.copy( fin, fout );

fout.flush();
Expand All @@ -248,7 +246,7 @@ public void testReaderToWriter()
public void testReaderToString()
throws Exception
{
FileReader fin = new FileReader( testFile );
Reader fin = Files.newBufferedReader( testFile.toPath() );
String out = IOUtil.toString( fin );
assertNotNull( out );
assertTrue( "Wrong output size: out.length()=" + out.length() + "!=" + FILE_SIZE, out.length() == FILE_SIZE );
Expand All @@ -260,7 +258,7 @@ public void testStringToOutputStream()
throws Exception
{
File destination = newFile( "copy5.txt" );
FileReader fin = new FileReader( testFile );
Reader fin = Files.newBufferedReader( testFile.toPath() );
// Create our String. Rely on testReaderToString() to make sure this is valid.
String str = IOUtil.toString( fin );
OutputStream fout = Files.newOutputStream( destination.toPath() );
Expand All @@ -284,10 +282,10 @@ public void testStringToWriter()
throws Exception
{
File destination = newFile( "copy6.txt" );
FileReader fin = new FileReader( testFile );
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 @@ -316,7 +314,7 @@ public void testInputStreamToByteArray()
public void testStringToByteArray()
throws Exception
{
FileReader fin = new FileReader( testFile );
Reader fin = Files.newBufferedReader( testFile.toPath() );

// Create our String. Rely on testReaderToString() to make sure this is valid.
String str = IOUtil.toString( fin );
Expand All @@ -331,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