Search in sources :

Example 36 with ByteSource

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

the class OrderRequestImpl method writeMultipleFiles.

private void writeMultipleFiles(DestinationSink destinationSink, PackagingSpecFormatType packagingSpecFormatType, List<ResourceContainer> files, String filename, List<String> sentFiles) throws IOException {
    int totalNum = files.size() + 1;
    String totalNumPortion = String.format(FILE_COUNT_FORMAT, totalNum);
    switch(packagingSpecFormatType) {
        case FILESUNC:
            {
                int currNum = 1;
                for (ResourceContainer file : files) {
                    String currNumPortion = String.format(FILE_COUNT_FORMAT, currNum);
                    String currFileName = filename + "." + currNumPortion + "." + totalNumPortion;
                    try (InputStream fileInputStream = file.getInputStream()) {
                        destinationSink.writeFile(fileInputStream, file.getSize(), currFileName, file.getMimeTypeValue(), Collections.singletonList(file.getMetacard()));
                        currNum++;
                        sentFiles.add(currFileName);
                    }
                }
            }
            break;
        case FILESCOMPRESS:
            {
                int currNum = 1;
                for (ResourceContainer file : files) {
                    try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        ZipOutputStream zipOut = new ZipOutputStream(fos);
                        InputStream fileInputStream = file.getInputStream()) {
                        getZip(zipOut, fileInputStream, file.getName());
                        ByteSource contents = fos.asByteSource();
                        String currNumPortion = String.format(FILE_COUNT_FORMAT, currNum);
                        String currFileName = filename + "." + currNumPortion + "." + totalNumPortion + packagingSpecFormatType.getExtension();
                        try (InputStream inputStream = contents.openStream()) {
                            destinationSink.writeFile(inputStream, contents.size(), currFileName, packagingSpecFormatType.getContentType(), Collections.singletonList(file.getMetacard()));
                            sentFiles.add(currFileName);
                        }
                        currNum++;
                    }
                }
            }
            break;
        case FILESZIP:
            {
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    ZipOutputStream zipOut = new ZipOutputStream(fos)) {
                    getZip(zipOut, files);
                    ByteSource zip = fos.asByteSource();
                    writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, zip, files.stream().map(ResourceContainer::getMetacard).collect(Collectors.toList()));
                }
            }
            break;
        case FILESGZIP:
            {
                int currNum = 1;
                for (ResourceContainer file : files) {
                    try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        GZIPOutputStream zipOut = new GZIPOutputStream(fos);
                        InputStream fileInputStream = file.getInputStream()) {
                        getGzip(zipOut, fileInputStream);
                        ByteSource contents = fos.asByteSource();
                        String currNumPortion = String.format(FILE_COUNT_FORMAT, currNum);
                        String currFileName = filename + "." + currNumPortion + "." + totalNumPortion + packagingSpecFormatType.getExtension();
                        try (InputStream inputStream = contents.openStream()) {
                            destinationSink.writeFile(inputStream, contents.size(), currFileName, packagingSpecFormatType.getContentType(), Collections.singletonList(file.getMetacard()));
                            sentFiles.add(currFileName);
                        }
                        currNum++;
                    }
                }
            }
            break;
        case TARUNC:
            {
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    TarOutputStream tarOut = new TarOutputStream(fos)) {
                    getTar(tarOut, files);
                    ByteSource tar = fos.asByteSource();
                    writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, tar, files.stream().map(ResourceContainer::getMetacard).collect(Collectors.toList()));
                }
            }
            break;
        case TARZIP:
            {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, files);
                    try (TemporaryFileBackedOutputStream zipFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        ZipOutputStream zipOut = new ZipOutputStream(zipFos)) {
                        getZip(zipOut, tarFos.asByteSource().openStream(), filename + ".tar");
                        ByteSource zip = zipFos.asByteSource();
                        writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, zip, files.stream().map(ResourceContainer::getMetacard).collect(Collectors.toList()));
                    }
                }
            }
            break;
        case TARGZIP:
            {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, files);
                    try (TemporaryFileBackedOutputStream gzipFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        GZIPOutputStream zipOut = new GZIPOutputStream(gzipFos)) {
                        getGzip(zipOut, tarFos.asByteSource().openStream());
                        ByteSource zip = gzipFos.asByteSource();
                        writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, zip, files.stream().map(ResourceContainer::getMetacard).collect(Collectors.toList()));
                    }
                }
            }
            break;
        case TARCOMPRESS:
            {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, files);
                    try (TemporaryFileBackedOutputStream zipFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        ZipOutputStream zipOut = new ZipOutputStream(zipFos)) {
                        getZip(zipOut, tarFos.asByteSource().openStream(), filename + ".tar");
                        writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, zipFos.asByteSource(), files.stream().map(ResourceContainer::getMetacard).collect(Collectors.toList()));
                    }
                }
            }
            break;
        default:
            LOGGER.debug("Unknown packaging format type, skipping");
            break;
    }
}
Also used : TarOutputStream(org.kamranzafar.jtar.TarOutputStream) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteSource(com.google.common.io.ByteSource) NsiliEndpoint(org.codice.alliance.nsili.endpoint.NsiliEndpoint)

