use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class RepoBackup method backup.
public static Path backup(final DBBroker broker) throws IOException {
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
final Path tempFile = temporaryFileManager.getTemporaryFile();
try (final ZipOutputStream os = new ZipOutputStream(Files.newOutputStream(tempFile))) {
final Path directory = ExistRepository.getRepositoryDir(broker.getConfiguration());
zipDir(directory.toAbsolutePath(), os, "");
}
return tempFile;
}
use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class AbstractRemoteResource method getRemoteContentIntoLocalFile.
protected void getRemoteContentIntoLocalFile(final OutputStream os, final boolean isRetrieve, final int handle, final int pos) throws XMLDBException {
final String command;
final List<Object> params = new ArrayList<>();
if (isRetrieve) {
command = "retrieveFirstChunk";
params.add(handle);
params.add(pos);
} else {
command = "getDocumentData";
params.add(path.toString());
}
Properties properties = getProperties();
params.add(properties);
try {
final TemporaryFileManager tempFileManager = TemporaryFileManager.getInstance();
final VirtualTempPath tempFile = new VirtualTempPath(getInMemorySize(properties), tempFileManager);
Map<?, ?> table = (Map<?, ?>) collection.execute(command, params);
final String method;
final boolean useLongOffset;
if (table.containsKey("supports-long-offset") && (Boolean) table.get("supports-long-offset")) {
useLongOffset = true;
method = "getNextExtendedChunk";
} else {
useLongOffset = false;
method = "getNextChunk";
}
long offset = (Integer) table.get("offset");
byte[] data = (byte[]) table.get("data");
final boolean isCompressed = "yes".equals(properties.getProperty(EXistOutputKeys.COMPRESS_OUTPUT, "no"));
try (final OutputStream osTempFile = tempFile.newOutputStream()) {
// One for the local cached file
Inflater dec = null;
byte[] decResult = null;
int decLength;
if (isCompressed) {
dec = new Inflater();
decResult = new byte[65536];
dec.setInput(data);
do {
decLength = dec.inflate(decResult);
osTempFile.write(decResult, 0, decLength);
// And other for the stream where we want to save it!
if (os != null) {
os.write(decResult, 0, decLength);
}
} while (decLength == decResult.length || !dec.needsInput());
} else {
osTempFile.write(data);
// And other for the stream where we want to save it!
if (os != null) {
os.write(data);
}
}
while (offset > 0) {
params.clear();
params.add(table.get("handle"));
params.add(useLongOffset ? Long.toString(offset) : Integer.valueOf((int) offset));
table = (Map<?, ?>) collection.execute(method, params);
offset = useLongOffset ? Long.parseLong((String) table.get("offset")) : ((Integer) table.get("offset"));
data = (byte[]) table.get("data");
// One for the local cached file
if (isCompressed) {
dec.setInput(data);
do {
decLength = dec.inflate(decResult);
osTempFile.write(decResult, 0, decLength);
// And other for the stream where we want to save it!
if (os != null) {
os.write(decResult, 0, decLength);
}
} while (decLength == decResult.length || !dec.needsInput());
} else {
osTempFile.write(data);
// And other for the stream where we want to save it!
if (os != null) {
os.write(data);
}
}
}
if (dec != null) {
dec.end();
}
}
contentFile = tempFile;
} catch (final IOException | DataFormatException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
}
use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class XMLDBStore method loadFromURI.
private Resource loadFromURI(final Collection collection, final URI uri, final String docName, final MimeType mimeType) throws XPathException {
Resource resource;
if ("file".equals(uri.getScheme())) {
final String path = uri.getPath();
if (path == null) {
throw new XPathException(this, "Cannot read from URI: " + uri.toASCIIString());
}
final Path file = Paths.get(path);
if (!Files.isReadable(file)) {
throw new XPathException(this, "Cannot read path: " + path);
}
resource = loadFromFile(collection, file, docName, mimeType);
} else {
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
Path temp = null;
try {
temp = temporaryFileManager.getTemporaryFile();
try (final InputStream is = uri.toURL().openStream()) {
Files.copy(is, temp);
resource = loadFromFile(collection, temp, docName, mimeType);
} finally {
if (temp != null) {
temporaryFileManager.returnTemporaryFile(temp);
}
}
} catch (final MalformedURLException e) {
throw new XPathException(this, "Malformed URL: " + uri.toString(), e);
} catch (final IOException e) {
throw new XPathException(this, "IOException while reading from URL: " + uri.toString(), e);
}
}
return resource;
}
use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.
the class RestXqServiceRegistryPersistence method getRegistryFile.
private Optional<Path> getRegistryFile(final boolean temp) {
try (final DBBroker broker = getBrokerPool().getBroker()) {
final Configuration configuration = broker.getConfiguration();
final Path dataDir = (Path) configuration.getProperty(BrokerPool.PROPERTY_DATA_DIR);
final Path registryFile;
if (temp) {
final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
registryFile = temporaryFileManager.getTemporaryFile();
} else {
registryFile = dataDir.resolve(REGISTRY_FILENAME);
}
return Optional.of(registryFile);
} catch (final EXistException | IOException e) {
log.error(e.getMessage(), e);
return Optional.empty();
}
}
Aggregations