br.com.objectos.way.etc.ResourceCopy Maven / Gradle / Ivy
The newest version!
/*
* ResourceProto.java criado em 15/12/2013
*
* Propriedade de Objectos Fábrica de Software LTDA.
* Reprodução parcial ou total proibida.
*/
package br.com.objectos.way.etc;
import java.io.File;
import java.io.IOException;
import java.util.List;
import br.com.objectos.way.core.io.Directory;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
/**
* @author [email protected] (Marcio Endo)
*/
class ResourceCopy {
final ResourceSet set;
final String dir;
final Resource resource;
ResourceCopy(ResourceSet set, Resource resource) {
this.set = set;
this.dir = set.getDir();
this.resource = resource;
}
public static ResourceCopy copyTo(ResourceSet set, Resource resource) {
return new ResourceCopy(set, resource);
}
public static ResourceCopy copyToEval(ResourceSet set, Resource resource) {
return new ResourceCopyEval(set, resource);
}
public static ResourceCopy copyToEvalSelection(ResourceSet set, Resource resource) {
return new ResourceCopyEvalSelection(set, resource);
}
public final List copyTo(Directory targetDir) {
List exs = ImmutableList.of();
try {
String name = resource.getName();
Class> contextClass = set.getContextClass();
ByteSource source = resource.open(contextClass);
File file = resource.targetFile(targetDir);
copy(name, source, file);
} catch (IOException e) {
exs = ImmutableList. of(e);
}
return exs;
}
void copy(String resourceName, ByteSource source, File file) throws IOException {
source.copyTo(Files.asByteSink(file));
}
}