Search in sources :

Example 16 with UnsynchronizedByteArrayOutputStream

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));
    }
}
Also used : XmlRpcClient(org.apache.xmlrpc.client.XmlRpcClient) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Example 17 with UnsynchronizedByteArrayOutputStream

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));
    }
}
Also used : XmlRpcClient(org.apache.xmlrpc.client.XmlRpcClient) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Example 18 with UnsynchronizedByteArrayOutputStream

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);
    }
}
Also used : XmlRpcClient(org.apache.xmlrpc.client.XmlRpcClient) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 19 with UnsynchronizedByteArrayOutputStream

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);
}
Also used : Path(java.nio.file.Path) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) InputStream(java.io.InputStream) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Example 20 with UnsynchronizedByteArrayOutputStream

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();
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

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