Search in sources :

Example 16 with ByteSource

use of com.google.common.io.ByteSource in project hale by halestudio.

the class MsAccessDataReaderTestSuit method createSourceTempFile.

/**
 * Copies the source database to a temporary file.
 *
 * @throws IOException if temp file can't be created
 */
public void createSourceTempFile() throws IOException {
    ByteSource source = Resources.asByteSource(MsAccessDataReaderTestSuit.class.getClassLoader().getResource(SOURCE_DB_PATH));
    ByteSink dest = Files.asByteSink(getSourceTempFilePath());
    source.copyTo(dest);
}
Also used : ByteSink(com.google.common.io.ByteSink) ByteSource(com.google.common.io.ByteSource)

Example 17 with ByteSource

use of com.google.common.io.ByteSource in project hale by halestudio.

the class SpatiaLiteTestSuite method createSourceTempFile.

/**
 * Copies the source database to a temporary file.
 *
 * @throws IOException if temp file can't be created
 */
public void createSourceTempFile() throws IOException {
    ByteSource source = Resources.asByteSource(SpatiaLiteTestSuite.class.getResource(SOURCE_DB_LOCATION));
    ByteSink dest = Files.asByteSink(new File(getSourceTempFilePath()));
    source.copyTo(dest);
}
Also used : ByteSink(com.google.common.io.ByteSink) ByteSource(com.google.common.io.ByteSource) File(java.io.File)

Example 18 with ByteSource

use of com.google.common.io.ByteSource in project hale by halestudio.

the class SpatiaLiteTestSuite method createTargetTempFile.

/**
 * Copies the target database to a temporary file and deletes all instances
 * in it.
 *
 * @throws IOException if temp file can't be created or instances can't be
 *             deleted
 */
public void createTargetTempFile() throws IOException {
    ByteSource source = Resources.asByteSource(SpatiaLiteTestSuite.class.getResource(SOURCE_DB_LOCATION));
    ByteSink dest = Files.asByteSink(new File(getTargetTempFilePath()));
    source.copyTo(dest);
    Connection conn = null;
    try {
        conn = DriverManager.getConnection("jdbc:sqlite:" + getTargetTempFilePath());
        Statement stmt = conn.createStatement();
        stmt.executeUpdate("DELETE FROM " + SOUURCE_TYPE_LOCAL_NAME);
    } catch (SQLException e) {
        throw new IOException("Could not empty target DB", e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            // ignore
            }
        }
    }
}
Also used : ByteSink(com.google.common.io.ByteSink) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) File(java.io.File)

Example 19 with ByteSource

use of com.google.common.io.ByteSource in project ddf by codice.

the class InMemoryProcessingFramework method storeProcessRequest.

private <T extends ProcessResourceItem> void storeProcessRequest(ProcessRequest<T> processRequest) {
    LOGGER.trace("Storing update request post processing change(s)");
    Map<String, ContentItem> contentItemsToUpdate = new HashMap<>();
    Map<String, Metacard> metacardsToUpdate = new HashMap<>();
    List<TemporaryFileBackedOutputStream> tfbosToCleanUp = new ArrayList<>();
    for (T item : processRequest.getProcessItems()) {
        if (item.getProcessResource() == null && item.isMetacardModified()) {
            metacardsToUpdate.put(item.getMetacard().getId(), item.getMetacard());
        }
        final ProcessResource processResource = item.getProcessResource();
        TemporaryFileBackedOutputStream tfbos = null;
        if (processResource != null && processResource.isModified() && !contentItemsToUpdate.containsKey(getContentItemKey(item.getMetacard(), processResource))) {
            try {
                tfbos = new TemporaryFileBackedOutputStream();
                long numberOfBytes = IOUtils.copyLarge(processResource.getInputStream(), tfbos);
                LOGGER.debug("Copied {} bytes to TemporaryFileBackedOutputStream.", numberOfBytes);
                ByteSource byteSource = tfbos.asByteSource();
                ContentItem contentItem = new ContentItemImpl(item.getMetacard().getId(), processResource.getQualifier(), byteSource, processResource.getMimeType(), processResource.getName(), processResource.getSize(), item.getMetacard());
                contentItemsToUpdate.put(getContentItemKey(item.getMetacard(), processResource), contentItem);
                tfbosToCleanUp.add(tfbos);
            } catch (IOException | RuntimeException e) {
                LOGGER.debug("Unable to store process request", e);
                if (tfbos != null) {
                    close(tfbos);
                }
            }
        }
    }
    storeContentItemUpdates(contentItemsToUpdate, processRequest.getProperties());
    storeMetacardUpdates(metacardsToUpdate, processRequest.getProperties());
    closeTfbos(tfbosToCleanUp);
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessResource(org.codice.ddf.catalog.async.data.api.internal.ProcessResource) IOException(java.io.IOException) Metacard(ddf.catalog.data.Metacard) ByteSource(com.google.common.io.ByteSource) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 20 with ByteSource

use of com.google.common.io.ByteSource in project ddf by codice.

the class TestTemporaryFileBackedOutputStream method testWriteByte.

@Test
public void testWriteByte() throws IOException {
    temporaryFileBackedOutputStream.write(TEST_BYTE);
    ByteSource byteSource = temporaryFileBackedOutputStream.asByteSource();
    assertThat(byteSource.read(), is(TEST_BYTE_ARRAY));
}
Also used : ByteSource(com.google.common.io.ByteSource) Test(org.junit.Test)

Aggregations

ByteSource (com.google.common.io.ByteSource)138 IOException (java.io.IOException)58 Test (org.junit.Test)58 InputStream (java.io.InputStream)42 ByteArrayInputStream (java.io.ByteArrayInputStream)33 File (java.io.File)33 ContentItemImpl (ddf.catalog.content.data.impl.ContentItemImpl)18 Metacard (ddf.catalog.data.Metacard)17 ContentItem (ddf.catalog.content.data.ContentItem)16 StringWriter (java.io.StringWriter)14 FileInputStream (java.io.FileInputStream)13 Test (org.junit.jupiter.api.Test)12 URI (java.net.URI)11 Path (java.nio.file.Path)11 ArrayList (java.util.ArrayList)11 URL (java.net.URL)10 CreateStorageRequestImpl (ddf.catalog.content.operation.impl.CreateStorageRequestImpl)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)9 FilterInputStream (java.io.FilterInputStream)8