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());
}
}
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);
}
}
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));
}
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());
}
}
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());
}
}
Aggregations