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:nl.knaw.huygens.alexandria.lmnl.importer.LMNLImporterInMemoryTest.java

@Test
public void testAcrosticFileThrowsSyntaxError() throws IOException {
    String pathname = "data/lmnl/acrostic-syntax-error.lmnl";
    InputStream input = FileUtils.openInputStream(new File(pathname));
    try {//from   ww w .  j  a  va  2 s . com
        Document actual = new LMNLImporterInMemory().importLMNL(input);
        fail("no LMNLSyntaxError thrown");
    } catch (LMNLSyntaxError e) {
        assertThat(e.getMessage())
                .contains("Unclosed LMNL range(s): [H}, [name}, [T}, [name}, [lizabeth}, [name=a}");
    }
}

From source file:nl.knaw.huygens.alexandria.lmnl.importer.LMNLImporterTest.java

@Test
public void testLMNL1kings12() throws IOException, LMNLSyntaxError {
    String pathname = "data/lmnl/1kings12.lmnl";
    InputStream input = FileUtils.openInputStream(new File(pathname));
    store.runInTransaction(() -> {//w ww .  ja v a2s.  c  o  m
        LMNLImporter importer = new LMNLImporter(store);
        TAGDocument actual = importer.importLMNL(input);

        LOG.info("document={}", actual);

        logTAGML(actual);

        List<TAGMarkup> actualMarkupList = actual.getMarkupStream().collect(toList());

        TAGMarkup excerpt = actualMarkupList.get(0);
        assertThat(excerpt.getTag()).isEqualTo("excerpt");

        List<AnnotationInfo> annotations = excerpt.getAnnotationStream().collect(toList());
        assertThat(annotations).hasSize(1); // just the soutce annotation;

        AnnotationInfo source = simpleAnnotation("source");
        AnnotationInfo book = simpleAnnotation("book", "1 Kings");
        //      source.addAnnotation(book);
        AnnotationInfo chapter = simpleAnnotation("chapter", "12");
        //      source.addAnnotation(chapter);
        String actualSourceTAGML = tagmlExporter.toTAGML(annotations.get(0)).toString();
        String expectedSourceTAGML = tagmlExporter.toTAGML(source).toString();
        assertThat(actualSourceTAGML).isEqualTo(expectedSourceTAGML);

        TAGMarkup q1 = actualMarkupList.get(2);
        assertThat(q1.getTag()).isEqualTo("q"); // first q
        assertThat(q1.getTextNodeStream()).hasSize(2); // has 2 textnodes

        TAGMarkup q2 = actualMarkupList.get(3);
        assertThat(q2.getTag()).isEqualTo("q"); // second q, nested in first
        assertThat(q2.getTextNodeStream()).hasSize(1); // has 1 textnode

        // compareTAGML(pathname, actual);
        logKdTree(actual);

        List<IndexPoint> expectedIndexPoints = new ArrayList<>();
        expectedIndexPoints.add(new IndexPoint(0, 0));
        // assertThat(indexPoints).containsExactlyElementsOf(expectedIndexPoints);
    });
}

From source file:nl.knaw.huygens.alexandria.lmnl.importer.LMNLImporterTest.java

@Ignore
@Test/*from ww  w  .  j  a  v a  2s . c o m*/
public void testLMNLOzymandias() throws IOException, LMNLSyntaxError {
    String pathname = "data/lmnl/ozymandias-voices-wap.lmnl";
    InputStream input = FileUtils.openInputStream(new File(pathname));
    store.runInTransaction(() -> {
        TAGDocument actual = new LMNLImporter(store).importLMNL(input);
        LOG.info("document={}", actual);
        logTAGML(actual);
        assertThat(actual.hasTextNodes()).isTrue();
        String tagml = tagmlExporter.asTAGML(actual);
        assertThat(tagml).startsWith("[sonneteer [id}ozymandias{] [encoding [resp}ebeshero{] [resp}wap{]]}"); // annotations from sonneteer endtag moved to start tag
        assertThat(tagml).contains("[meta [author}Percy Bysshe Shelley{] [title}Ozymandias{]]"); // anonymous markup
        // compareTAGML(pathname, actual);

        logKdTree(actual);

        List<IndexPoint> expectedIndexPoints = new ArrayList<>();
        expectedIndexPoints.add(new IndexPoint(0, 0));
        // assertThat(indexPoints).containsExactlyElementsOf(expectedIndexPoints);
    });
}

From source file:nl.knaw.huygens.alexandria.lmnl.importer.LMNLImporterTest.java

