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, String encoding) throws IOException 

Source Link

Document

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

Usage

From source file:de.shadowhunt.subversion.internal.ResourcePropertyUtilsTest.java

@Test
public void testEscapedInputStream_emptyTag() throws Exception {
    final String xml = "<C:foo:bar/>";
    final String expected = "<C:foo" + MARKER + "bar/>";
    final InputStream stream = IOUtils.toInputStream(xml, ResourcePropertyUtils.UTF8);

    final InputStream escapedStream = ResourcePropertyUtils.escapedInputStream(stream);
    final String escapedXml = IOUtils.toString(escapedStream, ResourcePropertyUtils.UTF8);

    Assert.assertEquals(expected, escapedXml);
}

From source file:com.evolveum.midpoint.prism.ParserStringSource.java

@NotNull
@Override
public InputStream getInputStream() throws IOException {
    return IOUtils.toInputStream(data, "utf-8");
}

From source file:com.ppcxy.cyfm.showcase.demos.utilities.io.IODemo.java

@Test
public void workWithFileContent() {
    File file = new File("woop.txt");
    File destFile = new File("bar.txt");
    try {/*from w w  w  .j  a  v  a  2s .  c o  m*/
        // text -> file, Collection<String>, byte[] ->file
        FileUtils.writeStringToFile(file, "Hey sailor!\nHaha\n", "UTF-8");

        // inputstream -> file
        InputStream source = IOUtils.toInputStream("Hej", "UTF-8");
        FileUtils.copyInputStreamToFile(source, file);

        // ///////////////////////////
        // file -> outputstream
        System.out.println("copy File to outputstream:");
        FileUtils.copyFile(file, System.out);

        // file -> file
        FileUtils.copyFile(file, destFile);

        // file -> string
        System.out.println("File to String:");
        System.out.println(FileUtils.readFileToString(file, "UTF-8"));

        // file -> list<string>
        System.out.println("File to List<String>:");
        List<String> lines = FileUtils.readLines(file, "UTF-8");
        for (String string : lines) {
            System.out.println(string);
        }
    } catch (IOException e) {
        Exceptions.unchecked(e);
    }
}

From source file:com.github.robozonky.cli.GoogleCredentialsFeatureTest.java

@BeforeEach
void replaceSystemIn() {
    System.setIn(IOUtils.toInputStream("", Defaults.CHARSET));
}

From source file:br.gov.lexml.parser.documentoarticulado.LexMLUtil.java

public static Document toDocument(String xml) {
    try {/*  w ww  .  j  a va  2 s. co m*/
        InputStream input = IOUtils.toInputStream(xml, ENCODING);
        try {
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
            doc.getDocumentElement().setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink",
                    "http://www.w3.org/1999/xlink");
            return doc;
        } finally {
            input.close();
        }
    } catch (Throwable e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.htmlhifive.tools.jslint.engine.option.xml.JaxbUtil.java

/**
 * JsCheckOptionsxml????.//w  w  w  .j  a  v a2  s  .c  o m
 * 
 * 
 * @param checkOptions .
 * @param output .
 */
public static void saveJsCheckOption(JsCheckOption checkOptions, IFile output) {
    try {
        Marshaller marshall = jc.createMarshaller();
        marshall.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.valueOf(true));
        StringWriter writer = new StringWriter();
        marshall.marshal(checkOptions, writer);
        if (output.exists()) {
            output.setContents(IOUtils.toInputStream(writer.toString(), "UTF-8"), IResource.FORCE, null);
        } else {
            output.create(IOUtils.toInputStream(writer.toString(), "UTF-8"), true, null);
        }
        output.refreshLocal(IResource.DEPTH_ONE, null);
    } catch (JAXBException e) {
        logger.put(Messages.EM0006, e, output.getName());
    } catch (CoreException e) {
        logger.put(Messages.EM0100, e);
    } catch (IOException e) {
        logger.put(Messages.EM0006, e, output.getName());
    }
}

From source file:com.hypirion.io.ClosingPipeTest.java

/**
 * Tests that a ClosingPipe closes an OutputStream after the InputStream is
 * properly consumed./*  www.jav a  2s  .c om*/
 */
@Test(timeout = 1000)
public void testBasicStreamClosingCapabilities() throws Exception {
    String input = RandomStringUtils.random(4023);
    InputStream in = IOUtils.toInputStream(input, "UTF-8");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CloseCheckingOutputStream wrapper = new CloseCheckingOutputStream(out);
    Pipe p = new ClosingPipe(in, wrapper);
    p.start();
    p.join();
    in.close();
    String output = out.toString("UTF-8");
    assertEquals(input, output);
    assertTrue(String.format("Failed to close for %s", input), wrapper.isClosed);
}

From source file:com.dalelotts.rpn.ScannerTest.java

@Test(expected = NoSuchElementException.class) // Then
public void nextThrowsExceptionForEmptyString() throws Exception {
    // Given//from w  w w.j  av  a2  s.c o  m
    try (final InputStream inputStream = IOUtils.toInputStream("", "UTF-8")) {
        scanner = new Scanner(inputStream);

        // When
        scanner.next(); // Expect this to throw an exception

        // Then
        // Will not reach this point.
        // (expected = NoSuchElementException.class) above
        // tells JUnit that this test should throw a NoSuchElementException.
    }
}

From source file:com.peadargrant.filecheck.core.checks.FakeTest.java

@Before
public void setUp() throws IOException {
    instance = new Fake();
    input = IOUtils.toInputStream(SOURCE_TEXT, "UTF-8");
    result = Mockito.mock(CheckResult.class);
}

From source file:com.hypirion.io.PipeTest.java

/**
 * Tests that an InputStream with random ascii characters will be completely
 * piped through the pipe.//from   w ww. jav a  2 s.  c  om
 */
@Test(timeout = 1000)
public void testBasicStreamCapabilities() throws Exception {
    String input = RandomStringUtils.random(3708);
    InputStream in = IOUtils.toInputStream(input, "UTF-8");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Pipe p = new Pipe(in, out);
    p.start();
    p.join();
    in.close();
    String output = out.toString("UTF-8");
    out.close();
    assertEquals(input, output);
}