use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class XmlRpcTest method getDocumentDataChunked_nextChunk.
@Test
public void getDocumentDataChunked_nextChunk() throws IOException, XmlRpcException {
final XmlRpcClient xmlrpc = getClient();
List<Object> params = new ArrayList<>();
params.add(TARGET_COLLECTION.toString());
Boolean result = (Boolean) xmlrpc.execute("createCollection", params);
assertTrue(result);
params.clear();
final String generatedXml = generateXml((int) (MAX_DOWNLOAD_CHUNK_SIZE * 1.5));
params.add(generatedXml);
params.add(TARGET_RESOURCE.toString());
params.add(1);
result = (Boolean) xmlrpc.execute("parse", params);
assertTrue(result);
params.clear();
final Map<String, Object> parameters = new HashMap<>();
parameters.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
parameters.put(OutputKeys.INDENT, "no");
params.add(TARGET_RESOURCE.toString());
params.add(parameters);
Map table = (Map) xmlrpc.execute("getDocumentData", params);
try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
int offset = (int) table.get("offset");
byte[] data = (byte[]) table.get("data");
os.write(data);
while (offset > 0) {
params.clear();
params.add(table.get("handle"));
params.add(offset);
table = (Map<?, ?>) xmlrpc.execute("getNextChunk", params);
offset = (int) table.get("offset");
data = (byte[]) table.get("data");
os.write(data);
}
data = os.toByteArray();
assertEquals(generatedXml, new String(data));
}
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class XmlRpcTest method getDocumentDataChunked_nextExtendedChunk.
@Test
public void getDocumentDataChunked_nextExtendedChunk() throws IOException, XmlRpcException {
final XmlRpcClient xmlrpc = getClient();
List<Object> params = new ArrayList<>();
params.add(TARGET_COLLECTION.toString());
Boolean result = (Boolean) xmlrpc.execute("createCollection", params);
assertTrue(result);
params.clear();
final String generatedXml = generateXml((int) (MAX_DOWNLOAD_CHUNK_SIZE * 1.75));
params.add(generatedXml);
params.add(TARGET_RESOURCE.toString());
params.add(1);
result = (Boolean) xmlrpc.execute("parse", params);
assertTrue(result);
params.clear();
final Map<String, Object> parameters = new HashMap<>();
parameters.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
parameters.put(OutputKeys.INDENT, "no");
params.add(TARGET_RESOURCE.toString());
params.add(parameters);
Map table = (Map) xmlrpc.execute("getDocumentData", params);
try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
long offset = (int) table.get("offset");
byte[] data = (byte[]) table.get("data");
os.write(data);
while (offset > 0) {
params.clear();
params.add(table.get("handle"));
params.add(String.valueOf(offset));
table = (Map<?, ?>) xmlrpc.execute("getNextExtendedChunk", params);
offset = Long.valueOf((String) table.get("offset"));
data = (byte[]) table.get("data");
os.write(data);
}
data = os.toByteArray();
assertEquals(generatedXml, new String(data));
}
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class XmlRpcTest method uploadCompressedAndDownload.
@Test
public void uploadCompressedAndDownload() throws IOException, XmlRpcException {
final XmlRpcClient xmlrpc = getClient();
final String resURI = XmldbURI.ROOT_COLLECTION_URI.append("test.bin").toString();
final Date now = new Date(System.currentTimeMillis());
final byte[] binary = generateBinary((int) (MAX_UPLOAD_CHUNK * 1.5));
// 1) upload
String uploadedFileName = null;
try (final InputStream is = new UnsynchronizedByteArrayInputStream(binary)) {
final byte[] chunk = new byte[MAX_UPLOAD_CHUNK];
int len;
while ((len = is.read(chunk)) > -1) {
final byte[] compressed = Compressor.compress(chunk, len);
final List<Object> params = new ArrayList<>();
if (uploadedFileName != null) {
params.add(uploadedFileName);
}
params.add(compressed);
params.add(len);
uploadedFileName = (String) xmlrpc.execute("uploadCompressed", params);
}
}
// set the properties of the uploaded file
final List<Object> paramsEx = new ArrayList<>();
paramsEx.add(uploadedFileName);
paramsEx.add(resURI);
paramsEx.add(Boolean.TRUE);
paramsEx.add("application/octet-stream");
paramsEx.add(Boolean.FALSE);
paramsEx.add(now);
paramsEx.add(now);
xmlrpc.execute("parseLocalExt", paramsEx);
// 2) download
final List<Object> params = new ArrayList<>();
params.add(resURI);
params.add(Collections.emptyMap());
Map table = (Map) xmlrpc.execute("getDocumentData", params);
try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
long offset = (int) table.get("offset");
byte[] data = (byte[]) table.get("data");
os.write(data);
while (offset > 0) {
params.clear();
params.add(table.get("handle"));
params.add(String.valueOf(offset));
table = (Map<?, ?>) xmlrpc.execute("getNextExtendedChunk", params);
offset = Long.valueOf((String) table.get("offset"));
data = (byte[]) table.get("data");
os.write(data);
}
data = os.toByteArray();
assertArrayEquals(binary, data);
}
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class Base64BinaryValueTypeTest method verify_validBase64_passes_large_string.
@Test
public void verify_validBase64_passes_large_string() throws XPathException, IOException, URISyntaxException {
Optional<Path> home = ConfigurationHelper.getExistHome();
Path binaryFile = Paths.get(getClass().getResource("logo.jpg").toURI());
final String base64data;
try (final InputStream is = new Base64InputStream(Files.newInputStream(binaryFile), true, -1, null);
final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
baos.write(is);
base64data = baos.toString(UTF_8);
}
assertNotNull(base64data);
TestableBase64BinaryValueType base64Type = new TestableBase64BinaryValueType();
base64Type.verifyString(base64data);
}
use of org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream in project exist by eXist-db.
the class BinaryValueFromInputStreamTest method filterFilter_withIncrementReferenceCount.
@Test
public void filterFilter_withIncrementReferenceCount() throws IOException, XPathException {
final BinaryValueManager binaryValueManager = new MockBinaryValueManager();
final byte[] testData = "test data".getBytes();
try (final InputStream bais = new UnsynchronizedByteArrayInputStream(testData)) {
final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais);
final InputStream bvis = binaryValue.getInputStream();
// create a filter over the first BinaryValue, and reference count increment
final InputStream fis1 = new BinaryValueFilteringInputStream(bvis, true);
final BinaryValue filteredBinaryValue1 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis1);
// create a second filter over the first filter, and reference count increment
final InputStream fbvis = filteredBinaryValue1.getInputStream();
final InputStream fis2 = new BinaryValueFilteringInputStream(fbvis, true);
final BinaryValue filteredBinaryValue2 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis2);
// we now destroy the second filtered binary value, just as it would if it went out of scope from popLocalVariables#popLocalVariables.
// It should not close the filtered binary value or original binary value, as BinaryValueFilteringInputStream increased the reference count.
filteredBinaryValue2.close();
assertTrue(filteredBinaryValue2.isClosed());
assertFalse(filteredBinaryValue1.isClosed());
assertFalse(binaryValue.isClosed());
// we should still be able to read from the filtered binary value!
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
filteredBinaryValue1.streamBinaryTo(baos);
assertArrayEquals(testData, baos.toByteArray());
}
// we now destroy the first filtered binary value, just as it would if it went out of scope from popLocalVariables#popLocalVariables.
// It should not close the original binary value, as BinaryValueFilteringInputStream increased the reference count.
fis2.close();
filteredBinaryValue1.close();
assertTrue(filteredBinaryValue1.isClosed());
// we should still be able to read from the origin binary value!
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
binaryValue.streamBinaryTo(baos);
assertArrayEquals(testData, baos.toByteArray());
}
// finally close the original binary value
bvis.close();
binaryValue.close();
assertTrue(binaryValue.isClosed());
} finally {
binaryValueManager.runCleanupTasks();
}
}
Aggregations