Skip to content

Commit

Permalink
Java 7: Files.newInputStream instead of new FilesInputStream
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 authored and michael-o committed Dec 14, 2020
1 parent 3dc33d1 commit ad1a668
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 36 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/codehaus/plexus/util/Expand.java
Expand Up @@ -55,7 +55,6 @@
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -98,7 +97,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
11 changes: 5 additions & 6 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.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
Expand Down Expand Up @@ -376,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 );
}
else
{
return new InputStreamReader( new FileInputStream( file ) );
return new InputStreamReader( Files.newInputStream( file.toPath() ) );
}
}

Expand Down Expand Up @@ -723,8 +722,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 @@ -2258,7 +2257,7 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
}
else
{
FileInputStream instream = new FileInputStream( from );
InputStream instream = Files.newInputStream( from.toPath() );

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

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/codehaus/plexus/util/PropertyUtils.java
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
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
4 changes: 2 additions & 2 deletions src/main/java/org/codehaus/plexus/util/xml/XmlReader.java
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
7 changes: 3 additions & 4 deletions src/test/java/org/codehaus/plexus/util/FileBasedTestCase.java
Expand Up @@ -21,7 +21,6 @@
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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
3 changes: 1 addition & 2 deletions src/test/java/org/codehaus/plexus/util/FileUtilsTest.java
Expand Up @@ -25,7 +25,6 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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
21 changes: 10 additions & 11 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.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
Expand Down Expand Up @@ -115,8 +114,8 @@ private void assertEqualContent( byte[] b0, byte[] b1 )
private void assertEqualContent( File f0, File f1 )
throws IOException
{
FileInputStream is0 = new FileInputStream( f0 );
FileInputStream is1 = new FileInputStream( f1 );
InputStream is0 = Files.newInputStream( f0.toPath() );
InputStream is1 = Files.newInputStream( f1.toPath() );
byte[] buf0 = new byte[FILE_SIZE];
byte[] buf1 = new byte[FILE_SIZE];
int n0 = 0;
Expand Down Expand Up @@ -145,7 +144,7 @@ private void assertEqualContent( File f0, File f1 )
private void assertEqualContent( byte[] b0, File file )
throws IOException
{
FileInputStream is = new FileInputStream( file );
InputStream is = Files.newInputStream( file.toPath() );
byte[] b1 = new byte[b0.length];
int numRead = is.read( b1 );
assertTrue( "Different number of bytes", numRead == b0.length && is.available() == 0 );
Expand All @@ -160,7 +159,7 @@ public void testInputStreamToOutputStream()
throws Exception
{
File destination = newFile( "copy1.txt" );
FileInputStream fin = new FileInputStream( testFile );
InputStream fin = Files.newInputStream( testFile.toPath() );
OutputStream fout = Files.newOutputStream( destination.toPath() );

IOUtil.copy( fin, fout );
Expand All @@ -179,7 +178,7 @@ public void testInputStreamToWriter()
throws Exception
{
File destination = newFile( "copy2.txt" );
FileInputStream fin = new FileInputStream( testFile );
InputStream fin = Files.newInputStream( testFile.toPath() );
FileWriter fout = new FileWriter( destination );

IOUtil.copy( fin, fout );
Expand All @@ -198,7 +197,7 @@ public void testInputStreamToWriter()
public void testInputStreamToString()
throws Exception
{
FileInputStream fin = new FileInputStream( testFile );
InputStream fin = Files.newInputStream( testFile.toPath() );
String out = IOUtil.toString( fin );
assertNotNull( out );
assertTrue( "Not all bytes were read", fin.available() == 0 );
Expand Down Expand Up @@ -304,7 +303,7 @@ public void testStringToWriter()
public void testInputStreamToByteArray()
throws Exception
{
FileInputStream fin = new FileInputStream( testFile );
InputStream fin = Files.newInputStream( testFile.toPath() );
byte[] out = IOUtil.toByteArray( fin );
assertNotNull( out );
assertTrue( "Not all bytes were read", fin.available() == 0 );
Expand Down Expand Up @@ -333,7 +332,7 @@ public void testByteArrayToWriter()
{
File destination = newFile( "copy7.txt" );
FileWriter fout = new FileWriter( destination );
FileInputStream fin = new FileInputStream( testFile );
InputStream fin = Files.newInputStream( testFile.toPath() );

// Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
byte[] in = IOUtil.toByteArray( fin );
Expand All @@ -350,7 +349,7 @@ public void testByteArrayToWriter()
public void testByteArrayToString()
throws Exception
{
FileInputStream fin = new FileInputStream( testFile );
InputStream fin = Files.newInputStream( testFile.toPath() );
byte[] in = IOUtil.toByteArray( fin );
// Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
String str = IOUtil.toString( in );
Expand All @@ -364,7 +363,7 @@ public void testByteArrayToOutputStream()
{
File destination = newFile( "copy8.txt" );
OutputStream fout = Files.newOutputStream( destination.toPath() );
FileInputStream fin = new FileInputStream( testFile );
InputStream fin = Files.newInputStream( testFile.toPath() );

// Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
byte[] in = IOUtil.toByteArray( fin );
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/org/codehaus/plexus/util/xml/XmlUtilTest.java
Expand Up @@ -20,7 +20,6 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -77,7 +76,7 @@ public void testPrettyFormatInputStreamOutputStream()
OutputStream os = null;
try
{
is = new FileInputStream( testDocument );
is = Files.newInputStream( testDocument.toPath() );
os = Files.newOutputStream( getTestOutputFile( "target/test/prettyFormatTestDocumentOutputStream.xml" ).toPath() );

assertNotNull( is );
Expand Down

0 comments on commit ad1a668

Please sign in to comment.