use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class TestEXistXMLSerialize method serialize2.
@Test
public void serialize2() throws ParserConfigurationException, SAXException, IOException, XMLDBException, URISyntaxException {
Collection testCollection = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/" + TEST_COLLECTION);
Document doc = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(Paths.get(testFile.toURI()).toFile());
XMLResource resource = (XMLResource) testCollection.createResource(null, "XMLResource");
resource.setContentAsDOM(doc);
testCollection.storeResource(resource);
resource = (XMLResource) testCollection.getResource(resource.getId());
assertNotNull(resource);
Node node = resource.getContentAsDOM();
OutputFormat format = new OutputFormat();
format.setLineWidth(0);
format.setIndent(5);
format.setPreserveSpace(true);
try (final UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream()) {
XMLSerializer serializer = new XMLSerializer(out, format);
if (node instanceof Document) {
serializer.serialize((Document) node);
} else if (node instanceof Element) {
serializer.serialize((Element) node);
} else {
fail("Can't serialize node type: " + node);
}
}
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class MetadataFunctions method exifToolExtract.
private Sequence exifToolExtract(final Path binaryFile) throws XPathException {
final ExiftoolModule module = (ExiftoolModule) getParentModule();
try {
final Process p = Runtime.getRuntime().exec(module.getPerlPath() + " " + module.getExiftoolPath() + " -X -struct " + binaryFile.toAbsolutePath().toString());
try (final InputStream stdIn = p.getInputStream();
final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
// buffer stdin
baos.write(stdIn);
// make sure process is complete
p.waitFor();
return ModuleUtils.inputSourceToXML(context, new InputSource(baos.toInputStream()));
}
} catch (final IOException | InterruptedException ex) {
throw new XPathException(this, "Could not execute the Exiftool " + ex.getMessage(), ex);
} catch (final SAXException saxe) {
throw new XPathException(this, "Could not parse output from the Exiftool " + saxe.getMessage(), saxe);
}
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class MetadataFunctions method exifToolWebExtract.
private Sequence exifToolWebExtract(final URI uri) throws XPathException {
final ExiftoolModule module = (ExiftoolModule) getParentModule();
try {
final Process p = Runtime.getRuntime().exec(module.getExiftoolPath() + " -fast -X -");
try (final InputStream stdIn = p.getInputStream();
final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
try (final OutputStream stdOut = p.getOutputStream()) {
final Source src = SourceFactory.getSource(context.getBroker(), null, uri.toString(), false);
if (src == null) {
throw new XPathException(this, "Could not read source for the Exiftool: " + uri.toString());
}
try (final InputStream isSrc = src.getInputStream()) {
// write the remote data to stdOut
int read = -1;
byte[] buf = new byte[4096];
while ((read = isSrc.read(buf)) > -1) {
stdOut.write(buf, 0, read);
}
}
}
// read stdin to buffer
baos.write(stdIn);
// make sure process is complete
p.waitFor();
return ModuleUtils.inputSourceToXML(context, new InputSource(baos.toInputStream()));
}
} catch (final IOException | InterruptedException | PermissionDeniedException ex) {
throw new XPathException(this, "Could not execute the Exiftool " + ex.getMessage(), ex);
} catch (final SAXException saxe) {
throw new XPathException(this, "Could not parse output from the Exiftool " + saxe.getMessage(), saxe);
}
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class ZipFileFunctions method updateZip.
private Sequence updateZip(XmldbURI uri, String[] paths, BinaryValue[] binaries) throws XPathException {
if (paths.length != binaries.length) {
throw new XPathException("Different number of paths (" + paths.length + ") and binaries (" + binaries.length + ")");
}
ZipFileSource zipFileSource = new ZipFileFromDb(uri);
ZipInputStream zis = null;
Map<String, BinaryValue> binariesTable = new HashMap<>(paths.length);
for (int i = 0; i < paths.length; i++) {
binariesTable.put(paths[i], binaries[i]);
}
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
zis = zipFileSource.getStream();
// zos is the output - the result
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry ze;
byte[] buffer = new byte[16384];
int bytes_read;
while ((ze = zis.getNextEntry()) != null) {
String zen = ze.getName();
if (binariesTable.containsKey(zen)) {
// Replace this entry
ZipEntry nze = new ZipEntry(zen);
zos.putNextEntry(nze);
binariesTable.get(zen).streamBinaryTo(zos);
binariesTable.remove(zen);
} else {
// copy this entry to output
if (ze.isDirectory()) {
// can't add empty directory to Zip
ZipEntry dirEntry = new ZipEntry(ze.getName() + System.getProperty("file.separator") + ".");
zos.putNextEntry(dirEntry);
} else {
// copy file across
ZipEntry nze = new ZipEntry(zen);
zos.putNextEntry(nze);
while ((bytes_read = zis.read(buffer)) != -1) zos.write(buffer, 0, bytes_read);
}
}
}
// add any remaining items as NEW entries
for (Map.Entry<String, BinaryValue> entry : binariesTable.entrySet()) {
ZipEntry nze = new ZipEntry(entry.getKey());
zos.putNextEntry(nze);
entry.getValue().streamBinaryTo(zos);
}
zos.close();
zis.close();
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
} catch (IOException e) {
logger.error(e.getMessage(), e);
throw new XPathException("IO Exception in zip:update");
} catch (PermissionDeniedException e) {
logger.error(e.getMessage(), e);
throw new XPathException("Permission denied to read the source zip");
}
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class RestXqServiceImpl method parseAsString.
private static StringValue parseAsString(final InputStream is, final String encoding) throws IOException {
final String s;
try (final UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream(4096)) {
bos.write(is);
s = new String(bos.toByteArray(), encoding);
}
return new StringValue(s);
}
Aggregations