Skip to content

Commit ad1a668

Browse files
mkargmichael-o
authored andcommitted
Java 7: Files.newInputStream instead of new FilesInputStream
Provides potentially better performance. Signed-off-by: Markus KARG <[email protected]>
1 parent 3dc33d1 commit ad1a668

File tree

9 files changed

+29
-36
lines changed

9 files changed

+29
-36
lines changed

src/main/java/org/codehaus/plexus/util/Expand.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
*/
5656

5757
import java.io.File;
58-
import java.io.FileInputStream;
5958
import java.io.FileNotFoundException;
6059
import java.io.IOException;
6160
import java.io.InputStream;
@@ -98,7 +97,7 @@ protected void expandFile( final File srcF, final File dir )
9897
throws Exception
9998
{
10099
// code from WarExpand
101-
try ( ZipInputStream zis = new ZipInputStream( new FileInputStream( srcF ) ) )
100+
try ( ZipInputStream zis = new ZipInputStream( Files.newInputStream( srcF.toPath() ) ) )
102101
{
103102
for ( ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry() )
104103
{

src/main/java/org/codehaus/plexus/util/FileUtils.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060

6161
import java.io.BufferedReader;
6262
import java.io.File;
63-
import java.io.FileInputStream;
6463
import java.io.FileReader;
6564
import java.io.FileWriter;
6665
import java.io.IOException;
@@ -376,11 +375,11 @@ private static InputStreamReader getInputStreamReader( File file, String encodin
376375
{
377376
if ( encoding != null )
378377
{
379-
return new InputStreamReader( new FileInputStream( file ), encoding );
378+
return new InputStreamReader( Files.newInputStream( file.toPath() ), encoding );
380379
}
381380
else
382381
{
383-
return new InputStreamReader( new FileInputStream( file ) );
382+
return new InputStreamReader( Files.newInputStream( file.toPath() ) );
384383
}
385384
}
386385

@@ -723,8 +722,8 @@ public static boolean contentEquals( final File file1, final File file2 )
723722
return false;
724723
}
725724

726-
try ( InputStream input1 = new FileInputStream( file1 );
727-
InputStream input2 = new FileInputStream( file2 ) )
725+
try ( InputStream input1 = Files.newInputStream( file1.toPath() );
726+
InputStream input2 = Files.newInputStream( file2.toPath() ) )
728727
{
729728
return IOUtil.contentEquals( input1, input2 );
730729
}
@@ -2258,7 +2257,7 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
22582257
}
22592258
else
22602259
{
2261-
FileInputStream instream = new FileInputStream( from );
2260+
InputStream instream = Files.newInputStream( from.toPath() );
22622261

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

src/main/java/org/codehaus/plexus/util/PropertyUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
import java.util.Properties;
2222
import java.io.File;
23-
import java.io.FileInputStream;
2423
import java.io.InputStream;
2524
import java.io.IOException;
2625
import java.net.URL;
26+
import java.nio.file.Files;
2727

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

4949
public static Properties loadProperties( final InputStream is )

src/main/java/org/codehaus/plexus/util/ReaderFactory.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818

1919
import java.io.File;
20-
import java.io.FileInputStream;
2120
import java.io.FileNotFoundException;
2221
import java.io.FileReader;
2322
import java.io.IOException;
@@ -27,6 +26,7 @@
2726
import java.io.UnsupportedEncodingException;
2827
import java.net.URL;
2928
import java.nio.charset.Charset;
29+
import java.nio.file.Files;
3030

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

@@ -199,14 +199,13 @@ public static Reader newReader( InputStream in, String encoding )
199199
* @param file not null file.
200200
* @param encoding not null supported encoding.
201201
* @return a reader instance for the input file using the given encoding.
202-
* @throws FileNotFoundException if any.
203-
* @throws UnsupportedEncodingException if any.
202+
* @throws IOException if any.
204203
* @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported encodings</a>
205204
*/
206205
public static Reader newReader( File file, String encoding )
207-
throws FileNotFoundException, UnsupportedEncodingException
206+
throws IOException
208207
{
209-
return new InputStreamReader( new FileInputStream( file ), encoding );
208+
return new InputStreamReader( Files.newInputStream( file.toPath() ), encoding );
210209
}
211210

212211
/**

src/main/java/org/codehaus/plexus/util/xml/XmlReader.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
import java.io.BufferedInputStream;
2020
import java.io.BufferedReader;
2121
import java.io.File;
22-
import java.io.FileInputStream;
2322
import java.io.IOException;
2423
import java.io.InputStream;
2524
import java.io.InputStreamReader;
2625
import java.io.Reader;
2726
import java.io.StringReader;
2827
import java.net.URL;
2928
import java.net.URLConnection;
29+
import java.nio.file.Files;
3030
import java.net.HttpURLConnection;
3131
import java.util.Locale;
3232
import java.util.regex.Pattern;
@@ -126,7 +126,7 @@ public static String getDefaultEncoding()
126126
public XmlReader( File file )
127127
throws IOException
128128
{
129-
this( new FileInputStream( file ) );
129+
this( Files.newInputStream( file.toPath() ) );
130130
}
131131

132132
/**

src/test/java/org/codehaus/plexus/util/FileBasedTestCase.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.BufferedOutputStream;
2222
import java.io.ByteArrayOutputStream;
2323
import java.io.File;
24-
import java.io.FileInputStream;
2524
import java.io.IOException;
2625
import java.io.InputStream;
2726
import java.io.OutputStream;
@@ -192,10 +191,10 @@ private void assertEqualContent( final File f0, final File f1 )
192191
* + " and " + f1 + " have differing file sizes (" + f0.length() + " vs " + f1.length() + ")", ( f0.length() ==
193192
* f1.length() ) );
194193
*/
195-
final InputStream is0 = new FileInputStream( f0 );
194+
final InputStream is0 = Files.newInputStream( f0.toPath() );
196195
try
197196
{
198-
final InputStream is1 = new FileInputStream( f1 );
197+
final InputStream is1 = Files.newInputStream( f1.toPath() );
199198
try
200199
{
201200
final byte[] buf0 = new byte[1024];
@@ -229,7 +228,7 @@ private void assertEqualContent( final File f0, final File f1 )
229228
protected void assertEqualContent( final byte[] b0, final File file )
230229
throws IOException
231230
{
232-
final InputStream is = new FileInputStream( file );
231+
final InputStream is = Files.newInputStream( file.toPath() );
233232
try
234233
{
235234
byte[] b1 = new byte[b0.length];

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import java.io.ByteArrayInputStream;
2727
import java.io.File;
28-
import java.io.FileInputStream;
2928
import java.io.IOException;
3029
import java.io.InputStream;
3130
import java.io.OutputStream;
@@ -268,7 +267,7 @@ public void testCopyURLToFile()
268267
FileUtils.copyURLToFile( getClass().getResource( resourceName ), file );
269268

270269
// Tests that resource was copied correctly
271-
final FileInputStream fis = new FileInputStream( file );
270+
final InputStream fis = Files.newInputStream( file.toPath() );
272271
try
273272
{
274273
assertTrue( "Content is not equal.",

src/test/java/org/codehaus/plexus/util/IOUtilTest.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import java.io.BufferedOutputStream;
2424
import java.io.File;
25-
import java.io.FileInputStream;
2625
import java.io.FileReader;
2726
import java.io.FileWriter;
2827
import java.io.IOException;
@@ -115,8 +114,8 @@ private void assertEqualContent( byte[] b0, byte[] b1 )
115114
private void assertEqualContent( File f0, File f1 )
116115
throws IOException
117116
{
118-
FileInputStream is0 = new FileInputStream( f0 );
119-
FileInputStream is1 = new FileInputStream( f1 );
117+
InputStream is0 = Files.newInputStream( f0.toPath() );
118+
InputStream is1 = Files.newInputStream( f1.toPath() );
120119
byte[] buf0 = new byte[FILE_SIZE];
121120
byte[] buf1 = new byte[FILE_SIZE];
122121
int n0 = 0;
@@ -145,7 +144,7 @@ private void assertEqualContent( File f0, File f1 )
145144
private void assertEqualContent( byte[] b0, File file )
146145
throws IOException
147146
{
148-
FileInputStream is = new FileInputStream( file );
147+
InputStream is = Files.newInputStream( file.toPath() );
149148
byte[] b1 = new byte[b0.length];
150149
int numRead = is.read( b1 );
151150
assertTrue( "Different number of bytes", numRead == b0.length && is.available() == 0 );
@@ -160,7 +159,7 @@ public void testInputStreamToOutputStream()
160159
throws Exception
161160
{
162161
File destination = newFile( "copy1.txt" );
163-
FileInputStream fin = new FileInputStream( testFile );
162+
InputStream fin = Files.newInputStream( testFile.toPath() );
164163
OutputStream fout = Files.newOutputStream( destination.toPath() );
165164

166165
IOUtil.copy( fin, fout );
@@ -179,7 +178,7 @@ public void testInputStreamToWriter()
179178
throws Exception
180179
{
181180
File destination = newFile( "copy2.txt" );
182-
FileInputStream fin = new FileInputStream( testFile );
181+
InputStream fin = Files.newInputStream( testFile.toPath() );
183182
FileWriter fout = new FileWriter( destination );
184183

185184
IOUtil.copy( fin, fout );
@@ -198,7 +197,7 @@ public void testInputStreamToWriter()
198197
public void testInputStreamToString()
199198
throws Exception
200199
{
201-
FileInputStream fin = new FileInputStream( testFile );
200+
InputStream fin = Files.newInputStream( testFile.toPath() );
202201
String out = IOUtil.toString( fin );
203202
assertNotNull( out );
204203
assertTrue( "Not all bytes were read", fin.available() == 0 );
@@ -304,7 +303,7 @@ public void testStringToWriter()
304303
public void testInputStreamToByteArray()
305304
throws Exception
306305
{
307-
FileInputStream fin = new FileInputStream( testFile );
306+
InputStream fin = Files.newInputStream( testFile.toPath() );
308307
byte[] out = IOUtil.toByteArray( fin );
309308
assertNotNull( out );
310309
assertTrue( "Not all bytes were read", fin.available() == 0 );
@@ -333,7 +332,7 @@ public void testByteArrayToWriter()
333332
{
334333
File destination = newFile( "copy7.txt" );
335334
FileWriter fout = new FileWriter( destination );
336-
FileInputStream fin = new FileInputStream( testFile );
335+
InputStream fin = Files.newInputStream( testFile.toPath() );
337336

338337
// Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
339338
byte[] in = IOUtil.toByteArray( fin );
@@ -350,7 +349,7 @@ public void testByteArrayToWriter()
350349
public void testByteArrayToString()
351350
throws Exception
352351
{
353-
FileInputStream fin = new FileInputStream( testFile );
352+
InputStream fin = Files.newInputStream( testFile.toPath() );
354353
byte[] in = IOUtil.toByteArray( fin );
355354
// Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
356355
String str = IOUtil.toString( in );
@@ -364,7 +363,7 @@ public void testByteArrayToOutputStream()
364363
{
365364
File destination = newFile( "copy8.txt" );
366365
OutputStream fout = Files.newOutputStream( destination.toPath() );
367-
FileInputStream fin = new FileInputStream( testFile );
366+
InputStream fin = Files.newInputStream( testFile.toPath() );
368367

369368
// Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
370369
byte[] in = IOUtil.toByteArray( fin );

src/test/java/org/codehaus/plexus/util/xml/XmlUtilTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.junit.Assert.assertTrue;
2121

2222
import java.io.File;
23-
import java.io.FileInputStream;
2423
import java.io.IOException;
2524
import java.io.InputStream;
2625
import java.io.OutputStream;
@@ -77,7 +76,7 @@ public void testPrettyFormatInputStreamOutputStream()
7776
OutputStream os = null;
7877
try
7978
{
80-
is = new FileInputStream( testDocument );
79+
is = Files.newInputStream( testDocument.toPath() );
8180
os = Files.newOutputStream( getTestOutputFile( "target/test/prettyFormatTestDocumentOutputStream.xml" ).toPath() );
8281

8382
assertNotNull( is );

0 commit comments

Comments
 (0)