use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.
the class FileManager method put.
/**
* Converts the supplied source path and dest URI to a File and URI object,
* respectively, and uploads the remote file according to the supplied
* options.
*
* @param sourceName
* source path
* @param destName
* remote URI to upload to (as a string)
* @param options
* set of options, either FORCE_REMOTE or CHECK_HASH
* @throws URIxSyntaxException if the dest is malformed
* @throws FileTransferException if upload fails.
* @see #put(File, URIx, int)
*/
public void put(String sourceName, String destName, int options) throws URIxSyntaxException, FileTransferException {
// try to make URI from sourceName
URIx dst = null;
if (destName != null) {
dst = new URIx(destName);
}
File src = new File(sourceName);
put(src, dst, options);
}
use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.
the class FileManager method mergeExtension.
// adds an extension onto a uri
private static URIx mergeExtension(URIx base, String extension) {
URIx merged = new URIx(base);
if (merged.isZip()) {
String fn = base.getFragment() + extension;
merged.setFragment(fn);
} else {
String fn = base.getPath(false) + extension;
merged.setPath(fn);
}
return merged;
}
use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.
the class FileManager method getInputStream.
/**
* Returns an input stream for the file located at either {@code localCopy}
* or {@code source} (according to options). Works with absolute paths and
* source URIs, otherwise combines path and source URI with the default
* download directory and remote source uri, respectively. If the
* destination is null or a directory, then the filename of source is
* appended.
*
* If there is any internal problem, (such as failing to obtain a hash, or
* failing to download a file), the function will log the error message and
* continue.
*
* @param localCopy
* the local path (relative or absolute) to check for overriding file
* @param source
* the remote URI to read from
* @param options
* set of options, from {@code FORCE_REMOTE, DOWNLOAD_ZIP, CHECK_HASH}
* @return File input stream
* @throws FileTransferException if we cannot open the stream
*/
public InputStream getInputStream(File localCopy, URIx source, int options) {
// default local copy if none provided
if (localCopy == null) {
if (source.isRelative()) {
localCopy = new File(source.getPath(false));
} else {
localCopy = new File(extractFileName(source));
}
} else if (localCopy.isDirectory()) {
if (source.isRelative()) {
localCopy = new File(localCopy, source.getRawPath());
} else {
localCopy = new File(localCopy, extractFileName(source));
}
}
// convert to absolute
localCopy = getAbsoluteFile(localCopy);
source = getAbsoluteURI(source);
// download zip file first if requested
if (source.isZip() && (options & DOWNLOAD_ZIP) != 0) {
// get zip file
URIx zipSource = source.getBaseURI();
File zipDest = getAbsoluteFile(extractFileName(zipSource));
File zipFile = get(zipDest, zipSource, options);
// replace source URI
source.setBaseURI(new URIx(zipFile));
// XXX no longer need to check hash, since zip's hash would
// have changed, although we do need to replace if re-downloaded zip
// options = options & (~CHECK_HASH);
}
// check if we need to open remote stream
boolean fetch = true;
if ((options & FORCE_REMOTE) == 0) {
// check if file exists
if (localCopy.canRead()) {
// check hash if options say so
if ((options & CHECK_HASH) != 0) {
try {
fetch = !equalsHash(localCopy, source);
if (fetch) {
logger.debug("Hash matches");
}
} catch (FileTransferException e) {
logger.debug("Cannot obtain hash, assuming it doesn't match, " + e.getMessage());
exceptionStack.add(e);
fetch = true;
}
} else {
// file exists, so let it be
fetch = false;
}
}
}
// open remote stream
InputStream in = null;
if (fetch) {
try {
cacher.initialize();
logger.debug("Opening stream from " + source.toString());
// open remote stream
in = cacher.getInputStream(source);
} catch (FileSystemException e) {
cacher.release();
String msg = "Failed to open remote file <" + source + ">";
throw new FileTransferException(msg, e);
}
} else {
logger.debug("Local file '" + localCopy + "' exists.");
try {
in = new FileInputStream(localCopy);
} catch (IOException e) {
String msg = "Failed to open local file <" + localCopy + ">";
throw new FileTransferException(msg, e);
}
}
lastFile = null;
lastWasRemote = fetch;
return in;
}
use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.
the class FileManager method getRemote.
/**
* Retrieves a remote file if it exists, null otherwise. If dest is null, or
* a directory, appends source filename
*
* @param destName
* the destination file (local)
* @param sourceName
* the source URI
* @return a File reference to the new local copy
* @throws URIxSyntaxException if the source URI is malformed
* @throws FileTransferException if grabbing the remote file fails
*/
public File getRemote(String destName, String sourceName) throws FileTransferException, URIxSyntaxException {
URIx remote = new URIx(sourceName);
File localFile = null;
if (destName != null) {
localFile = new File(destName);
}
return getRemote(localFile, remote);
}
use of maspack.fileutil.uri.URIx in project artisynth_core by artisynth.
the class FileManager method getInputStream.
public InputStream getInputStream(String localCopy, String sourceName) throws FileTransferException {
URIx source = new URIx(sourceName);
File local = null;
if (localCopy != null) {
local = new File(localCopy);
}
return getInputStream(local, source, myOptions);
}
Aggregations