From b8696fb4b0ee29d866c5e09c08b6617dbc1fbfc2 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Wed, 11 Jan 2023 17:30:34 +0000 Subject: [PATCH] Using Files.readAllBytes instead of custom loop --- .../org/codehaus/plexus/util/FileUtils.java | 31 +++++-------------- 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/util/FileUtils.java b/src/main/java/org/codehaus/plexus/util/FileUtils.java index 4dca8b3c..95b939e0 100644 --- a/src/main/java/org/codehaus/plexus/util/FileUtils.java +++ b/src/main/java/org/codehaus/plexus/util/FileUtils.java @@ -62,7 +62,6 @@ 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; @@ -70,6 +69,7 @@ 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; @@ -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