Search in sources :

Example 11 with UnsynchronizedByteArrayOutputStream

use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.

the class RestBinariesTest method streamBinaryRaw.

/**
 * {@see https://github.com/eXist-db/exist/issues/790#error-case-2}
 *
 * response:stream-binary is used to return raw binary.
 */
@Test
public void streamBinaryRaw() throws JAXBException, IOException {
    final String query = "import module namespace util = \"http://exist-db.org/xquery/util\";\n" + "import module namespace response = \"http://exist-db.org/xquery/response\";\n" + "let $bin := util:binary-doc('" + TEST_COLLECTION.append(BIN1_FILENAME).toString() + "')\n" + "return response:stream-binary($bin, 'media-type=application/octet-stream', ())";
    final HttpResponse response = postXquery(query);
    final HttpEntity entity = response.getEntity();
    try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
        entity.writeTo(baos);
        assertArrayEquals(BIN1_CONTENT, baos.toByteArray());
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Example 12 with UnsynchronizedByteArrayOutputStream

use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.

the class LockTable method xmlFullDumpToLog.

@Override
public void xmlFullDumpToLog() {
    try (final UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream();
        final Writer writer = new OutputStreamWriter(bos)) {
        LockTableUtils.stateToXml(pool.getLockManager().getLockTable(), true, writer);
        LOCK_LOG.info(new String(bos.toByteArray(), UTF_8));
    } catch (final IOException | XMLStreamException e) {
        throw new RuntimeException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 13 with UnsynchronizedByteArrayOutputStream

use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.

the class BinaryValueFromBinaryString method convertTo.

@Override
public BinaryValue convertTo(BinaryValueType binaryValueType) throws XPathException {
    // TODO temporary approach, consider implementing a TranscodingBinaryValueFromBinaryString(BinaryValueFromBinaryString) class
    // that only does the transncoding lazily
    final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
    FilterOutputStream fos = null;
    try {
        // transcode
        fos = binaryValueType.getEncoder(baos);
        streamBinaryTo(fos);
    } catch (final IOException ioe) {
        throw new XPathException(ioe);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (final IOException ioe) {
                LOG.error("Unable to close stream: {}", ioe.getMessage(), ioe);
            }
        }
        try {
            baos.close();
        } catch (final IOException ioe) {
            LOG.error("Unable to close stream: {}", ioe.getMessage(), ioe);
        }
    }
    return new BinaryValueFromBinaryString(binaryValueType, baos.toString(UTF_8));
}
Also used : XPathException(org.exist.xquery.XPathException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)

Example 14 with UnsynchronizedByteArrayOutputStream

use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.

the class BlobStoreImplTest method readAll.

private Tuple2<byte[], MessageDigest> readAll(InputStream is) throws IOException {
    final StreamableDigest streamableDigest = DIGEST_TYPE.newStreamableDigest();
    is = new DigestInputStream(is, streamableDigest);
    try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
        os.write(is);
        return Tuple(os.toByteArray(), streamableDigest.copyMessageDigest());
    }
}
Also used : DigestInputStream(org.exist.util.crypto.digest.DigestInputStream) StreamableDigest(org.exist.util.crypto.digest.StreamableDigest) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)

Example 15 with UnsynchronizedByteArrayOutputStream

use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.

the class StoreResourceTest method replaceBinDoc.

private void replaceBinDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
    final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
    final BrokerPool pool = existWebServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
        broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content.getBytes(UTF_8)), MimeType.BINARY_TYPE, col);
        transaction.commit();
    }
    // check the replaced document is correct
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK);
        final InputStream is = broker.getBinaryResource((BinaryDocument) lockedDoc.getDocument());
        final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
        os.write(is);
        assertArrayEquals(content.getBytes(UTF_8), os.toByteArray());
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) InputStream(java.io.InputStream) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) XmldbURI(org.exist.xmldb.XmldbURI)

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