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

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

Introduction

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

Prototype

public boolean isThresholdExceeded() 

Source Link

Document

Determines whether or not the configured threshold has been exceeded for this output stream.

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.//from  w ww.  j  a  va  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);
    }
}