Skip to content

Various bug fixes for classes 'FileUtils' and 'IOUtil'. #9

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

Merged
merged 1 commit into from
May 4, 2016
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
37 changes: 32 additions & 5 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,12 @@ public static String fileRead( File file, String encoding )
}
int count;
char[] b = new char[512];
while ( ( count = reader.read( b ) ) > 0 ) // blocking read
while ( ( count = reader.read( b ) ) >= 0 ) // blocking read
{
buf.append( b, 0, count );
}
reader.close();
reader = null;
}
finally
{
Expand Down Expand Up @@ -439,6 +441,8 @@ public static void fileAppend( String fileName, String encoding, String data )
{
out.write( data.getBytes() );
}
out.close();
out = null;
}
finally
{
Expand Down Expand Up @@ -515,6 +519,8 @@ public static void fileWrite( File file, String encoding, String data )
writer = new OutputStreamWriter( out );
}
writer.write( data );
writer.close();
writer = null;
}
finally
{
Expand Down Expand Up @@ -761,18 +767,23 @@ public static boolean contentEquals( final File file1, final File file2 )

InputStream input1 = null;
InputStream input2 = null;
boolean equals = false;
try
{
input1 = new FileInputStream( file1 );
input2 = new FileInputStream( file2 );
return IOUtil.contentEquals( input1, input2 );

equals = IOUtil.contentEquals( input1, input2 );
input1.close();
input1 = null;
input2.close();
input2 = null;
}
finally
{
IOUtil.close( input1 );
IOUtil.close( input2 );
}
return equals;
}

/**
Expand Down Expand Up @@ -813,11 +824,11 @@ public static File toFile( final URL url )
public static URL[] toURLs( final File[] files )
throws IOException
{
final URL[] urls = new URL[files.length];
final URL[] urls = new URL[ files.length ];

for ( int i = 0; i < urls.length; i++ )
{
urls[i] = files[i].toURL();
urls[i] = files[i].toURI().toURL();
}

return urls;
Expand Down Expand Up @@ -1112,6 +1123,14 @@ private static void doCopyFile( File source, File destination )
count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;
pos += output.transferFrom( input, pos, count );
}
output.close();
output = null;
fos.close();
fos = null;
input.close();
input = null;
fis.close();
fis = null;
}
finally
{
Expand Down Expand Up @@ -1197,6 +1216,10 @@ public static void copyStreamToFile( final InputStreamFacade source, final File
input = source.getInputStream();
output = new FileOutputStream( destination );
IOUtil.copy( input, output );
output.close();
output = null;
input.close();
input = null;
}
finally
{
Expand Down Expand Up @@ -2332,6 +2355,10 @@ public static void copyFile( File from, File to, String encoding, FilterWrapper[
}

IOUtil.copy( reader, fileWriter );
fileWriter.close();
fileWriter = null;
fileReader.close();
fileReader = null;
}
finally
{
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/codehaus/plexus/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static void copy( final InputStream input,
{
final byte[] buffer = new byte[bufferSize];
int n = 0;
while ( -1 != ( n = input.read( buffer ) ) )
while ( 0 <= ( n = input.read( buffer ) ) )
{
output.write( buffer, 0, n );
}
Expand All @@ -210,7 +210,7 @@ public static void copy( final Reader input, final Writer output, final int buff
{
final char[] buffer = new char[bufferSize];
int n = 0;
while ( -1 != ( n = input.read( buffer ) ) )
while ( 0 <= ( n = input.read( buffer ) ) )
{
output.write( buffer, 0, n );
}
Expand Down Expand Up @@ -699,7 +699,7 @@ public static boolean contentEquals( final InputStream input1,
final InputStream bufferedInput2 = new BufferedInputStream( input2 );

int ch = bufferedInput1.read();
while ( -1 != ch )
while ( 0 <= ch )
{
final int ch2 = bufferedInput2.read();
if ( ch != ch2 )
Expand All @@ -710,7 +710,7 @@ public static boolean contentEquals( final InputStream input1,
}

final int ch2 = bufferedInput2.read();
if ( -1 != ch2 )
if ( 0 <= ch2 )
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/codehaus/plexus/util/IOUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void assertEqualContent( File f0, File f1 )

try
{
while ( -1 != n0 )
while ( 0 <= n0 )
{
n0 = is0.read( buf0 );
n1 = is1.read( buf1 );
Expand Down