Example usage for org.apache.commons.io FileUtils openInputStream

List of usage examples for org.apache.commons.io FileUtils openInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils openInputStream.

Prototype

public static FileInputStream openInputStream(File file) throws IOException 

Source Link

Document

Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

Usage

From source file:org.sonar.updatecenter.common.UpdateCenterDeserializer.java

public static UpdateCenter fromProperties(File file) throws IOException {
    FileInputStream in = FileUtils.openInputStream(file);
    try {/*from   w  ww . j  a  v a 2 s . c om*/
        Properties props = new Properties();
        props.load(in);
        UpdateCenter center = fromProperties(props);
        center.setDate(new Date(file.lastModified()));
        return center;

    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.sonarqube.tests.performance.AbstractPerfTest.java

protected Properties readProfiling(File baseDir, String moduleKey) throws IOException {
    File profilingFile = new File(baseDir, ".sonar/profiling/" + moduleKey + "-profiler.properties");
    Properties props = new Properties();
    FileInputStream in = FileUtils.openInputStream(profilingFile);
    try {/*from w  w w  .  j  a  va  2s  .c  o m*/
        props.load(in);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return props;
}

From source file:org.sonatype.nexus.plugins.webhook.WebHookPlugin.java

/**
 * Reload the plugin's configuration, based on the content of the configuration's file (see
 * {@link #getConfigurationFile()}).//from  ww  w. ja  v  a 2  s  .c  o m
 * 
 * @throws IOException in case of error when reading the configuration's file content
 * @throws IllegalArgumentException if the configuration's file content contains a malformed Unicode escape
 *             sequence.
 */
public void reloadConfiguration() throws IOException, IllegalArgumentException {
    configuration.clear();

    InputStream stream = null;
    try {
        stream = FileUtils.openInputStream(getConfigurationFile());
        configuration.load(stream);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:org.sourcepit.common.manifest.util.ManifestUtils.java

public static Manifest readJarManifest(File jarFile) throws IOException {
    final URI manifestUri = URI
            .createURI("jar:" + URI.createFileURI(jarFile.getAbsolutePath()) + "!/" + JarFile.MANIFEST_NAME);

    final ManifestResource resource = new GenericManifestResourceImpl(manifestUri);

    ZipInputStream jarIn = null;/*from   www.j  a v  a  2s .c o  m*/
    try {
        jarIn = new ZipInputStream(new BufferedInputStream(FileUtils.openInputStream(jarFile)));
        ZipEntry zipEntry = jarIn.getNextEntry();
        while (zipEntry != null) {
            if (JarFile.MANIFEST_NAME.equals(zipEntry.getName())) {
                resource.load(jarIn, null);
                break;
            }
            zipEntry = jarIn.getNextEntry();
        }
    } finally {
        IOUtils.closeQuietly(jarIn);
    }

    return (Manifest) resource.getContents().get(0);

}

From source file:org.tat.api.WebAppOptimizerMojo.java

public void compressJavaScript(String inputFilename, String outputFilename, Options o)
        throws IOException, EvaluatorException {
    Reader in = null;/* w w w  .j a  v  a 2 s.c om*/
    Writer out = null;
    try {
        in = new InputStreamReader(FileUtils.openInputStream(new File(inputFilename)), o.charset);

        JavaScriptCompressor compressor = new JavaScriptCompressor(in, new YuiCompressorErrorReporter());
        in.close();
        in = null;

        out = new OutputStreamWriter(FileUtils.openOutputStream(new File(outputFilename)), o.charset);
        compressor.compress(out, o.lineBreakPos, o.munge, o.verbose, o.preserveAllSemiColons,
                o.disableOptimizations);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:org.tat.api.WebAppOptimizerMojo.java

public void compressCss(String inputFilename, String outputFilename, Options o)
        throws IOException, EvaluatorException {
    Reader in = null;/*  ww  w .  j ava2  s .c om*/
    Writer out = null;
    try {
        in = new InputStreamReader(FileUtils.openInputStream(new File(inputFilename)), o.charset);

        CssCompressor compressor = new CssCompressor(in);
        in.close();
        in = null;

        out = new OutputStreamWriter(FileUtils.openOutputStream(new File(outputFilename)), o.charset);
        compressor.compress(out, -1);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:org.tonguetied.datatransfer.ExportServiceTest.java

/**
 * Test export to a java properties file
 * /*from  ww  w. ja  v a  2 s .c  om*/
 * @throws Exception
 */
@Test
public final void testExportToProperties() throws Exception {
    ExportParameters parameters = new ExportParameters();
    parameters.addLanguage(english);
    parameters.addLanguage(chinese);
    parameters.addBundle(bundle);
    parameters.addCountry(australia);
    parameters.addCountry(singapore);
    parameters.setFormatType(FormatType.properties);
    parameters.setTranslationState(TranslationState.VERIFIED);
    dataService.exportData(parameters);

    Properties expected_en_AU = new Properties();
    expected_en_AU.setProperty(translation1_1.getKeyword().getKeyword(), translation1_1.getValue());
    expected_en_AU.setProperty(translation2_1.getKeyword().getKeyword(), translation2_1.getValue());
    expected_en_AU.setProperty(translation3_3.getKeyword().getKeyword(), translation3_3.getValue());
    Properties expected_en_SG = new Properties();
    expected_en_SG.setProperty(translation1_2.getKeyword().getKeyword(), translation1_2.getValue());
    // expected_en_SG.setProperty(translation3_2.getKeyword().getKeyword(),
    // translation3_2.getValue());
    Properties expected_zh_SG = new Properties();
    expected_zh_SG.setProperty(translation3_1.getKeyword().getKeyword(), translation3_1.getValue());

    final File exportDir = dataService.getExportPath();
    File[] files = exportDir.listFiles(new FileExtensionFilter(".properties"));
    assertEquals(3, files.length);
    Properties actual;
    InputStream is = null;
    Properties expected = null;
    for (File file : files) {
        try {
            if (file.getName().contains("en_AU")) {
                expected = expected_en_AU;
            } else if (file.getName().contains("en_SG")) {
                expected = expected_en_SG;
            } else if (file.getName().contains("zh_SG")) {
                expected = expected_zh_SG;
            }
            if (expected != null) {
                is = FileUtils.openInputStream(file);
                is = new BufferedInputStream(is);
                actual = new Properties();
                actual.load(is);
                assertEquals(expected, actual);
            }
        } finally {
            expected = null;
            IOUtils.closeQuietly(is);
        }
    }
}

From source file:org.wisdom.api.bodies.RenderableFile.java

/**
 * Renders the file. If just returns an empty stream on the served file.
 *
 * @param context the HTTP context/*  w ww. j  a  v  a  2 s .c o  m*/
 * @param result  the result having built this renderable object
 * @return the input stream. Be aware that the stream may be blocking.
 * @throws RenderableException if the file cannot be read.
 */
@Override
public InputStream render(Context context, Result result) throws RenderableException {
    try {
        return FileUtils.openInputStream(file);
    } catch (IOException e) {
        throw new RenderableException("Cannot read file " + file.getAbsolutePath(), e);
    }
}

From source file:org.wisdom.api.bodies.RenderableTest.java

@Test
public void testRenderableStream() throws Exception {
    final File file = new File("target/test-classes/a_file.txt");
    RenderableStream body = new RenderableStream(FileUtils.openInputStream(file));
    // Unknown size and mime type
    assertThat(body.length()).isEqualTo(-1);
    assertThat(body.mimetype()).isNull();
    assertThat(body.mustBeChunked()).isTrue();
    assertThat(body.requireSerializer()).isFalse();
    byte[] bytes = IOUtils.toByteArray(body.render(null, null));
    assertThat(new String(bytes, Charsets.UTF_8)).isEqualTo("Used as test data.");
}

From source file:org.wisdom.maven.utils.Properties2HoconConverter.java

private static Properties readFileAsProperties(File props) throws IOException {
    Properties properties = new Properties();
    InputStream stream = null;/*  w  ww .j  av  a2s .  c  o m*/
    try {
        stream = FileUtils.openInputStream(props);
        properties.load(stream);
        return properties;
    } finally {
        IOUtils.closeQuietly(stream);
    }
}