use of org.apache.commons.io.output.DeferredFileOutputStream in project jforum2 by rafaelsteil.
the class DiskFileItem method getOutputStream.
/**
* Returns an {@link java.io.OutputStream OutputStream} that can
* be used for storing the contents of the file.
*
* @return An {@link java.io.OutputStream OutputStream} that can be used
* for storing the contensts of the file.
*
* @exception IOException if an error occurs.
*/
public OutputStream getOutputStream() throws IOException {
if (dfos == null) {
File outputFile = getTempFile();
dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
}
return dfos;
}
use of org.apache.commons.io.output.DeferredFileOutputStream in project maven-plugins by apache.
the class ProcessRemoteResourcesMojo method processResourceBundles.
protected void processResourceBundles(RemoteResourcesClassLoader classLoader, VelocityContext context) throws MojoExecutionException {
try {
// CHECKSTYLE_OFF: LineLength
for (Enumeration<URL> e = classLoader.getResources(BundleRemoteResourcesMojo.RESOURCES_MANIFEST); e.hasMoreElements(); ) {
URL url = e.nextElement();
InputStream in = null;
OutputStream out = null;
Reader reader = null;
Writer writer = null;
try {
reader = new InputStreamReader(url.openStream());
RemoteResourcesBundleXpp3Reader bundleReader = new RemoteResourcesBundleXpp3Reader();
RemoteResourcesBundle bundle = bundleReader.read(reader);
reader.close();
reader = null;
for (String bundleResource : bundle.getRemoteResources()) {
String projectResource = bundleResource;
boolean doVelocity = false;
if (projectResource.endsWith(TEMPLATE_SUFFIX)) {
projectResource = projectResource.substring(0, projectResource.length() - 3);
doVelocity = true;
}
// Don't overwrite resource that are already being provided.
File f = new File(outputDirectory, projectResource);
FileUtils.mkdir(f.getParentFile().getAbsolutePath());
if (!copyResourceIfExists(f, projectResource, context)) {
if (doVelocity) {
DeferredFileOutputStream os = new DeferredFileOutputStream(velocityFilterInMemoryThreshold, f);
writer = bundle.getSourceEncoding() == null ? new OutputStreamWriter(os) : new OutputStreamWriter(os, bundle.getSourceEncoding());
if (bundle.getSourceEncoding() == null) {
// TODO: Is this correct? Shouldn't we behave like the rest of maven and fail
// down to JVM default instead ISO-8859-1 ?
velocity.mergeTemplate(bundleResource, "ISO-8859-1", context, writer);
} else {
velocity.mergeTemplate(bundleResource, bundle.getSourceEncoding(), context, writer);
}
writer.close();
writer = null;
fileWriteIfDiffers(os);
} else {
URL resUrl = classLoader.getResource(bundleResource);
if (resUrl != null) {
FileUtils.copyURLToFile(resUrl, f);
}
}
File appendedResourceFile = new File(appendedResourcesDirectory, projectResource);
File appendedVmResourceFile = new File(appendedResourcesDirectory, projectResource + ".vm");
if (appendedResourceFile.exists()) {
in = new FileInputStream(appendedResourceFile);
out = new FileOutputStream(f, true);
IOUtil.copy(in, out);
out.close();
out = null;
in.close();
in = null;
} else if (appendedVmResourceFile.exists()) {
reader = new FileReader(appendedVmResourceFile);
if (bundle.getSourceEncoding() == null) {
writer = new PrintWriter(new FileWriter(f, true));
} else {
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f, true), bundle.getSourceEncoding()));
}
Velocity.init();
Velocity.evaluate(context, writer, "remote-resources", reader);
writer.close();
writer = null;
reader.close();
reader = null;
}
}
}
} finally {
IOUtil.close(out);
IOUtil.close(in);
IOUtil.close(writer);
IOUtil.close(reader);
}
// CHECKSTYLE_ON: LineLength
}
} catch (IOException e) {
throw new MojoExecutionException("Error finding remote resources manifests", e);
} catch (XmlPullParserException e) {
throw new MojoExecutionException("Error parsing remote resource bundle descriptor.", e);
} catch (Exception e) {
throw new MojoExecutionException("Error rendering velocity resource.", e);
}
}
use of org.apache.commons.io.output.DeferredFileOutputStream in project maven-plugins by apache.
the class ProcessRemoteResourcesMojo method fileWriteIfDiffers.
/**
* If the transformation result fits in memory and the destination file already exists
* then both are compared.
* <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 = null;
try {
is = new FileInputStream(file);
final InputStream newContents = new ByteArrayInputStream(outStream.getData());
needOverwrite = !IOUtil.contentEquals(is, newContents);
if (getLog().isDebugEnabled()) {
getLog().debug("File " + file + " contents " + (needOverwrite ? "differs" : "does not differ"));
}
is.close();
is = null;
} 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);
os.close();
os = null;
} finally {
IOUtil.close(os);
}
}
use of org.apache.commons.io.output.DeferredFileOutputStream in project maven-plugins by apache.
the class ProcessRemoteResourcesMojo method copyResourceIfExists.
protected boolean copyResourceIfExists(File file, String relFileName, VelocityContext context) throws IOException, MojoExecutionException {
for (Resource resource : resources) {
File resourceDirectory = new File(resource.getDirectory());
if (!resourceDirectory.exists()) {
continue;
}
// TODO - really should use the resource includes/excludes and name mapping
File source = new File(resourceDirectory, relFileName);
File templateSource = new File(resourceDirectory, relFileName + TEMPLATE_SUFFIX);
if (!source.exists() && templateSource.exists()) {
source = templateSource;
}
if (source.exists() && !source.equals(file)) {
if (source == templateSource) {
Reader reader = null;
Writer writer = null;
DeferredFileOutputStream os = new DeferredFileOutputStream(velocityFilterInMemoryThreshold, file);
try {
if (encoding != null) {
reader = new InputStreamReader(new FileInputStream(source), encoding);
writer = new OutputStreamWriter(os, encoding);
} else {
reader = ReaderFactory.newPlatformReader(source);
writer = WriterFactory.newPlatformWriter(os);
}
velocity.evaluate(context, writer, "", reader);
writer.close();
writer = null;
reader.close();
reader = null;
} catch (ParseErrorException e) {
throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
} catch (MethodInvocationException e) {
throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
} catch (ResourceNotFoundException e) {
throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
} finally {
IOUtil.close(writer);
IOUtil.close(reader);
}
fileWriteIfDiffers(os);
} else if (resource.isFiltering()) {
MavenFileFilterRequest req = setupRequest(resource, source, file);
try {
fileFilter.copyFile(req);
} catch (MavenFilteringException e) {
throw new MojoExecutionException("Error filtering resource: " + source, e);
}
} else {
FileUtils.copyFile(source, file);
}
// exclude the original (so eclipse doesn't complain about duplicate resources)
resource.addExclude(relFileName);
return true;
}
}
return false;
}
Aggregations