Example usage for org.apache.commons.io IOUtils toInputStream

List of usage examples for org.apache.commons.io IOUtils toInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils toInputStream.

Prototype

public static InputStream toInputStream(String input) 

Source Link

Document

Convert the specified string to an input stream, encoded as bytes using the default character encoding of the platform.

Usage

From source file:com.github.amihalik.sesame.debugging.SerializingDataException.java

public static void main(String[] args) throws Exception {
    // TurtleParser p = new TurtleParser();
    // p.setRDFHandler(new NTriplesWriter(System.out));
    // p.parse(IOUtils.toInputStream(TTL), "");

    SailRepository s = new SailRepository(new MemoryStore());
    s.initialize();/* w ww . j a  v a 2  s.  c  om*/
    SailRepositoryConnection conn = s.getConnection();
    conn.add(IOUtils.toInputStream(TTL), "", RDFFormat.TURTLE);
    System.out.println("Repo size :: " + conn.size());

    System.out.println(conn.getStatements(null, null, null, false).next());
}

From source file:edu.toronto.cs.xcurator.cli.CLIRunner.java

public static void main(String[] args) {
    Options options = setupOptions();//from   w  w  w  .  j a v a2s  . co m
    CommandLineParser parser = new BasicParser();
    try {
        CommandLine line = parser.parse(options, args);
        if (line.hasOption('t')) {
            fileType = line.getOptionValue('t');
        } else {
            fileType = XML;
        }
        if (line.hasOption('o')) {
            tdbDirectory = line.getOptionValue('o');
            File d = new File(tdbDirectory);
            if (!d.exists() || !d.isDirectory()) {
                throw new Exception("TDB directory does not exist, please create.");
            }
        }
        if (line.hasOption('h')) {
            domain = line.getOptionValue('h');
            try {
                URL url = new URL(domain);
            } catch (MalformedURLException ex) {
                throw new Exception("The domain name is ill-formed");
            }
        } else {
            printHelpAndExit(options);
        }
        if (line.hasOption('m')) {
            serializeMapping = true;
            mappingFilename = line.getOptionValue('m');
        }
        if (line.hasOption('d')) {
            dirLocation = line.getOptionValue('d');
            inputStreams = new ArrayList<>();
            final List<String> files = Util.getFiles(dirLocation);
            for (String inputfile : files) {
                File f = new File(inputfile);
                if (f.isFile() && f.exists()) {
                    System.out.println("Adding document to mapping discoverer: " + inputfile);
                    inputStreams.add(new FileInputStream(f));
                } // If it is a URL download link for the document from SEC
                else if (inputfile.startsWith("http") && inputfile.contains("://")) {
                    // Download
                    System.out.println("Adding remote document to mapping discoverer: " + inputfile);
                    try {
                        URL url = new URL(inputfile);
                        InputStream remoteDocumentStream = url.openStream();
                        inputStreams.add(remoteDocumentStream);
                    } catch (MalformedURLException ex) {
                        throw new Exception("The document URL is ill-formed: " + inputfile);
                    } catch (IOException ex) {
                        throw new Exception("Error in downloading remote document: " + inputfile);
                    }
                } else {
                    throw new Exception("Cannot open XBRL document: " + f.getName());
                }
            }
        }

        if (line.hasOption('f')) {
            fileLocation = line.getOptionValue('f');
            inputStreams = new ArrayList<>();
            File f = new File(fileLocation);
            if (f.isFile() && f.exists()) {
                System.out.println("Adding document to mapping discoverer: " + fileLocation);
                inputStreams.add(new FileInputStream(f));
            } // If it is a URL download link for the document from SEC
            else if (fileLocation.startsWith("http") && fileLocation.contains("://")) {
                // Download
                System.out.println("Adding remote document to mapping discoverer: " + fileLocation);
                try {
                    URL url = new URL(fileLocation);
                    InputStream remoteDocumentStream = url.openStream();
                    inputStreams.add(remoteDocumentStream);
                } catch (MalformedURLException ex) {
                    throw new Exception("The document URL is ill-formed: " + fileLocation);
                } catch (IOException ex) {
                    throw new Exception("Error in downloading remote document: " + fileLocation);
                }
            } else {

                throw new Exception("Cannot open XBRL document: " + f.getName());
            }

        }

        setupDocumentBuilder();
        RdfFactory rdfFactory = new RdfFactory(new RunConfig(domain));
        List<Document> documents = new ArrayList<>();
        for (InputStream inputStream : inputStreams) {
            Document dataDocument = null;
            if (fileType.equals(JSON)) {
                String json = IOUtils.toString(inputStream);
                final String xml = Util.json2xml(json);
                final InputStream xmlInputStream = IOUtils.toInputStream(xml);
                dataDocument = createDocument(xmlInputStream);
            } else {
                dataDocument = createDocument(inputStream);
            }
            documents.add(dataDocument);
        }
        if (serializeMapping) {
            System.out.println("Mapping file will be saved to: " + new File(mappingFilename).getAbsolutePath());
            rdfFactory.createRdfs(documents, tdbDirectory, mappingFilename);
        } else {
            rdfFactory.createRdfs(documents, tdbDirectory);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        System.err.println("Unexpected exception: " + ex.getMessage());
        System.exit(1);
    }
}

From source file:com.mayalogy.mayu.io.FileAppender.java

public static void appendToFile(final String in, final File f) throws IOException {
    InputStream stream = null;//w ww  .j a v  a 2  s.c  om
    try {
        stream = IOUtils.toInputStream(in);
        appendToFile(stream, f);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:fi.solita.datatree.xml.XmlVulnerabilitiesTest.java

@Test
public void safe_XML_passes_the_check() {
    String xml = "<?xml version=\"1.0\"?>" + "<foo></foo>";

    XmlVulnerabilities.check(IOUtils.toInputStream(xml));
}

From source file:com.elasticbox.jenkins.k8s.repositories.api.charts.factory.ManifestFactory.java

public static void addManifest(String yamlAsText, Chart.ChartBuilder chartBuilder) throws RepositoryException {

    final String manifestKind = findManifestKind(yamlAsText);

    final ManifestType type = ManifestType.findByType(manifestKind);

    KubernetesClient client = new DefaultKubernetesClient();

    final InputStream inputStream = IOUtils.toInputStream(yamlAsText);

    switch (type) {

    case POD://from   w  w  w.j  a  v  a  2s . c  om
        final Pod pod = client.pods().load(inputStream).get();
        chartBuilder.addPod(pod);
        return;

    case REPLICATION_CONTROLLER:
        final ReplicationController replicationController = client.replicationControllers().load(inputStream)
                .get();
        chartBuilder.addReplicationController(replicationController);
        return;

    case SERVICE:
        final Service service = client.services().load(inputStream).get();
        chartBuilder.addService(service);
        return;

    default:

    }

    throw new RepositoryException("Manifest kind: " + manifestKind + " is not supported");
}

From source file:io.dfox.junit.http.example.ExampleTest.java

@BeforeClass
public static void setUp() throws IOException {
    final File tempDir = new File("/tmp", NoteRepository.class.getName());
    repository = new NoteRepository(tempDir);
    repository.init();/*from w  ww. j  av a 2 s .co  m*/

    JsonNode notesFixture = getTestData("notes.json");
    JsonNode noteFixture = notesFixture.path("load");
    String name = noteFixture.path("name").asText();
    String contents = noteFixture.path("contents").asText();

    repository.saveNote(name, IOUtils.toInputStream(contents));
}

From source file:fi.solita.datatree.xml.XmlVulnerabilitiesTest.java

@Test
public void notices_XXE_attacks() {
    // See https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Processing
    String xml = "<?xml version=\"1.0\"?>" + "<!DOCTYPE foo [" + "  <!ELEMENT foo ANY >"
            + "  <!ENTITY xxe SYSTEM \"file:///secret.txt\" >" + "]>" + "<foo>&xxe;</foo>";

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("DOCTYPE is disallowed");
    XmlVulnerabilities.check(IOUtils.toInputStream(xml));
}

From source file:com.excilys.spring.mom.parser.MOMResponseBinaryParser.java

@Override
public Object[] parse(byte[] data) throws MOMResponseParsingException {
    return new Object[] { IOUtils.toInputStream(new String(data)) };
}

From source file:com.collective.celos.ci.testing.fixtures.compare.PlainFileComparerTest.java

@Test
public void testComparesOk() throws Exception {

    InputStream inputStream1 = IOUtils.toInputStream("stream");
    InputStream inputStream2 = IOUtils.toInputStream("stream");

    FixFile fixFile1 = mock(FixFile.class);
    PlainFileComparer comparer = new PlainFileComparer(inputStream2, fixFile1);

    doReturn(inputStream1).when(fixFile1).getContent();
    FixObjectCompareResult compareResult = comparer.check(null);
    Assert.assertEquals(compareResult, FixObjectCompareResult.SUCCESS);
}

From source file:com.collective.celos.ci.testing.fixtures.create.FixFileFromStringCreator.java

public FixFile create(TestRun testRun) throws Exception {
    return new FixFile(IOUtils.toInputStream(content));
}