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;
}
}
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();
}
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;
}
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));
}
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));
}
Aggregations