-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a caching writer to avoid overwriting identical files (#116)
- Loading branch information
Showing
4 changed files
with
70 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
modello-core/src/main/java/org/codehaus/modello/plugin/CachingWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.codehaus.modello.plugin; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
import org.sonatype.plexus.build.incremental.BuildContext; | ||
|
||
public class CachingWriter extends StringWriter | ||
{ | ||
private final BuildContext buildContext; | ||
private final Path path; | ||
private final Charset charset; | ||
|
||
public CachingWriter( BuildContext buildContext, Path path, Charset charset ) | ||
{ | ||
this.buildContext = buildContext; | ||
this.path = Objects.requireNonNull( path ); | ||
this.charset = Objects.requireNonNull( charset ); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException | ||
{ | ||
String str = getBuffer().toString(); | ||
if ( Files.exists( path ) ) | ||
{ | ||
String old = readString( path, charset ); | ||
if ( str.equals( old ) ) | ||
{ | ||
return; | ||
} | ||
} | ||
writeString( path, str, charset ); | ||
if ( buildContext != null ) | ||
{ | ||
buildContext.refresh( path.toFile() ); | ||
} | ||
} | ||
|
||
private static String readString( Path path, Charset charset ) throws IOException | ||
{ | ||
byte[] ba = Files.readAllBytes( path ); | ||
return new String( ba, charset ); | ||
} | ||
|
||
private static void writeString( Path path, String str, Charset charset ) throws IOException | ||
{ | ||
byte[] ba = str.getBytes( charset ); | ||
Files.write( path, ba ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters