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

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

Introduction

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

Prototype

public static void copy(Reader input, OutputStream output, String encoding) throws IOException 

Source Link

Document

Copy chars from a Reader to bytes on an OutputStream using the specified character encoding, and calling flush.

Usage

From source file:com.galenframework.actions.GalenActionConfig.java

@Override
public void execute() throws IOException {
    File file = new File("config");

    if (!file.exists()) {
        file.createNewFile();/*from w  ww  .  jav  a 2 s . co m*/
        FileOutputStream fos = new FileOutputStream(file);

        StringWriter writer = new StringWriter();
        IOUtils.copy(getClass().getResourceAsStream("/config-template.conf"), writer, "UTF-8");
        IOUtils.write(writer.toString(), fos, "UTF-8");
        fos.flush();
        fos.close();
        outStream.println("Created config file");
    } else {
        errStream.println("Config file already exists");
    }
}

From source file:com.seer.datacruncher.streams.DelimiterStreamTest.java

@Test
public void testDelimitedStream() {
    InputStream in = this.getClass().getClassLoader()
            .getResourceAsStream(stream_file_path + flat_file_delimited_file_name);
    StringWriter writer = new StringWriter();
    try {/*w w  w.  j ava 2s  .  c  o  m*/
        IOUtils.copy(in, writer, "UTF-8");
    } catch (IOException e) {
        assertTrue("IOException while delimited file reading", false);
    }
    String stream = writer.toString();
    DatastreamsInput datastreamsInput = new DatastreamsInput();
    String res = datastreamsInput.datastreamsInput(stream, (Long) schemaEntity.getIdSchema(), null, true);
    assertTrue("Delimited file validation failed", Boolean.parseBoolean(res));
}

From source file:com.seer.datacruncher.streams.FixedPosStreamTest.java

@Test
public void testFixedPosStream() {
    InputStream in = this.getClass().getClassLoader()
            .getResourceAsStream(stream_file_path + flat_file_fixedposition_file_name);
    StringWriter writer = new StringWriter();
    try {/*from ww  w.j  av  a 2s.c  o  m*/
        IOUtils.copy(in, writer, "UTF-8");
    } catch (IOException e) {
        assertTrue("IOException while fixedPosition file reading", false);
    }
    String stream = writer.toString();
    DatastreamsInput datastreamsInput = new DatastreamsInput();
    String res = datastreamsInput.datastreamsInput(stream, (Long) schemaEntity.getIdSchema(), null, true);
    assertTrue("FixedPosition file validation failed", Boolean.parseBoolean(res));
}

From source file:com.qdeve.oilprice.FileUtils.java

public String getFileContentAsString(String filename) throws IOException {
    Resource resource = appContext.getResource(filename);
    InputStream inputStream = resource.getInputStream();
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, "UTF-8");
    return writer.toString();
}

From source file:de.tudarmstadt.ukp.argumentation.data.roomfordebate.NYTimesArticleExtractor.java

public String readHTML(String url) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(new URL(url).openStream(), writer, "utf-8");

    return writer.toString();
}

From source file:com.seer.datacruncher.streams.XMLStreamTest.java

@Test
public void testXMLStream() {
    DatastreamsInput datastreamsInput = new DatastreamsInput();
    InputStream in = this.getClass().getClassLoader()
            .getResourceAsStream(stream_file_path + xml_stream_file_name);
    StringWriter writer = new StringWriter();
    try {/*w  w w.ja v a  2s.co m*/
        IOUtils.copy(in, writer, "UTF-8");
    } catch (IOException e) {
        assertTrue("IOException while xml parsing", false);
    }
    String dataStream = writer.toString();
    String res = datastreamsInput.datastreamsInput(dataStream, (Long) schemaEntity.getIdSchema(), null, true);
    assertTrue("XMLStream validation failed", Boolean.parseBoolean(res));
}

From source file:com.vigglet.oei.standardinspection.CopyStandardinspectionToServiceServlet.java

