if (destfile.exists() && (!destfile.canWrite() || !destfile.isFile())) {
throw new BuildException("Unable to write to destination file");
}
final ExecutionDataStore dataStore = new ExecutionDataStore();
int numFilesMerged = 0;
final Iterator<?> resourceIterator = files.iterator();
while (resourceIterator.hasNext()) {
final Resource resource = (Resource) resourceIterator.next();
if (resource.isDirectory()) {
continue;
}
log(String.format("Merging %s", resource.getName()),
Project.MSG_DEBUG);
InputStream resourceStream = null;
try {
resourceStream = resource.getInputStream();
final ExecutionDataReader reader = new ExecutionDataReader(
resourceStream);
reader.setExecutionDataVisitor(dataStore);
reader.read();
numFilesMerged++;
} catch (final IOException e) {
throw new BuildException(String.format("Unable to read %s",
resource.getName()), e);
} finally {
FileUtils.close(resourceStream);
}
}
log(String.format("%d files merged", Integer.valueOf(numFilesMerged)),
Project.MSG_INFO);
OutputStream outputStream = null;
try {
destfile.getParentFile().mkdirs();
destfile.createNewFile();
outputStream = new BufferedOutputStream(new FileOutputStream(
destfile));
final ExecutionDataWriter dataWriter = new ExecutionDataWriter(
outputStream);
dataStore.accept(dataWriter);
} catch (final IOException e) {
throw new BuildException(String.format(
"Unable to write merged file %s", destfile.getName()), e);
} finally {
FileUtils.close(outputStream);