Search in sources :

Example 46 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method readBytes_pastEndOfStream.

@Test
public void readBytes_pastEndOfStream() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    final String testString = "helloWorld";
    final byte[] testData = testString.getBytes();
    final InputStream is = new UnsynchronizedByteArrayInputStream(testData);
    final CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    final byte[] result = new byte[testData.length];
    int read = cfis.read(result);
    assertEquals(testData.length, read);
    assertArrayEquals(testData, result);
    final byte[] endOfStreamResult = new byte[testData.length];
    read = cfis.read(endOfStreamResult);
    assertEquals(-1, read);
    assertArrayEquals(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, endOfStreamResult);
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) MarkShieldInputStream(org.apache.commons.io.input.MarkShieldInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 47 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class LDAPRealmTest method setUpBeforeClass.

/**
 * @throws java.lang.Exception
 */
@BeforeClass
public static void setUpBeforeClass() throws Exception {
    try (final InputStream is = new UnsynchronizedByteArrayInputStream(config.getBytes(UTF_8))) {
        Configuration config = Configurator.parse(is);
        realm = new LDAPRealm(null, config);
    }
}
Also used : Configuration(org.exist.config.Configuration) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) BeforeClass(org.junit.BeforeClass)

Example 48 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class BlobStoreImplTest method addAndVerify.

private BlobId addAndVerify(final BlobStore blobStore, final Tuple2<byte[], MessageDigest> blob) throws IOException {
    final Tuple2<BlobId, Long> actualBlob;
    try (final InputStream is = new UnsynchronizedByteArrayInputStream(blob._1)) {
        actualBlob = blobStore.add(null, is);
    }
    assertNotNull(actualBlob);
    assertArrayEquals(blob._2.getValue(), actualBlob._1.getId());
    return actualBlob._1;
}
Also used : DigestInputStream(org.exist.util.crypto.digest.DigestInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)

Example 49 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream 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 50 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream 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

UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)114 InputStream (java.io.InputStream)102 Test (org.junit.Test)93 MarkShieldInputStream (org.apache.commons.io.input.MarkShieldInputStream)31 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)10 IOException (java.io.IOException)8 FilterInputStream (java.io.FilterInputStream)7 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)7 XMLResource (org.xmldb.api.modules.XMLResource)6 DBBroker (org.exist.storage.DBBroker)5 Txn (org.exist.storage.txn.Txn)5 Element (org.w3c.dom.Element)4 Collection (org.xmldb.api.base.Collection)4 NodeProxy (org.exist.dom.persistent.NodeProxy)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 DigestInputStream (org.exist.util.crypto.digest.DigestInputStream)3 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)3 BooleanValue (org.exist.xquery.value.BooleanValue)3 DoubleValue (org.exist.xquery.value.DoubleValue)3 StringValue (org.exist.xquery.value.StringValue)3