@Override
protected void preProcessRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(req.getInputStream(), writer, "UTF-8");
    String theString = writer.toString();
    User user = getUser(req);/*w w w  .  j  a v  a 2 s  .co  m*/

    Standardinspection model = JsonUtil.read(theString, Standardinspection.class);
    if (model != null) {
        model.setCompany(user.getCompany());
        model = PostStandardinspectionServlet.update(model);

        Service service = new Service();
        service.setCompany(model.getCompany());
        service.setDate(new Date().getTime());
        service.setDescription(model.getDescription());
        service.setNote(model.getName());
        service = ServiceUtil.getInstance().insertOrUpdate(service);

        int i = 0;
        for (Standardinspectionrow standardinspectionrow : model.getRows()) {
            Servicerow servicerow = new Servicerow();
            servicerow.setMaterial(standardinspectionrow.getMaterial());
            servicerow.setName(standardinspectionrow.getComment());
            servicerow.setQuantity(standardinspectionrow.getQuantity());
            servicerow.setType(standardinspectionrow.getType());
            servicerow.setListorder(i++);
            servicerow.setService(service.getId());
            ServicerowUtil.getInstance().insertOrUpdate(servicerow);
        }

        JsonUtil.write(resp.getOutputStream(), ServiceUtil.getInstance().findById(service.getId()));
    } else {
        Logger.getLogger(PostJsonServlet.class.getName()).log(Level.WARNING, "Could not read json!", theString);
    }
}

From source file:intapp.databasemigration.Engine.PrepareTargetDatabaseActor.java

@Override
public void onReceive(Object message) throws Exception {
    if (message instanceof PrepareDatabaseRequest) {
        PrepareDatabaseRequest request = (PrepareDatabaseRequest) message;
        this.engine = sender();

        request.Scripts.forEach(script -> {

            try {
                System.out.println("Loading sql for resource " + script);
                InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(script);
                StringWriter writer = new StringWriter();
                IOUtils.copy(resourceAsStream, writer, Charset.defaultCharset());
                String sql = writer.toString();
                queries.add(sql);/*  w  ww .j a  va  2  s  .c  o  m*/

            } catch (Exception ex) {
                System.err.println(ex);
            }
        });

        this.pgConnectionActor.tell("get", self());

    } else if (message instanceof Connection) {
        this.connection = (Connection) message;
        this.queries.forEach(sql -> {
            try {
                PreparedStatement s1 = connection.prepareStatement(sql);
                s1.execute();
            } catch (Exception ex) {
                System.err.println(ex);
            }
        });

        System.out.println("SQL queries executed");
        this.engine.tell(this.getResponse(), null);
    }
}

From source file:name.martingeisse.ecobuild.util.buildlog.DynamicHtmlFlatTreeLogOrganizer.java

@Override
protected void beginTree() {
    out.println("<html><body><script type=\"text/javascript\">");
    try {//  www.j  av a2s  .com
        InputStream jqueryInputStream = DynamicHtmlFlatTreeLogOrganizer.class
                .getResourceAsStream("jquery-1.7.1.min.js");
        IOUtils.copy(jqueryInputStream, out, "utf-8");
        jqueryInputStream.close();
    } catch (IOException e) {
        throw new FatalBuildException("could not include JQuery code", e);
    }
    out.println("</script>");
    moduleCounter = 0;
}

From source file:com.programmablefun.ide.editor.TextMateThemeLoader.java

public TextMateThemeLoader(InputStream is) throws IOException {
    this.stylesheet = new Stylesheet.JsonStylesheet();
    StringWriter w = new StringWriter();
    IOUtils.copy(is, w, Charset.defaultCharset());
    String content = removeXmlComments(w.toString());
    try {//  w  w  w  .  j  av a  2  s .co  m
        this.plist = Plist.fromXml(content);
    } catch (XmlParseException e) {
        throw new IOException(e);
    }
}