Search in sources :

Example 6 with UnsynchronizedByteArrayInputStream

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

the class RemoteCollection method uploadAndStore.

private void uploadAndStore(final Resource res) throws XMLDBException {
    InputStream is = null;
    String descString = "<unknown>";
    try {
        if (res instanceof RemoteBinaryResource) {
            is = ((RemoteBinaryResource) res).getStreamContent();
            descString = ((RemoteBinaryResource) res).getStreamSymbolicPath();
        } else {
            final Object content = res.getContent();
            if (content instanceof File) {
                final File file = (File) content;
                try {
                    is = new BufferedInputStream(new FileInputStream(file));
                } catch (final FileNotFoundException e) {
                    throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "could not read resource from file " + file.getAbsolutePath(), e);
                }
            } else if (content instanceof InputSource) {
                is = ((InputSource) content).getByteStream();
                if (content instanceof EXistInputSource) {
                    descString = ((EXistInputSource) content).getSymbolicPath();
                }
            } else if (content instanceof String) {
                // TODO(AR) we really should not allow String to be used here, as we loose the encoding info and default to UTF-8!
                is = new UnsynchronizedByteArrayInputStream(((String) content).getBytes(UTF_8));
            } else {
                LOG.error("Unable to get content from {}", content);
            }
        }
        final byte[] chunk;
        if (res instanceof ExtendedResource) {
            if (res instanceof AbstractRemoteResource) {
                final long contentLen = ((AbstractRemoteResource) res).getContentLength();
                if (contentLen != -1) {
                    // content length is known
                    chunk = new byte[(int) Math.min(contentLen, MAX_UPLOAD_CHUNK)];
                } else {
                    chunk = new byte[MAX_UPLOAD_CHUNK];
                }
            } else {
                final long streamLen = ((ExtendedResource) res).getStreamLength();
                if (streamLen != -1) {
                    // stream length is known
                    chunk = new byte[(int) Math.min(streamLen, MAX_UPLOAD_CHUNK)];
                } else {
                    chunk = new byte[MAX_UPLOAD_CHUNK];
                }
            }
        } else {
            chunk = new byte[MAX_UPLOAD_CHUNK];
        }
        try {
            String fileName = null;
            if (chunk.length > 0) {
                int len;
                while ((len = is.read(chunk)) > -1) {
                    final List<Object> params = new ArrayList<>();
                    if (fileName != null) {
                        params.add(fileName);
                    }
                    /*
                    Only compress the chunk if it is larger than 256 bytes,
                    otherwise the compression framing overhead results in a larger chunk
                    */
                    if (len < 256) {
                        params.add(chunk);
                        params.add(len);
                        fileName = (String) execute("upload", params);
                    } else {
                        final byte[] compressed = Compressor.compress(chunk, len);
                        params.add(compressed);
                        params.add(len);
                        fileName = (String) execute("uploadCompressed", params);
                    }
                }
            }
            if (fileName == null) {
                // Zero length stream? Let's get a fileName!
                final List<Object> params = new ArrayList<>();
                params.add(new byte[0]);
                params.add(0);
                fileName = (String) execute("upload", params);
            }
            final List<Object> params = new ArrayList<>();
            final List<Object> paramsEx = new ArrayList<>();
            params.add(fileName);
            paramsEx.add(fileName);
            try {
                final String resURI = getPathURI().append(XmldbURI.xmldbUriFor(res.getId())).toString();
                params.add(resURI);
                paramsEx.add(resURI);
            } catch (final URISyntaxException e) {
                throw new XMLDBException(ErrorCodes.INVALID_URI, e);
            }
            params.add(Boolean.TRUE);
            paramsEx.add(Boolean.TRUE);
            if (res instanceof EXistResource) {
                final EXistResource rxres = (EXistResource) res;
                params.add(rxres.getMimeType());
                paramsEx.add(rxres.getMimeType());
                // This one is only for the new style!!!!
                paramsEx.add((BinaryResource.RESOURCE_TYPE.equals(res.getResourceType())) ? Boolean.FALSE : Boolean.TRUE);
                if (rxres.getCreationTime() != null) {
                    params.add(rxres.getCreationTime());
                    paramsEx.add(rxres.getCreationTime());
                    params.add(rxres.getLastModificationTime());
                    paramsEx.add(rxres.getLastModificationTime());
                }
            }
            try {
                execute("parseLocalExt", paramsEx);
            } catch (final XMLDBException e) {
                // Identifying old versions
                final String excMsg = e.getCause().getMessage();
                if (excMsg.contains("No such handler") || excMsg.contains("No method matching")) {
                    execute("parseLocal", params);
                } else {
                    throw e;
                }
            }
        } catch (final IOException e) {
            throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "failed to read resource from " + descString, e);
        }
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (final IOException ioe) {
                LOG.warn(ioe.getMessage(), ioe);
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) EXistInputSource(org.exist.util.EXistInputSource) BufferedInputStream(java.io.BufferedInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) EXistInputSource(org.exist.util.EXistInputSource) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) File(java.io.File)