Example 37 with ByteSource

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

the class CatalogOutputAdapterTest method testGetNitfSegmentsFlowTFBOSAsBytesourceThrows.

@SuppressWarnings("unchecked")
@Test
public void testGetNitfSegmentsFlowTFBOSAsBytesourceThrows() throws IOException, NitfFormatException {
    ByteSource byteSource = mock(ByteSource.class);
    when(byteSource.openBufferedStream()).thenThrow(IOException.class);
    TemporaryFileBackedOutputStream tfbos = mock(TemporaryFileBackedOutputStream.class);
    when(tfbos.asByteSource()).thenReturn(byteSource);
    catalogOutputAdapter = new CatalogOutputAdapter() {

        @Override
        protected TemporaryFileBackedOutputStream createTemporaryFileBackedOutputStream() {
            return tfbos;
        }
    };
    try {
        catalogOutputAdapter.getNitfSegmentsFlow(new ByteArrayInputStream(new byte[] { (byte) 0 }));
        fail("expected an exception, shouldn't reach this line");
    } catch (IOException | NitfFormatException e) {
        assertThat(e, notNullValue());
    }
    verify(tfbos).close();
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) NitfFormatException(org.codice.imaging.nitf.core.common.NitfFormatException) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) Test(org.junit.Test)

Example 38 with ByteSource

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

the class NitfPreStoragePlugin method createOriginalImage.

private ContentItem createOriginalImage(String id, BufferedImage image, Metacard metacard) {
    try {
        byte[] originalBytes = renderToJpeg2k(image);
        ByteSource source = ByteSource.wrap(originalBytes);
        ContentItem contentItem = new ContentItemImpl(id, ORIGINAL, source, IMAGE_JPEG2K, buildDerivedImageTitle(metacard.getTitle(), ORIGINAL, JP2), originalBytes.length, metacard);
        addDerivedResourceAttribute(metacard, contentItem);
        return contentItem;
    } catch (IOException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    return null;
}
Also used : ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) ContentItem(ddf.catalog.content.data.ContentItem) ContentItemImpl(ddf.catalog.content.data.impl.ContentItemImpl)

Example 39 with ByteSource

use of com.google.common.io.ByteSource in project tutorials by eugenp.

the class GuavaIOUnitTest method whenReadUsingByteSource_thenRead.

@Test
public void whenReadUsingByteSource_thenRead() throws IOException {
    final String expectedValue = "Hello world";
    final File file = new File("src/test/resources/test1.in");
    final ByteSource source = Files.asByteSource(file);
    final byte[] result = source.read();
    assertEquals(expectedValue, new String(result));
}
Also used : ByteSource(com.google.common.io.ByteSource) File(java.io.File) Test(org.junit.Test)

Example 40 with ByteSource

use of com.google.common.io.ByteSource in project tutorials by eugenp.

the class JavaInputStreamToXUnitTest method givenUsingGuava_whenConvertingAnInputStreamToAString_thenCorrect.

@Test
public final void givenUsingGuava_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
    final String originalString = randomAlphabetic(DEFAULT_SIZE);
    final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
    final ByteSource byteSource = new ByteSource() {

        @Override
        public final InputStream openStream() throws IOException {
            return inputStream;
        }
    };
    final String text = byteSource.asCharSource(Charsets.UTF_8).read();
    assertThat(text, equalTo(originalString));
}
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