Skip to content

Commit

Permalink
Using Files.readAllBytes instead of custom loop
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarg authored and michael-o committed Jan 14, 2023
1 parent 6288353 commit b8696fb
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
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.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.SecureRandom;
Expand Down Expand Up @@ -355,33 +355,16 @@ public static String fileRead( File file )
public static String fileRead( File file, String encoding )
throws IOException
{
StringBuilder buf = new StringBuilder();

try ( Reader reader = getInputStreamReader( file, encoding ) )
{
int count;
char[] b = new char[512];
while ( ( count = reader.read( b ) ) >= 0 ) // blocking read
{
buf.append( b, 0, count );
}
}

return buf.toString();
return fileRead( file.toPath(), encoding );
}

private static InputStreamReader getInputStreamReader( File file, String encoding ) throws IOException
private static String fileRead( Path path, String encoding )
throws IOException
{
if ( encoding != null )
{
return new InputStreamReader( Files.newInputStream( file.toPath() ), encoding );
}
else
{
return new InputStreamReader( Files.newInputStream( file.toPath() ) );
}
byte[] bytes = Files.readAllBytes( path );
return encoding != null ? new String( bytes, encoding ) : new String( bytes );
}

/**
* Appends data to a file. The file will be created if it does not exist. Note: the data is written with platform
* encoding
Expand Down

0 comments on commit b8696fb

Please sign in to comment.