Search in sources :

Example 46 with UncheckedIOException

use of java.io.UncheckedIOException in project ArachneCentralAPI by OHDSI.

the class AnalysisController method convertToMultipartFile.

private MultipartFile convertToMultipartFile(Resource resource) {
    try {
        String rootPath = ((ClassPathResource) resource).getPath();
        String name = convertToUnixPath(rootPath.substring(rootPath.indexOf(CC_SQLS_DIR) + CC_SQLS_DIR.length() + 1));
        return new MockMultipartFile(name, name, null, readResource(CC_SQLS_DIR + "/" + name));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 47 with UncheckedIOException

use of java.io.UncheckedIOException in project assertj-core by joel-costigliola.

the class Files method assertSameContentAs.

/**
 * Asserts that the given files have same content. Adapted from <a
 * href="http://junit-addons.sourceforge.net/junitx/framework/FileAssert.html" target="_blank">FileAssert</a> (from <a
 * href="http://sourceforge.net/projects/junit-addons">JUnit-addons</a>.)
 * @param info contains information about the assertion.
 * @param actual the "actual" file.
 * @param actualCharset {@link Charset} of the "actual" file.
 * @param expected the "expected" file.
 * @param expectedCharset {@link Charset} of the "actual" file.
 * @throws NullPointerException if {@code expected} is {@code null}.
 * @throws IllegalArgumentException if {@code expected} is not an existing file.
 * @throws AssertionError if {@code actual} is {@code null}.
 * @throws AssertionError if {@code actual} is not an existing file.
 * @throws UncheckedIOException if an I/O error occurs.
 * @throws AssertionError if the given files do not have same content.
 */
public void assertSameContentAs(AssertionInfo info, File actual, Charset actualCharset, File expected, Charset expectedCharset) {
    verifyIsFile(expected);
    assertIsFile(info, actual);
    try {
        List<Delta<String>> diffs = diff.diff(actual, actualCharset, expected, expectedCharset);
        if (diffs.isEmpty())
            return;
        throw failures.failure(info, shouldHaveSameContent(actual, expected, diffs));
    } catch (MalformedInputException e) {
        try {
            // MalformedInputException is thrown by readLine() called in diff
            // compute a binary diff, if there is a binary diff, it it shows the offset of the malformed input
            BinaryDiffResult binaryDiffResult = binaryDiff.diff(actual, readAllBytes(expected.toPath()));
            if (binaryDiffResult.hasNoDiff()) {
                // fall back to the UncheckedIOException : not throwing an error is wrong as there was one in the first place.
                throw e;
            }
            throw failures.failure(info, shouldHaveBinaryContent(actual, binaryDiffResult));
        } catch (IOException ioe) {
            throw new UncheckedIOException(format(UNABLE_TO_COMPARE_FILE_CONTENTS, actual, expected), ioe);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(format(UNABLE_TO_COMPARE_FILE_CONTENTS, actual, expected), e);
    }
}
Also used : Delta(org.assertj.core.util.diff.Delta) MalformedInputException(java.nio.charset.MalformedInputException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 48 with UncheckedIOException

use of java.io.UncheckedIOException in project junit5 by junit-team.

the class JUnitPlatformProvider method getConfigurationParameters.

private Map<String, String> getConfigurationParameters() {
    String content = parameters.getProviderProperties().get(CONFIGURATION_PARAMETERS);
    if (content == null) {
        return emptyMap();
    }
    try (StringReader reader = new StringReader(content)) {
        Map<String, String> result = new HashMap<>();
        Properties props = new Properties();
        props.load(reader);
        props.stringPropertyNames().forEach(key -> result.put(key, props.getProperty(key)));
        return result;
    } catch (IOException ex) {
        throw new UncheckedIOException("Error reading " + CONFIGURATION_PARAMETERS, ex);
    }
}
Also used : HashMap(java.util.HashMap) StringReader(java.io.StringReader) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Properties(java.util.Properties)

Example 49 with UncheckedIOException

use of java.io.UncheckedIOException in project spf4j by zolyfarkas.

the class MessageFormat method formatToCharacterIterator.

/**
 * Formats an array of objects and inserts them into the <code>MessageFormat</code>'s pattern, producing an
 * <code>AttributedCharacterIterator</code>. You can use the returned <code>AttributedCharacterIterator</code> to
 * build the resulting String, as well as to determine information about the resulting String.
 * <p>
 * The text of the returned <code>AttributedCharacterIterator</code> is the same that would be returned by
 * <blockquote>
 * <code>{@link #format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition) format}(arguments, new
 * StringBuffer(), null).toString()</code>
 * </blockquote>
 * <p>
 * In addition, the <code>AttributedCharacterIterator</code> contains at least attributes indicating where text was
 * generated from an argument in the <code>arguments</code> array. The keys of these attributes are of type
 * <code>MessageFormat.Field</code>, their values are <code>Integer</code> objects indicating the index in the
 * <code>arguments</code> array of the argument from which the text was generated.
 * <p>
 * The attributes/value from the underlying <code>Format</code> instances that <code>MessageFormat</code> uses will
 * also be placed in the resulting <code>AttributedCharacterIterator</code>. This allows you to not only find where an
 * argument is placed in the resulting String, but also which fields it contains in turn.
 *
 * @param arguments an array of objects to be formatted and substituted.
 * @return AttributedCharacterIterator describing the formatted value.
 * @exception NullPointerException if <code>arguments</code> is null.
 * @exception IllegalArgumentException if an argument in the <code>arguments</code> array is not of the type expected
 * by the format element(s) that use it.
 * @since 1.4
 */
public AttributedCharacterIterator formatToCharacterIterator(@Nonnull Object arguments) {
    StringBuilder result = new StringBuilder();
    ArrayList<AttributedCharacterIterator> iterators = new ArrayList<>();
    try {
        subformat((Object[]) arguments, result, null, iterators);
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
    if (iterators.isEmpty()) {
        return createAttributedCharacterIterator("");
    }
    return new AttributedString(iterators.toArray(new AttributedCharacterIterator[iterators.size()])).getIterator();
}
Also used : ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 50 with UncheckedIOException

use of java.io.UncheckedIOException in project spf4j by zolyfarkas.

the class Strings method decode.

@SuppressFBWarnings("SUA_SUSPICIOUS_UNINITIALIZED_ARRAY")
public static String decode(final CharsetDecoder cd, final byte[] ba, final int off, final int len) {
    if (len == 0) {
        return "";
    }
    int en = (int) (len * (double) cd.maxCharsPerByte());
    char[] ca = TLScratch.getCharsTmp(en);
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder) cd).decode(ba, off, len, ca);
        return new String(ca, 0, clen);
    }
    cd.reset();
    ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
    CharBuffer cb = CharBuffer.wrap(ca);
    try {
        CoderResult cr = cd.decode(bb, cb, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = cd.flush(cb);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        throw new UncheckedIOException(x);
    }
    return new String(ca, 0, cb.position());
}
Also used : ArrayDecoder(sun.nio.cs.ArrayDecoder) CharBuffer(java.nio.CharBuffer) UncheckedIOException(java.io.UncheckedIOException) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

UncheckedIOException (java.io.UncheckedIOException)826 IOException (java.io.IOException)786 File (java.io.File)109 Path (java.nio.file.Path)106 ArrayList (java.util.ArrayList)70 InputStream (java.io.InputStream)58 Map (java.util.Map)58 List (java.util.List)55 HashMap (java.util.HashMap)44 Test (org.junit.Test)38 Files (java.nio.file.Files)37 Collectors (java.util.stream.Collectors)37 Stream (java.util.stream.Stream)31 URL (java.net.URL)29 StringWriter (java.io.StringWriter)27 CursorContext (org.neo4j.io.pagecache.context.CursorContext)25 FileInputStream (java.io.FileInputStream)23 HashSet (java.util.HashSet)23 PageCursor (org.neo4j.io.pagecache.PageCursor)22 Optional (java.util.Optional)21