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

fileAppend makes use of optimized fileWrite implementations for JRE 8 and JRE 11 #235

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions src/main/java/org/codehaus/plexus/util/BaseFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

/**
* Implementation specific to Java SE 8 version.
Expand All @@ -15,9 +17,9 @@ static String fileRead( Path path, String encoding ) throws IOException
return encoding != null ? new String( bytes, encoding ) : new String( bytes );
}

static void fileWrite( Path path, String encoding, String data ) throws IOException
static void fileWrite( Path path, String encoding, String data, OpenOption... openOptions ) throws IOException
{
byte[] bytes = encoding != null ? data.getBytes( encoding ) : data.getBytes();
Files.write( path, bytes );
Files.write( path, bytes, openOptions );
}
}
18 changes: 6 additions & 12 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,12 @@ public static void fileAppend( String fileName, String data )
public static void fileAppend( String fileName, String encoding, String data )
throws IOException
{
try ( OutputStream out = Files.newOutputStream( Paths.get(fileName),
StandardOpenOption.APPEND, StandardOpenOption.CREATE ) )
{
if ( encoding != null )
{
out.write( data.getBytes( encoding ) );
}
else
{
out.write( data.getBytes() );
}
}
fileAppend( Paths.get( fileName), encoding, data );
}

private static void fileAppend( Path path, String encoding, String data ) throws IOException
{
fileWrite( path, encoding, data, StandardOpenOption.APPEND, StandardOpenOption.CREATE );
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/main/java11/org/codehaus/plexus/util/BaseFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

/**
* Implementation specific to Java SE 11 version.
Expand All @@ -15,15 +17,15 @@ static String fileRead( Path path, String encoding ) throws IOException
return encoding != null ? Files.readString( path, Charset.forName( encoding ) ) : Files.readString( path );
}

static void fileWrite( Path path, String encoding, String data ) throws IOException
static void fileWrite( Path path, String encoding, String data, OpenOption... openOptions ) throws IOException
{
if ( encoding != null )
{
Files.writeString( path, data, Charset.forName( encoding ) );
Files.writeString( path, data, Charset.forName( encoding ), openOptions );
}
else
{
Files.writeString( path, data );
Files.writeString( path, data, openOptions );
}
}
}