Search in sources :

Example 61 with UnsynchronizedByteArrayOutputStream

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);
        }
    }
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) OutputFormat(org.apache.xml.serialize.OutputFormat) Collection(org.xmldb.api.base.Collection) Document(org.w3c.dom.Document) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Example 62 with UnsynchronizedByteArrayOutputStream

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) SAXException(org.xml.sax.SAXException)

Example 63 with UnsynchronizedByteArrayOutputStream

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Source(org.exist.source.Source) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 64 with UnsynchronizedByteArrayOutputStream

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");
    }
}
Also used : HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) ZipInputStream(java.util.zip.ZipInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) PermissionDeniedException(org.exist.security.PermissionDeniedException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 65 with UnsynchronizedByteArrayOutputStream

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);
}
Also used : UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) StringValue(org.exist.xquery.value.StringValue)

Aggregations

UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)65 Test (org.junit.Test)24 InputStream (java.io.InputStream)23 IOException (java.io.IOException)20 HttpResponse (org.apache.http.HttpResponse)14 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)11 XPathException (org.exist.xquery.XPathException)11 FilterInputStream (java.io.FilterInputStream)7 SAXException (org.xml.sax.SAXException)7 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)6 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)6 Path (java.nio.file.Path)5 XmldbURI (org.exist.xmldb.XmldbURI)5 BinaryValue (org.exist.xquery.value.BinaryValue)5 HttpEntity (org.apache.http.HttpEntity)4 Request (org.apache.http.client.fluent.Request)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)4 Image (java.awt.Image)3 BufferedImage (java.awt.image.BufferedImage)3 XmlRpcClient (org.apache.xmlrpc.client.XmlRpcClient)3