Example usage for org.apache.commons.io.output DeferredFileOutputStream getThreshold

List of usage examples for org.apache.commons.io.output DeferredFileOutputStream getThreshold

Introduction

In this page you can find the example usage for org.apache.commons.io.output DeferredFileOutputStream getThreshold.

Prototype

public int getThreshold() 

Source Link

Document

Returns the threshold, in bytes, at which an event will be triggered.

Usage

From source file:org.apache.maven.plugin.resources.remote.ProcessRemoteResourcesMojo.java

/**
 * If the transformation result fits in memory and the destination file already exists
 * then both are compared.//w  w  w.j ava 2 s .  c  o m
 * <p>If destination file is byte-by-byte equal, then it is not overwritten.
 * This improves subsequent compilation times since upstream plugins property see that
 * the resource was not modified.
 * <p>Note: the method should be called after {@link org.apache.commons.io.output.DeferredFileOutputStream#close}
 *
 * @param outStream Deferred stream
 * @throws IOException
 */
private void fileWriteIfDiffers(DeferredFileOutputStream outStream) throws IOException {
    File file = outStream.getFile();
    if (outStream.isThresholdExceeded()) {
        getLog().info("File " + file + " was overwritten due to content limit threshold "
                + outStream.getThreshold() + " reached");
        return;
    }
    boolean needOverwrite = true;

    if (file.exists()) {
        InputStream is = new FileInputStream(file);
        InputStream newContents = new ByteArrayInputStream(outStream.getData());
        try {
            needOverwrite = !IOUtil.contentEquals(is, newContents);
            if (getLog().isDebugEnabled()) {
                getLog().debug("File " + file + " contents " + (needOverwrite ? "differs" : "does not differ"));
            }
        } finally {
            IOUtil.close(is);
        }
    }

    if (!needOverwrite) {
        getLog().info("File " + file + " is up to date");
        return;
    }
    getLog().info("Writing " + file);
    OutputStream os = new FileOutputStream(file);
    try {
        outStream.writeTo(os);
    } finally {
        IOUtil.close(os);
    }
}