use of org.exist.util.io.VirtualTempPath in project exist by eXist-db.
the class RemoteResourceSet method getMembersAsResource.
@Override
public Resource getMembersAsResource() throws XMLDBException {
final List<Object> params = new ArrayList<>();
params.add(handle);
params.add(outputProperties);
VirtualTempPath tempFile = new VirtualTempPath(getInMemorySize(outputProperties), TemporaryFileManager.getInstance());
try (final OutputStream os = tempFile.newOutputStream()) {
Map<?, ?> table = (Map<?, ?>) collection.execute("retrieveAllFirstChunk", params);
long offset = (Integer) table.get("offset");
byte[] data = (byte[]) table.get("data");
final boolean isCompressed = "yes".equals(outputProperties.getProperty(EXistOutputKeys.COMPRESS_OUTPUT, "no"));
// One for the local cached file
Inflater dec = null;
byte[] decResult = null;
int decLength = 0;
if (isCompressed) {
dec = new Inflater();
decResult = new byte[65536];
dec.setInput(data);
do {
decLength = dec.inflate(decResult);
os.write(decResult, 0, decLength);
} while (decLength == decResult.length || !dec.needsInput());
} else {
os.write(data);
}
while (offset > 0) {
params.clear();
params.add(table.get("handle"));
params.add(Long.toString(offset));
table = (Map<?, ?>) collection.execute("getNextExtendedChunk", params);
offset = Long.parseLong((String) table.get("offset"));
data = (byte[]) table.get("data");
// One for the local cached file
if (isCompressed) {
dec.setInput(data);
do {
decLength = dec.inflate(decResult);
os.write(decResult, 0, decLength);
} while (decLength == decResult.length || !dec.needsInput());
} else {
os.write(data);
}
}
if (dec != null) {
dec.end();
}
final RemoteXMLResource res = new RemoteXMLResource(collection, handle, 0, XmldbURI.EMPTY_URI, Optional.empty());
res.setContent(tempFile);
res.setProperties(outputProperties);
return res;
} catch (final XMLDBException xre) {
final byte[] data = (byte[]) collection.execute("retrieveAll", params);
String content;
try {
content = new String(data, outputProperties.getProperty(OutputKeys.ENCODING, "UTF-8"));
} catch (final UnsupportedEncodingException ue) {
LOG.warn(ue);
content = new String(data);
}
final RemoteXMLResource res = new RemoteXMLResource(collection, handle, 0, XmldbURI.EMPTY_URI, Optional.empty());
res.setContent(content);
res.setProperties(outputProperties);
return res;
} catch (final IOException | DataFormatException ioe) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, ioe.getMessage(), ioe);
}
}
use of org.exist.util.io.VirtualTempPath in project exist by eXist-db.
the class RemoteXMLResource method setContentAsDOM.
@Override
public void setContentAsDOM(final Node root) throws XMLDBException {
Properties properties = getProperties();
try {
VirtualTempPath tempFile = new VirtualTempPath(getInMemorySize(properties), TemporaryFileManager.getInstance());
try (OutputStream out = tempFile.newOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(out, UTF_8)) {
final DOMSerializer xmlout = new DOMSerializer(osw, properties);
final short type = root.getNodeType();
if (type == Node.ELEMENT_NODE || type == Node.DOCUMENT_FRAGMENT_NODE || type == Node.DOCUMENT_NODE) {
xmlout.serialize(root);
} else {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "invalid node type");
}
}
setContent(tempFile);
} catch (final TransformerException | IOException ioe) {
freeResources();
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, ioe.getMessage(), ioe);
}
}
use of org.exist.util.io.VirtualTempPath 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);
}
}
Aggregations