Skip to content
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

Reading and writing files using Java NIO #232

Merged
merged 2 commits into from
Jan 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 13 additions & 39 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 Expand Up @@ -481,23 +464,14 @@ public static void fileWrite( File file, String data )
public static void fileWrite( File file, String encoding, String data )
throws IOException
{
try ( Writer writer = getOutputStreamWriter( file, encoding ) )
{
writer.write( data );
}
fileWrite( file.toPath(), encoding, data );
}

private static OutputStreamWriter getOutputStreamWriter( File file, String encoding ) throws IOException

private static void fileWrite( Path path, String encoding, String data )
throws IOException
{
OutputStream out = Files.newOutputStream( file.toPath() );
if ( encoding != null )
{
return new OutputStreamWriter( out, encoding );
}
else
{
return new OutputStreamWriter( out );
}
byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes();
Files.write( path, bytes );
}

/**
Expand Down