@Test
public void testAcrosticFileThrowsSyntaxError() throws IOException {
    String pathname = "data/lmnl/acrostic-syntax-error.lmnl";
    InputStream input = FileUtils.openInputStream(new File(pathname));
    store.runInTransaction(() -> {/*from ww  w.j  av  a  2s . c  o m*/
        try {
            new LMNLImporter(store).importLMNL(input);
            fail("no LMNLSyntaxError thrown");
        } catch (LMNLSyntaxError e) {
            assertThat(e.getMessage())
                    .contains("Unclosed LMNL range(s): [H}, [name}, [T}, [name}, [lizabeth}, [name=a}");
        }
    });
}

From source file:nl.knaw.huygens.alexandria.texmecs.importer.ImportDataTexMECSInMemoryTest.java

private InputStream getInputStream(String basename) throws IOException {
    return FileUtils.openInputStream(new File("data/texmecs/" + basename + ".texmecs"));
}

From source file:nl.knaw.huygens.alexandria.text.FileSystemTextService.java

@Override
public Optional<InputStream> getAsStream(UUID resourceUUID) {
    File textFile = textFile(resourceUUID);
    if (!textFile.isFile()) {
        return Optional.empty();
    }/*from  w  ww.  j  a v  a  2s. co  m*/
    try {
        return Optional.of(FileUtils.openInputStream(textFile));
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:nl.sidn.pcap.load.LoaderThread.java

public void createReader(String file) {
    LOGGER.info("Start loading queue from file:" + file);
    fileCount++;//from  w w w.  j a va 2 s  .  com
    try {
        File f = FileUtils.getFile(file);
        LOGGER.info("Load data for server: " + current_server.getFullname());

        FileInputStream fis = FileUtils.openInputStream(f);
        int bufSize = Settings.getInstance().getIntSetting(Settings.BUFFER_PCAP_READER,
                DEFAULT_PCAP_READER_BUFFER_SIZE);
        //sanity check
        if (bufSize <= 512) {
            //use default
            bufSize = DEFAULT_PCAP_READER_BUFFER_SIZE;
        }
        GZIPInputStream gzip = new GZIPInputStream(fis, bufSize);
        dis = new DataInputStream(gzip);
        pcapReader.init(dis);
    } catch (IOException e) {
        LOGGER.error("Error opening pcap file: " + file, e);
        throw new RuntimeException("Error opening pcap file: " + file);
    }
}

From source file:no.digipost.api.client.eksempelkode.FallbackTilPrintEksempel.java

private static InputStream lesInnSertifikat() {
    try {//from   w  w  w.j  av a2  s. c om
        // Leser inn sertifikatet selv med Apache Commons FileUtils.
        return FileUtils.openInputStream(new File("/path/til/sertifikatfil.p12"));
    } catch (IOException e) {
        // Hndter at sertifikatet ikke kunne leses!
        throw new RuntimeException("Kunne ikke lese sertifikatfil");
    }
}

From source file:org.adf.emg.sonar.ojaudit.XmlMetricsDecorator.java

@Override
public void decorate(Resource resource, DecoratorContext context) {
    if (!Qualifiers.isFile(resource)) {
        return;//  w w  w.  ja  v a2s .  co m
    }
    ProjectFileSystem fileSystem = context.getProject().getFileSystem();
    File file = lookup(resource, fileSystem);

    try {
        if (readFirstByte(file) != '<') {
            return;
        }
    } catch (IOException e) {
        throw new SonarException(e);
    }

    int numCommentLines;
    CountCommentParser commentCounter = new CountCommentParser();
    try {
        numCommentLines = commentCounter.countLinesOfComment(FileUtils.openInputStream(file));
        if (numCommentLines == -1) {
            return;
        }
    } catch (IOException e) {
        throw new SonarException(e);
    }

    LineIterator iterator = null;
    int numLines = 0;
    int numBlankLines = 0;
    try {
        Charset charset = fileSystem.getSourceCharset();
        iterator = charset == null ? FileUtils.lineIterator(file)
                : FileUtils.lineIterator(file, charset.name());
        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            numLines++;
            if (line.trim().isEmpty()) {
                numBlankLines++;
            }
        }
    } catch (IOException e) {
        LOG.warn("error reading " + file + " to collect metrics", e);
    } finally {
        LineIterator.closeQuietly(iterator);
    }

    context.saveMeasure(CoreMetrics.LINES, (double) numLines); // Lines
    context.saveMeasure(CoreMetrics.COMMENT_LINES, (double) numCommentLines); // Non Commenting Lines of Code
    context.saveMeasure(CoreMetrics.NCLOC, (double) numLines - numBlankLines - numCommentLines); // Comment Lines
}

From source file:org.adf.emg.sonar.ojaudit.XmlMetricsDecorator.java

private byte readFirstByte(File file) throws IOException {
    byte[] buffer = new byte[1];
    int len = IOUtils.read(FileUtils.openInputStream(file), buffer);
    return len == 1 ? buffer[0] : null;
}