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

NIO-7: Potential performance gains using offloading #91

Merged
merged 2 commits into from
Dec 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/org/codehaus/plexus/util/Expand.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -103,7 +103,7 @@ protected void expandFile( final File srcF, final File dir )
throws Exception
{
// code from WarExpand
try ( ZipInputStream zis = new ZipInputStream( new FileInputStream( srcF ) ) )
try ( ZipInputStream zis = new ZipInputStream( Files.newInputStream( srcF.toPath() ) ) )
{
for ( ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry() )
{
Expand Down Expand Up @@ -149,7 +149,7 @@ protected void extractFile( File srcF, File dir, InputStream compressedInputStre
{
byte[] buffer = new byte[65536];

try ( FileOutputStream fos = new FileOutputStream( f ) )
try ( OutputStream fos = Files.newOutputStream( f.toPath() ) )
{
for ( int length = compressedInputStream.read( buffer );
length >= 0;
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -72,6 +70,9 @@
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -374,11 +375,11 @@ private static InputStreamReader getInputStreamReader( File file, String encodin
{
if ( encoding != null )
{
return new InputStreamReader( new FileInputStream( file ), encoding );
return new InputStreamReader( Files.newInputStream( file.toPath() ), encoding );
michael-o marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
return new InputStreamReader( new FileInputStream( file ) );
return new InputStreamReader( Files.newInputStream( file.toPath() ) );
}
}

Expand Down Expand Up @@ -407,7 +408,7 @@ public static void fileAppend( String fileName, String data )
public static void fileAppend( String fileName, String encoding, String data )
throws IOException
{
try ( FileOutputStream out = new FileOutputStream( fileName, true ) )
try ( OutputStream out = Files.newOutputStream( Paths.get(fileName), StandardOpenOption.APPEND ) )
{
if ( encoding != null )
{
Expand Down Expand Up @@ -484,7 +485,7 @@ public static void fileWrite( File file, String encoding, String data )

private static OutputStreamWriter getOutputStreamWriter( File file, String encoding ) throws IOException
{
OutputStream out = new FileOutputStream( file );
OutputStream out = Files.newOutputStream( file.toPath() );
if ( encoding != null )
{
return new OutputStreamWriter( out, encoding );
Expand Down Expand Up @@ -724,8 +725,8 @@ public static boolean contentEquals( final File file1, final File file2 )
return false;
}

try ( InputStream input1 = new FileInputStream( file1 );
InputStream input2 = new FileInputStream( file2 ) )
try ( InputStream input1 = Files.newInputStream( file1.toPath() );
InputStream input2 = Files.newInputStream( file2.toPath() ) )
{
return IOUtil.contentEquals( input1, input2 );
}
Expand Down Expand Up @@ -1123,7 +1124,7 @@ public static void copyStreamToFile( final InputStreamFacade source, final File
checkCanWrite( destination );

try ( InputStream input = source.getInputStream();
FileOutputStream output = new FileOutputStream( destination ) )
OutputStream output = Files.newOutputStream( destination.toPath() ) )
{
IOUtil.copy( input, output );
}
Expand Down Expand Up @@ -2226,9 +2227,9 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
}
else
{
FileInputStream instream = new FileInputStream( from );
InputStream instream = Files.newInputStream( from.toPath() );

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

fileReader = new BufferedReader( new InputStreamReader( instream, encoding ) );

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/codehaus/plexus/util/PropertyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;

/**
* Static methods to create Properties loaded from various sources.
Expand All @@ -43,7 +43,7 @@ public static Properties loadProperties( final URL url )
public static Properties loadProperties( final File file )
throws IOException
{
return loadProperties( new FileInputStream( Objects.requireNonNull( file, "file" ) ) );
return loadProperties( Files.newInputStream( Objects.requireNonNull( file, "file" ).toPath() ) );
}

public static Properties loadProperties( final InputStream is )
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/codehaus/plexus/util/ReaderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -27,6 +26,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;

import org.codehaus.plexus.util.xml.XmlStreamReader;

Expand Down Expand Up @@ -199,14 +199,13 @@ public static Reader newReader( InputStream in, String encoding )
* @param file not null file.
* @param encoding not null supported encoding.
* @return a reader instance for the input file using the given encoding.
* @throws FileNotFoundException if any.
* @throws UnsupportedEncodingException if any.
* @throws IOException if any.
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
*/
public static Reader newReader( File file, String encoding )
throws FileNotFoundException, UnsupportedEncodingException
throws IOException
{
return new InputStreamReader( new FileInputStream( file ), encoding );
return new InputStreamReader( Files.newInputStream( file.toPath() ), encoding );
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/codehaus/plexus/util/WriterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;

import org.codehaus.plexus.util.xml.XmlStreamWriter;

Expand Down Expand Up @@ -169,13 +169,12 @@ public static Writer newWriter( OutputStream out, String encoding )
* @param file not null file.
* @param encoding not null supported encoding.
* @return a writer instance for the output file using the given encoding.
* @throws UnsupportedEncodingException if any.
* @throws FileNotFoundException if any.
* @throws IOException if any.
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
*/
public static Writer newWriter( File file, String encoding )
throws UnsupportedEncodingException, FileNotFoundException
throws IOException
{
return newWriter( new FileOutputStream( file ), encoding );
return newWriter( Files.newOutputStream( file.toPath() ), encoding );
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/codehaus/plexus/util/xml/XmlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.net.HttpURLConnection;
import java.util.Locale;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -126,7 +126,7 @@ public static String getDefaultEncoding()
public XmlReader( File file )
throws IOException
{
this( new FileInputStream( file ) );
this( Files.newInputStream( file.toPath() ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -55,9 +54,9 @@ public XmlStreamWriter( OutputStream out )
}

public XmlStreamWriter( File file )
throws FileNotFoundException
throws IOException
{
this( new FileOutputStream( file ) );
this( Files.newOutputStream( file.toPath() ) );
}

public String getEncoding()
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/org/codehaus/plexus/util/FileBasedTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.util.Arrays;

import junit.framework.AssertionFailedError;
Expand Down Expand Up @@ -61,7 +60,7 @@ protected byte[] createFile( final File file, final long size )

byte[] data = generateTestData( size );

final BufferedOutputStream output = new BufferedOutputStream( new FileOutputStream( file ) );
final BufferedOutputStream output = new BufferedOutputStream( Files.newOutputStream( file.toPath() ) );

try
{
Expand Down Expand Up @@ -192,10 +191,10 @@ private void assertEqualContent( final File f0, final File f1 )
* + " and " + f1 + " have differing file sizes (" + f0.length() + " vs " + f1.length() + ")", ( f0.length() ==
* f1.length() ) );
*/
final InputStream is0 = new FileInputStream( f0 );
final InputStream is0 = Files.newInputStream( f0.toPath() );
try
{
final InputStream is1 = new FileInputStream( f1 );
final InputStream is1 = Files.newInputStream( f1.toPath() );
try
{
final byte[] buf0 = new byte[1024];
Expand Down Expand Up @@ -229,7 +228,7 @@ private void assertEqualContent( final File f0, final File f1 )
protected void assertEqualContent( final byte[] b0, final File file )
throws IOException
{
final InputStream is = new FileInputStream( file );
final InputStream is = Files.newInputStream( file.toPath() );
try
{
byte[] b1 = new byte[b0.length];
Expand Down
15 changes: 7 additions & 8 deletions src/test/java/org/codehaus/plexus/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.nio.file.Files;
import java.util.Properties;

import org.junit.Before;
Expand Down Expand Up @@ -268,7 +267,7 @@ public void testCopyURLToFile()
FileUtils.copyURLToFile( getClass().getResource( resourceName ), file );

// Tests that resource was copied correctly
final FileInputStream fis = new FileInputStream( file );
final InputStream fis = Files.newInputStream( file.toPath() );
try
{
assertTrue( "Content is not equal.",
Expand Down Expand Up @@ -1089,7 +1088,7 @@ public void testFileRead()
Writer writer = null;
try
{
writer = new OutputStreamWriter( new FileOutputStream( testFile ) );
writer = new OutputStreamWriter( Files.newOutputStream( testFile.toPath() ) );
writer.write( testString );
writer.flush();
}
Expand All @@ -1114,7 +1113,7 @@ public void testFileReadWithEncoding()
Writer writer = null;
try
{
writer = new OutputStreamWriter( new FileOutputStream( testFile ), encoding );
writer = new OutputStreamWriter( Files.newOutputStream( testFile.toPath() ), encoding );
writer.write( testString );
writer.flush();
}
Expand All @@ -1137,7 +1136,7 @@ public void testFileAppend()
Writer writer = null;
try
{
writer = new OutputStreamWriter( new FileOutputStream( testFile ) );
writer = new OutputStreamWriter( Files.newOutputStream( testFile.toPath() ) );
writer.write( baseString );
writer.flush();
}
Expand All @@ -1163,7 +1162,7 @@ public void testFileAppendWithEncoding()
Writer writer = null;
try
{
writer = new OutputStreamWriter( new FileOutputStream( testFile ), encoding );
writer = new OutputStreamWriter( Files.newOutputStream( testFile.toPath() ), encoding );
writer.write( baseString );
writer.flush();
}
Expand Down Expand Up @@ -1236,7 +1235,7 @@ public void testDeleteLongPathOnWindows()
File f = new File( a1, path.toString() + "test.txt" );

InputStream is = new ByteArrayInputStream( "Blabla".getBytes( "UTF-8" ) );
OutputStream os = new FileOutputStream( f.getCanonicalFile() );
OutputStream os = Files.newOutputStream( f.getCanonicalFile().toPath() );
IOUtil.copy( is, os );
IOUtil.close( is );
IOUtil.close( os );
Expand Down
Loading