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

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

Introduction

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

Prototype

public static File toFile(URL url) 

Source Link

Document

Convert from a URL to a File.

Usage

From source file:net.pms.dlna.DLNAMediaSubtitleTest.java

@Test
public void testIsEmbedded() throws Exception {
    File file_cp1251 = FileUtils.toFile(CLASS.getResource("../util/russian-cp1251.srt"));
    DLNAMediaSubtitle sub1 = new DLNAMediaSubtitle();
    sub1.setType(SUBRIP);/*from   w w  w  .j  a  v a  2  s .co m*/
    sub1.setExternalFile(file_cp1251);
    assertThat(sub1.isEmbedded()).isFalse();
    assertThat(sub1.isExternal()).isTrue();

    DLNAMediaSubtitle sub2 = new DLNAMediaSubtitle();
    sub2.setType(SUBRIP);
    assertThat(sub2.isEmbedded()).isTrue();
    assertThat(sub2.isExternal()).isFalse();
}

From source file:com.github.jrh3k5.flume.mojo.plugin.AbstractFlumePluginMojoITest.java

/**
 * Get a project's POM./*from  w ww  .  j a  v  a2 s  . co m*/
 * 
 * @param artifactId
 *            The artifact ID of the project whose POM is to be retrieved.
 * @return A {@link File} reference representing the desired POM file.
 * @throws FileNotFoundException
 *             If the given file cannot be found on the classpath.
 * @throws URISyntaxException
 *             If converting the {@link URL} representing the file on the classpath cannot be converted into a {@link URI}.
 */
protected File getPom(final String artifactId) throws FileNotFoundException, URISyntaxException {
    final URL resourceUrl = getClass().getResource("/flume-plugin-maven-plugin-test-projects/"
            + getClass().getSimpleName() + "/" + artifactId + "/pom.xml");
    if (resourceUrl == null)
        throw new FileNotFoundException("Project POM not found: " + artifactId);
    return FileUtils.toFile(resourceUrl);
}

From source file:net.sf.eclipsecs.core.config.CheckConfigurationWorkingCopy.java

/**
 * Stores the (edited) list of modules to the Checkstyle configuration file.
 *
 * @param modules//from w  ww  .j  a  v  a2  s  .  com
 *            the list of modules to store into the Checkstyle configuration file
 * @throws CheckstylePluginException
 *             error storing the Checkstyle configuration
 */
public void setModules(List<Module> modules) throws CheckstylePluginException {

    OutputStream out = null;
    ByteArrayOutputStream byteOut = null;
    try {

        // First write to a byte array outputstream
        // because otherwise in an error case the original
        // file would be destroyed
        byteOut = new ByteArrayOutputStream();

        ConfigurationWriter.write(byteOut, modules, this);

        // all went ok, write to the file
        File configFile = FileUtils.toFile(getResolvedConfigurationFileURL());
        out = new BufferedOutputStream(new FileOutputStream(configFile));
        out.write(byteOut.toByteArray());

        // refresh the files if within the workspace
        // Bug 1251194 - Resource out of sync after performing changes to
        // config
        IPath path = new Path(configFile.toString());
        IFile[] files = CheckstylePlugin.getWorkspace().getRoot().findFilesForLocation(path);
        for (int i = 0; i < files.length; i++) {
            try {
                files[i].refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
            } catch (CoreException e) {
                // NOOP - just ignore
            }
        }

        mHasConfigChanged = true;

        // throw away the cached Checkstyle configurations
        CheckConfigurationFactory.refresh();
    } catch (IOException e) {
        CheckstylePluginException.rethrow(e);
    } finally {
        IOUtils.closeQuietly(byteOut);
        IOUtils.closeQuietly(out);
    }
}

From source file:net.pms.util.FileUtilTest.java

@Test(expected = IllegalArgumentException.class)
public void testConvertFileFromUtf16ToUtf8_notUtf16InputFile() throws Exception {
    File file_cp1251 = FileUtils.toFile(CLASS.getResource("russian-cp1251.srt"));
    FileUtil.convertFileFromUtf16ToUtf8(file_cp1251, new File("output.srt"));
}

From source file:de.dfki.km.perspecting.obie.experiments.PhraseExperiment.java