Example 7 with UnsynchronizedByteArrayInputStream

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

the class CollectionStoreTest method storeBinary.

private void storeBinary(final PreserveType preserveOnCopy) throws EXistException, PermissionDeniedException, IOException, TriggerException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        try (final Collection col = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI)) {
            final byte[] bin = TEST_BIN_DOC.getBytes(UTF_8);
            try (final InputStream is = new UnsynchronizedByteArrayInputStream(bin)) {
                final int docId = broker.getNextResourceId(transaction);
                final BinaryDocument binDoc = col.addBinaryResource(transaction, broker, new BinaryDocument(broker.getBrokerPool(), col, docId, TEST_BIN_DOC_URI), is, "text/plain", bin.length, null, null, preserveOnCopy);
                assertNotNull(binDoc);
            }
            broker.saveCollection(transaction, col);
        }
        try (final Collection col = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
            try (final LockedDocument lockedDoc = col.getDocumentWithLock(broker, TEST_BIN_DOC_URI, LockMode.READ_LOCK)) {
                // NOTE: early release of collection lock inline with async locking
                col.close();
                if (lockedDoc != null) {
                    assertTrue(lockedDoc.getDocument() instanceof BinaryDocument);
                    final BinaryDocument doc = (BinaryDocument) lockedDoc.getDocument();
                    final Try<String, IOException> docContent = broker.withBinaryFile(transaction, doc, is -> Try.TaggedTryUnchecked(IOException.class, () -> new String(Files.readAllBytes(is), UTF_8)));
                    assertEquals(TEST_BIN_DOC, docContent.get());
                }
            }
        }
        transaction.commit();
    }
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) BrokerPool(org.exist.storage.BrokerPool)

Example 8 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest method readBytes.

@Test
public void readBytes() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    final String testString = "helloWorld";
    final byte[] testData = testString.getBytes();
    InputStream is = new UnsynchronizedByteArrayInputStream(testData);
    CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    // read the first 3 bytes
    byte[] result = new byte[3];
    int read = cfis.read(result);
    assertEquals(3, read);
    assertArrayEquals(subArray(testData, 3), result);
    // mark position
    cfis.mark(Integer.MAX_VALUE);
    // read the next 3 bytes
    result = new byte[3];
    read = cfis.read(result);
    assertEquals(3, read);
    assertArrayEquals(subArray(testData, 3, 3), result);
    // reset position to the mark
    cfis.reset();
    // attempt to reread the last 3 bytes from the mark (from the cache)
    result = new byte[3];
    read = cfis.read(result);
    assertEquals(3, read);
    assertArrayEquals(subArray(testData, 3, 3), result);
    // read the next 2 bytes past the reset mark (past the cache, e.g. from src)
    result = new byte[2];
    read = cfis.read(result);
    assertEquals(2, read);
    assertArrayEquals(subArray(testData, 6, 2), result);
    // reset position to the mark
    cfis.reset();
    // attempt to read the last 5 bytes (from the cache)
    result = new byte[5];
    read = cfis.read(result);
    assertEquals(5, read);
    assertArrayEquals(subArray(testData, 3, 5), result);
    // mark position
    cfis.mark(-1);
    // read the next 2 bytes past the reset mark (past the cache, e.g. from src)
    result = new byte[2];
    read = cfis.read(result);
    assertEquals(2, read);
    assertArrayEquals(subArray(testData, 8, 2), result);
    // reset position to the mark
    cfis.reset();
    // attempt to reread the last 2 bytes from the mark (from the cache)
    result = new byte[2];
    read = cfis.read(result);
    assertEquals(2, read);
    assertArrayEquals(subArray(testData, 8, 2), result);
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 9 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest method readByte_allFromCache.

@Test
public void readByte_allFromCache() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    final String testString = "hello";
    final byte[] testData = testString.getBytes();
    InputStream is = new UnsynchronizedByteArrayInputStream(testData);
    CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    // mark the position
    cfis.mark(Integer.MAX_VALUE);
    // read the data
    assertEquals(testData[0], cfis.read());
    assertEquals(testData[1], cfis.read());
    assertEquals(testData[2], cfis.read());
    assertEquals(testData[3], cfis.read());
    assertEquals(testData[4], cfis.read());
    // reset position to the mark
    cfis.reset();
    // attempt to reread the data (from the cache)
    assertEquals(testData[0], cfis.read());
    assertEquals(testData[1], cfis.read());
    assertEquals(testData[2], cfis.read());
    assertEquals(testData[3], cfis.read());
    assertEquals(testData[4], cfis.read());
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 10 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest method available_onEmptyStream.

@Test
public void available_onEmptyStream() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    InputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {});
    CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    cfis.close();
    assertEquals(0, cfis.available());
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) 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