Example usage for java.io UncheckedIOException UncheckedIOException

List of usage examples for java.io UncheckedIOException UncheckedIOException

Introduction

In this page you can find the example usage for java.io UncheckedIOException UncheckedIOException.

Prototype

public UncheckedIOException(IOException cause) 

Source Link

Document

Constructs an instance of this class.

Usage

From source file:com.joyent.manta.client.jobs.MantaClientJobIT.java

@Test
public void canListOutputsForJobAsStreams() throws IOException, InterruptedException {
    String path1 = String.format("%s/%s", testPathPrefix, UUID.randomUUID());
    mantaClient.put(path1, TEST_DATA);/*from   ww  w  . ja v  a2s.c  o  m*/

    String path2 = String.format("%s/%s", testPathPrefix, UUID.randomUUID());
    mantaClient.put(path2, TEST_DATA);

    final MantaJob job = buildJob();
    final UUID jobId = mantaClient.createJob(job);

    List<String> inputs = new ArrayList<>();
    inputs.add(path1);
    inputs.add(path2);

    mantaClient.addJobInputs(jobId, inputs.iterator());
    Assert.assertTrue(mantaClient.endJobInput(jobId));

    awaitJobCompletion(jobId);

    final AtomicInteger count = new AtomicInteger(0);

    mantaClient.getJobOutputsAsStreams(jobId).forEach(o -> {
        count.incrementAndGet();
        try {
            String content = IOUtils.toString(o, Charset.defaultCharset());
            Assert.assertEquals(content, TEST_DATA);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });

    Assert.assertEquals(count.get(), 2, "Missing both outputs");
}

From source file:com.example.bot.spring.KitchenSinkController.java

private static DownloadedContent saveContent(String ext, ResponseBody responseBody) {
    log.info("Got filename: {}", responseBody.contentType());
    DownloadedContent tempFile = createTempFile(ext);
    try (OutputStream outputStream = Files.newOutputStream(tempFile.path)) {
        ByteStreams.copy(responseBody.byteStream(), outputStream);
        log.info("Saved {}: {}", ext, tempFile);
        return tempFile;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }//from w w  w  . jav  a  2  s. com
}

From source file:org.nuxeo.ecm.core.io.download.DownloadServiceImpl.java

@Override
public void transferBlobWithByteRange(Blob blob, ByteRange byteRange,
        Supplier<OutputStream> outputStreamSupplier) throws UncheckedIOException {
    try (InputStream in = blob.getStream()) {
        @SuppressWarnings("resource")
        OutputStream out = outputStreamSupplier.get(); // not ours to close
        BufferingServletOutputStream.stopBuffering(out);
        if (byteRange == null) {
            IOUtils.copy(in, out);/*from w ww . j a v  a2s.  c  o m*/
        } else {
            IOUtils.copyLarge(in, out, byteRange.getStart(), byteRange.getLength());
        }
        out.flush();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:com.joyent.manta.client.multipart.EncryptedJobsMultipartManagerIT.java

public void canReturnEmptyMultipartList() throws IOException {
    final List<MantaMultipartUpload> list;

    try (Stream<MantaMultipartUpload> inProgress = multipart.listInProgress()) {
        list = inProgress.collect(Collectors.toList());
    }/*from w  ww .j  a  v  a2 s.  c  o m*/

    if (!list.isEmpty()) {
        System.err.println("List should be empty. Actually had " + list.size() + " elements");
        list.forEach(element -> {
            System.err.println(element.getPath());

            if (element instanceof EncryptedMultipartUpload) {
                @SuppressWarnings("unchecked")
                EncryptedMultipartUpload<JobsMultipartUpload> jobsUpload = (EncryptedMultipartUpload<JobsMultipartUpload>) element;
                try (Stream<MantaMultipartUploadPart> innerStream = multipart.listParts(jobsUpload)) {
                    innerStream.forEach(part -> System.err.println("   " + part.getObjectPath()));
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        });
        throw new SkipException("List should be empty. Actually had " + list.size() + " elements");
    } else {
        assertTrue(true);
    }
}

From source file:com.example.bot.spring.KitchenSinkController.java

private void system(String... args) {
    ProcessBuilder processBuilder = new ProcessBuilder(args);
    try {/*  w  w  w.ja v a2s.com*/
        Process start = processBuilder.start();
        int i = start.waitFor();
        log.info("result: {} =>  {}", Arrays.toString(args), i);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (InterruptedException e) {
        log.info("Interrupted", e);
        Thread.currentThread().interrupt();
    }
}

From source file:com.joyent.manta.client.MantaClient.java

/**
 * Recursively deletes an object in Manta.
 *
 * @param path The fully qualified path of the Manta object.
 * @throws IOException If an IO exception has occurred.
 * @throws MantaClientHttpResponseException If a http status code {@literal > 300} is returned.
 *//*from ww w  . java 2 s .c  om*/
public void deleteRecursive(final String path) throws IOException {
    LOG.debug("DELETE {} [recursive]", path);

    /* We repetitively run the find() -> delete() stream operation and check
     * the diretory targeted for deletion by attempting to delete it this
     * deals with unpredictable directory contents changes that are a result
     * or concurrent modifications to the contents of the directory path to
     * be deleted. */
    int loops = 0;

    /* We record the number of request timeouts where we were unable to get
     * a HTTP connection from the pool in order to provide feedback to the
     * consumer of the SDK so that they can better tune their settings.*/
    final AtomicInteger responseTimeouts = new AtomicInteger(0);

    while (true) {
        loops++;

        /* Initially, we delete only the file objects returned from the
         * stream because we don't care what order they are in. */
        Stream<MantaObject> toDelete = find(path).map(obj -> {
            if (obj.isDirectory()) {
                return obj;
            }

            try {
                delete(obj.getPath());
            } catch (MantaClientHttpResponseException e) {
                if (!e.getServerCode().equals(MantaErrorCode.RESOURCE_NOT_FOUND_ERROR)) {
                    throw new UncheckedIOException(e);
                }
                /* This exception can be thrown if the parallelism value
                 * isn't tuned for the findForkJoinPool in relation to
                 * the amount of bandwidth available. Essentially, the
                 * processing thread is waiting too long for a new
                 * connection from the pool. If this is thrown too often,
                 * the maximum number of connections can be increased,
                 * the ConnectionRequestTimeout can be increased, or
                 * the fork join pool parallelism value can be
                 * decreased.
                 * Below we cope with this problem, by skipping the
                 * deletion of the object and letting it
                 * get deleted later in the loop when there is less
                 * contention on the connection pool.
                 */
            } catch (ConnectionPoolTimeoutException e) {
                responseTimeouts.incrementAndGet();
                LOG.debug("{} for deleting object {}", e.getMessage(), obj.getPath());
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }

            obj.getHttpHeaders().put("deleted", true);

            return obj;
        })
                /* We then sort the directories (and remaining files) with
                 * the deepest paths in the filesystem hierarchy first, so
                 * that we can delete subdirectories and files before
                 * the parent directories.*/
                .sorted(MantaObjectDepthComparator.INSTANCE);

        /* We go through every remaining directory and file attempt to
         * delete it even though that operation may not be immediately
         * successful. */
        toDelete.forEachOrdered(obj -> {
            for (int i = 0; i < config.getRetries(); i++) {
                try {
                    /* Don't bother deleting the file if it was marked as
                     * deleted from the map step. */
                    if (obj.getHttpHeaders().containsKey("deleted")) {
                        break;
                    }

                    /* If a file snuck in, we will delete it here. Typically
                     * this should be an empty directory. */
                    delete(obj.getPath());

                    LOG.trace("Finished deleting path {}", obj.getPath());

                    break;
                } catch (MantaClientHttpResponseException e) {
                    // If the directory has already gone, we are good to go
                    if (e.getServerCode().equals(MantaErrorCode.RESOURCE_NOT_FOUND_ERROR)) {
                        break;
                    }

                    /* If we get a directory not empty error we try again
                     * hoping that the next iteration will clean up any
                     * remaining files. */
                    if (e.getServerCode().equals(MantaErrorCode.DIRECTORY_NOT_EMPTY_ERROR)) {
                        continue;
                    }

                    throw new UncheckedIOException(e);
                } catch (ConnectionPoolTimeoutException e) {
                    responseTimeouts.incrementAndGet();
                    LOG.debug("{} for deleting object {}", e.getMessage(), obj.getPath());
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        });

        /* For each iteration of this loop, we attempt to delete the parent
         * path. If all subdirectories and files have been deleted, then
         * this operation will succeed.
         */
        try {
            delete(path);
            break;
        } catch (MantaClientHttpResponseException e) {
            // Somehow our current path has been deleted, so our work is done
            if (e.getServerCode().equals(MantaErrorCode.RESOURCE_NOT_FOUND_ERROR)) {
                break;
            } else if (e.getServerCode().equals(MantaErrorCode.DIRECTORY_NOT_EMPTY_ERROR)) {
                continue;
            }

            MantaIOException mioe = new MantaIOException("Unable to delete path", e);
            mioe.setContextValue("path", path);

            throw mioe;
        } catch (ConnectionPoolTimeoutException e) {
            responseTimeouts.incrementAndGet();
            LOG.debug("{} for deleting root object {}", e.getMessage(), path);
        }
    }

    LOG.debug("Finished deleting path {}. It took {} loops to delete recursively", path, loops);

    if (responseTimeouts.get() > 0) {
        LOG.info("Request timeouts were hit [%d] times when attempting to delete "
                + "recursively. You may want to adjust the Manta SDK request "
                + "timeout config setting, the Manta SDK maximum connections "
                + "setting, or the Java system property "
                + "[java.util.concurrent.ForkJoinPool.common.parallelism].");
    }
}

From source file:org.haiku.haikudepotserver.pkg.job.PkgScreenshotImportArchiveJobRunner.java

/**
 * <p>If this screenshot coming in from the archive does not exist persisted then load it in.</p>
 *//*w ww  .j a  va2s  .c  o  m*/

private void importScreenshotsFromArchiveAndReport(CSVWriter writer, ScreenshotImportMetadatas data,
        ArchiveInputStream archiveInputStream, ArchiveEntry archiveEntry, String pkgName, int order) {

    String row[] = { archiveEntry.getName(), // path
            pkgName, // pkg
            "", // action
            "", // message
            "", // code
    };

    if (data.isNotFound()) {
        row[CSV_COLUMN_ACTION] = Action.NOTFOUND.name();
    } else {
        FromArchiveScreenshotMetadata fromArchiveScreenshotMetadata = data.getFromArchiveScreenshots().stream()
                .filter((as) -> as.getLength() == archiveEntry.getSize()).filter((as) -> as.getOrder() == order)
                .findAny().orElseThrow(
                        () -> new IllegalStateException("unable to find the from-archive screenshot metadata"));

        Optional<ExistingScreenshotMetadata> existingScreenshotMetadata = data.getExistingScreenshots().stream()
                .filter((es) -> archiveEntry.getSize() == es.getLength())
                .filter((es) -> fromArchiveScreenshotMetadata.getDataHash().equals(es.getDataHash())).findAny();

        if (existingScreenshotMetadata.isPresent()) {
            row[CSV_COLUMN_ACTION] = Action.PRESENT.name();
            row[CSV_COLUMN_CODE] = existingScreenshotMetadata.get().getCode();
        } else {
            ObjectContext context = serverRuntime.newContext();

            try {
                PkgScreenshot screenshot = pkgScreenshotService.storePkgScreenshotImage(archiveInputStream,
                        context, Pkg.getByName(context, pkgName),
                        fromArchiveScreenshotMetadata.getDerivedOrder());

                row[CSV_COLUMN_CODE] = screenshot.getCode();
                row[CSV_COLUMN_ACTION] = Action.ADDED.name();
            } catch (IOException ioe) {
                throw new UncheckedIOException(ioe);
            } catch (BadPkgScreenshotException e) {
                row[CSV_COLUMN_ACTION] = Action.INVALID.name();
                row[CSV_COLUMN_MESSAGE] = e.getMessage();
            }

            context.commitChanges();
        }
    }

    writer.writeNext(row);
}

From source file:org.codice.ddf.configuration.migration.ExportMigrationEntryImplTest.java

@Test
public void testOutputStreamWithException() throws Exception {
    final IOException e = new IOException("testing");

    Mockito.when(context.getOutputStreamFor(Mockito.any())).thenThrow(new UncheckedIOException(e));

    thrown.expect(Matchers.sameInstance(e));

    entry.getOutputStream();/*w w w . ja v a2 s  .co m*/
}

From source file:org.gradle.api.plugins.buildcomparison.gradle.CompareGradleBuilds.java

private void writeReport(BuildComparisonResult result,
        DefaultBuildOutcomeComparisonResultRendererFactory<HtmlRenderContext> renderers) {
    File reportDir = getReportDir();
    if (reportDir.exists() && reportDir.list().length > 0) {
        GFileUtils.cleanDirectory(reportDir);
    }//from w  w  w. j a va  2s.c  o  m

    fileStore.moveFilestore(getFileStoreDir());

    OutputStream outputStream;
    Writer writer;

    try {
        outputStream = FileUtils.openOutputStream(getReportFile());
        writer = new OutputStreamWriter(outputStream, Charset.defaultCharset());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }

    try {
        createResultRenderer(renderers).render(result, writer);
    } finally {
        IOUtils.closeQuietly(writer);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:org.omegat.util.FileUtilTest.java

public void testBuildFileList() throws Exception {
    File tempDir = Files.createTempDirectory("omegat").toFile();
    assertTrue(tempDir.isDirectory());/* w  w  w .  j  ava 2 s . c om*/

    File subDir = new File(tempDir, "a");
    assertTrue(subDir.mkdirs());

    File aFile = new File(subDir, "foo");
    assertTrue(aFile.createNewFile());
    aFile = new File(subDir, "bar");
    assertTrue(aFile.createNewFile());

    List<File> list1 = FileUtil.buildFileList(tempDir, false);
    assertTrue(list1.isEmpty());

    List<File> list2 = FileUtil.buildFileList(tempDir, true);
    assertEquals(2, list2.size());

    Collections.sort(list2);
    assertTrue(list2.get(0).getPath().endsWith("bar"));

    try {
        File lnk = new File(tempDir, "hoge");
        Files.createSymbolicLink(lnk.toPath(), subDir.toPath());
        List<File> list3 = FileUtil.buildFileList(lnk, true);
        List<File> list4 = FileUtil.buildFileList(subDir, true);
        assertEquals(list3.size(), list4.size());
        assertTrue(IntStream.range(0, list3.size()).allMatch(i -> {
            try {
                return list3.get(i).getCanonicalFile().equals(list4.get(i).getCanonicalFile());
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }));
    } catch (UnsupportedOperationException | IOException ex) {
        // Creating symbolic links appears to not be supported on this
        // system
    }

    try {
        Files.createSymbolicLink(new File(tempDir, "baz").toPath(), tempDir.toPath());
        FileUtil.buildFileList(tempDir, true);
        fail("Should die from file system loop");
    } catch (UnsupportedOperationException | IOException ex) {
        // Creating symbolic links appears to not be supported on this
        // system
    } catch (UncheckedIOException ex) {
        if (!(ex.getCause() instanceof FileSystemLoopException)) {
            throw ex;
        }
        // Creating symbolic links appears to not be supported on this
        // system
    }

    FileUtils.deleteDirectory(tempDir);
}