@Test
public void testDifferentPrefixLengths() {

    final String template = "SELECT * WHERE {?s ?p ?o}";
    try {//w ww. jav a  2 s .c  om
        URL url = new URL("http://en.wikipedia.org/wiki/Kaiserslautern");
        Document document = pipeline.createDocument(FileUtils.toFile(url), url.toURI(), MediaType.HTML,
                template, Language.EN);
        for (int step = 0; pipeline.hasNext(step) && step <= 5; step = pipeline.execute(step, document)) {
            System.out.println(step);
        }
        final BufferedWriter bw = new BufferedWriter(
                new FileWriter($SCOOBIE_HOME + "results/response_time_prefix_hashing.csv"));

        for (int SIZE = 1; SIZE < 11; SIZE++) {

            TreeSet<String> hist = new TreeSet<String>();

            int count = 0;

            for (TokenSequence<String> i : document.getNounPhrases()) {
                String[] words = i.toString().split("[\\s]+");
                for (String word : words) {
                    count++;
                    if (word.length() >= SIZE)
                        hist.add(word.substring(0, SIZE));
                    else
                        hist.add(word);
                }
            }

            StringBuilder query = new StringBuilder();

            query.append("SELECT count(*) FROM index_literals, symbols WHERE "
                    + "( symbols.object = index_literals.index AND substr(index_literals.literal,1," + SIZE
                    + ") IN (");

            for (String p : hist) {
                query.append("(?) , ");
            }

            query.setLength(query.length() - 3);
            query.append("))");
            System.out.println(query.toString());

            Connection c = pool.getConnection();
            PreparedStatement stmtGetDatatypePropertyValues = c.prepareStatement(query.toString());
            int paramIndex = 0;
            for (String p : hist) {
                stmtGetDatatypePropertyValues.setString(++paramIndex, p);
            }
            long start = System.currentTimeMillis();
            ResultSet rs = stmtGetDatatypePropertyValues.executeQuery();
            long end = System.currentTimeMillis();
            while (rs.next()) {
                bw.append(SIZE + "\t" + (end - start) + "\t" + rs.getInt(1));
                bw.newLine();
            }
            stmtGetDatatypePropertyValues.close();
            c.close();

        }
        bw.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:net.pms.util.FileUtilTest.java

@Test
public void testIsFileReadable() {
    assertThat(FileUtil.isFileReadable(null)).isFalse();
    assertThat(FileUtil.isFileReadable(new File(""))).isFalse();
    assertThat(FileUtil.isFileReadable(new File(System.getProperty("user.dir")))).isFalse();
    assertThat(FileUtil.isFileReadable(new File("no such file"))).isFalse();

    File file = FileUtils.toFile(CLASS.getResource("english-utf8-with-bom.srt"));
    assertThat(FileUtil.isFileReadable(file)).isTrue();

    assertThat(file.getParentFile()).isNotNull();
    assertThat(FileUtil.isFileReadable(new File(file.getParentFile(), "no such file"))).isFalse();
}

From source file:net.pms.formats.v2.SubtitleUtilsTest.java

@Test(expected = NullPointerException.class)
public void testShiftSubtitlesTimingWithUtfConversion_withNullSubtitleType() throws IOException {
    final DLNAMediaSubtitle inputSubtitles = new DLNAMediaSubtitle() {
        @Override//ww  w  .  j a v a  2s  .  c  o  m
        public SubtitleType getType() {
            return null;
        }
    };
    inputSubtitles
            .setExternalFile(FileUtils.toFile(CLASS.getResource("../../util/russian-utf8-without-bom.srt")));
    SubtitleUtils.shiftSubtitlesTimingWithUtfConversion(inputSubtitles, 12);
}

From source file:net.pms.formats.v2.SubtitleUtilsTest.java

@Test(expected = IllegalArgumentException.class)
public void testShiftSubtitlesTimingWithUtfConversion_withInvalidSubtitleType() throws IOException {
    final DLNAMediaSubtitle inputSubtitles = new DLNAMediaSubtitle();
    inputSubtitles/*  www  .  ja  va  2  s . c om*/
            .setExternalFile(FileUtils.toFile(CLASS.getResource("../../util/russian-utf8-without-bom.srt")));
    SubtitleUtils.shiftSubtitlesTimingWithUtfConversion(inputSubtitles, 12);
}

From source file:com.github.jrh3k5.flume.mojo.plugin.AbstractFlumePluginMojoITest.java

/**
 * Get a project's project directory./* w  ww.  j  a v  a 2 s.com*/
 * 
 * @param projectName
 *            The name of the project for which the project is to be retrieved.
 * @return A {@link File} representing the given project's project directory.
 * @throws FileNotFoundException
 *             If the given project's project directory cannot be found.
 */
protected File getTestProjectDirectory(final String projectName) throws FileNotFoundException {
    final URL projectUrl = getClass().getResource(
            "/flume-plugin-maven-plugin-test-projects/" + getClass().getSimpleName() + "/" + projectName);
    if (projectUrl == null) {
        throw new FileNotFoundException("Project not found: " + projectName);
    }
    return FileUtils.toFile(projectUrl);
}

From source file:net.pms.util.FileUtilTest.java

@Test
public void testIsFileWritable() throws IOException {
    assertThat(FileUtil.isFileWritable(null)).isFalse();
    assertThat(FileUtil.isFileWritable(new File(""))).isFalse();
    assertThat(FileUtil.isFileWritable(new File(System.getProperty("user.dir")))).isFalse();
    String filename = String.format("pms_temp_writable_file_%d_1.tmp", System.currentTimeMillis());
    assertThat(FileUtil.isFileWritable(new File(filename))).isTrue();

    File file = FileUtils.toFile(CLASS.getResource("english-utf8-with-bom.srt"));
    assertThat(FileUtil.isFileReadable(file)).isTrue();

    assertThat(file.getParentFile()).isNotNull();

    filename = String.format("pms_temp_writable_file_%d_2.tmp", System.currentTimeMillis());
    File tempFile = null;// w ww.j  a  v  a  2  s  .  c o  m

    try {
        tempFile = new File(file.getParentFile(), filename);
        tempFile.createNewFile();
        assertThat(file.isFile()).isTrue();
        assertThat(FileUtil.isFileReadable(tempFile)).isTrue();
        assertThat(FileUtil.isFileWritable(tempFile)).isTrue();
    } finally {
        if (tempFile != null) {
            tempFile.delete();
        }
    }
}