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

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

Introduction

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

Prototype

public static void write(StringBuffer data, OutputStream output, String encoding) throws IOException 

Source Link

Document

Writes chars from a StringBuffer to bytes on an OutputStream using the specified character encoding.

Usage

From source file:com.cloudbees.sdk.utils.PersistedGAVStore.java

protected void write(File f, GAV v) {
    try {/*from   ww  w. j av  a2  s . c  o m*/
        FileOutputStream o = new FileOutputStream(f);
        try {
            IOUtils.write(v.toString(), o, "UTF-8");
        } finally {
            IOUtils.closeQuietly(o);
        }
    } catch (IOException e) {
        throw new IllegalStateException("Failed to write to " + f, e);
    }
}

From source file:com.thoughtworks.go.plugin.infra.monitor.AbstractDefaultPluginJarLocationMonitorTest.java

protected void updateFileContents(File someFile) {
    try (FileOutputStream output = new FileOutputStream(someFile)) {
        IOUtils.write("some rubbish", output, Charset.defaultCharset());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }/*from w  w w  .  j  a  v  a2s  .com*/
}

From source file:acromusashi.stream.ml.common.notify.ResultFilePrinter.java

/**
 * {@inheritDoc}//from w  ww.  ja  v  a 2s .  co m
 */
@Override
public void notifyResult(T result) {
    try {
        IOUtils.write(this.header + result.toString(), this.outputStream, this.encode);
    } catch (IOException ex) {
        logger.warn("ResultFilePrint failed. File=" + this.filePath + ", Content=" + this.header
                + result.toString(), ex);
    }
}

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

@Test
public void workWithStream() {
    InputStream in = null;//from ww  w  .j  a  v a2 s.co  m
    try {
        String content = "Stream testing";

        // String - > InputStream.
        in = IOUtils.toInputStream(content, "UTF-8");

        // String - > OutputStream
        System.out.println("String to OutputStram:");
        IOUtils.write(content, System.out, "UTF-8");

        // //////////////////
        // InputStream/Reader -> String
        System.out.println("\nInputStram to String:");
        System.out.println(IOUtils.toString(in, "UTF-8"));

        // InputStream/Reader -> OutputStream/Writer ???.
        InputStream in2 = IOUtils.toInputStream(content); // ?inputSteam
        System.out.println("InputStream to OutputStream:");
        IOUtils.copy(in2, System.out);

        // /////////////////
        // InputStream ->Reader
        InputStreamReader reader = new InputStreamReader(in, Charsets.UTF_8);
        // Reader->InputStream
        ReaderInputStream in3 = new ReaderInputStream(reader, Charsets.UTF_8);

        // OutputStream ->Writer
        OutputStreamWriter writer = new OutputStreamWriter(System.out, Charsets.UTF_8);
        // Writer->OutputStream
        WriterOutputStream out2 = new WriterOutputStream(writer, Charsets.UTF_8);

        // ////////////////////
        // WriterString.
        StringWriter sw = new StringWriter();
        sw.write("I am String writer");
        System.out.println("\nCollect writer content:");
        System.out.println(sw.toString());

    } catch (IOException e) {
        Exceptions.unchecked(e);
    } finally {
        // ?Stream
        IOUtils.closeQuietly(in);
    }
}

From source file:ch.cyberduck.core.googlestorage.S3SingleUploadServiceTest.java

@Test
public void testUpload() throws Exception {
    final S3SingleUploadService m = new S3SingleUploadService(session,
            new S3WriteFeature(session, new S3DisabledMultipartService()));
    final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final Path test = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    final String random = new RandomStringGenerator.Builder().build().generate(1000);
    final OutputStream out = local.getOutputStream(false);
    IOUtils.write(random, out, Charset.defaultCharset());
    out.close();/*  w ww.  j  a  v  a 2  s.  c  o m*/
    final TransferStatus status = new TransferStatus();
    status.setLength(random.getBytes().length);
    m.upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(),
            status, new DisabledLoginCallback());
    assertTrue(new S3FindFeature(session).find(test));
    final PathAttributes attributes = session.list(container, new DisabledListProgressListener()).get(test)
            .attributes();
    assertEquals(random.getBytes().length, attributes.getSize());
    new S3DefaultDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
    local.delete();
}

From source file:com.rpeactual.json2xml.JSON2XML.java

/**
 * Takes an JSON input stream and returns an XML output stream 
 * /* w ww .  ja v a 2 s.c  o m*/
 * @param inputStream
 * @param outputStream
 * @throws IOException
 * @throws XmlException 
 */
public void json2xml(InputStream jsonStream, String outputFolder) throws IOException, XmlException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(jsonStream, writer, UTF_8);
    String jsonStr = writer.toString();

    JSONObject o = new JSONObject(jsonStr);

    String xml = org.json.XML.toString(o);

    File xmlFile = new File(outputFolder, "output.xml");
    OutputStream xmlStream = new FileOutputStream(xmlFile);

    IOUtils.write(xml, xmlStream, UTF_8);

    File xsdFile = new File(outputFolder, "output.xsd");
    xml2xsd(xmlFile.getAbsolutePath(), xsdFile.getAbsolutePath());
}

From source file:net.logstash.logback.encoder.CompositeJsonEncoder.java

@Override
public void doEncode(Event event) throws IOException {

    doEncodeWrapped(prefix, event);//from   w  w  w  .j  av  a2s.  com

    formatter.writeEventToOutputStream(event, outputStream);

    doEncodeWrapped(suffix, event);

    if (this.lineSeparator != null) {
        IOUtils.write(this.lineSeparator, outputStream, charset);
    }

    if (immediateFlush) {
        outputStream.flush();
    }

}

From source file:com.blackducksoftware.integration.build.utils.FlatDependencyListWriter.java

public void write(final File outputDirectory, final String hubProjectName, final DependencyNode rootNode)
        throws IOException {
    final Set<String> gavStrings = new HashSet<>();
    addAllGavs(gavStrings, rootNode);//  ww w  . j  av  a 2 s.  c o  m
    final List<String> gavList = new ArrayList<>(gavStrings);
    Collections.sort(gavList);

    // if the directory doesn't exist yet, let's create it
    outputDirectory.mkdirs();

    String filename = getFilename(hubProjectName);
    final File file = new File(outputDirectory, filename);
    logger.info(String.format("Generating file: %s", file.getCanonicalPath()));

    try (final OutputStream outputStream = new FileOutputStream(file)) {
        for (final String gav : gavList) {
            IOUtils.write(gav, outputStream, "UTF8");
            IOUtils.write("\n", outputStream, "UTF8");
        }
    }
}

From source file:eu.delving.metadata.CachedResourceResolver.java

@Override
public LSInput resolveResource(String type, final String namespaceUri, final String publicId,
        final String systemId, final String baseUri) {
    File resourceFile = context.file(systemId);
    if (!resourceFile.exists()) {
        try {/* w w  w .j a v  a 2  s  .c  o m*/
            String schemaText = fetchResource(systemId, baseUri);
            FileOutputStream fileOutputStream = new FileOutputStream(resourceFile);
            IOUtils.write(schemaText, fileOutputStream, "UTF-8");
            IOUtils.closeQuietly(fileOutputStream);
        } catch (Exception e) {
            throw new RuntimeException("Unable to fetch and store resource: " + resourceFile, e);
        }
    }
    return new FileBasedInput(publicId, systemId, baseUri, resourceFile);
}

From source file:codes.thischwa.c5c.requestcycle.response.GenericResponse.java

/**
 * Write the response to the {@link HttpServletResponse} (The character encoding of the {@link HttpServletResponse} will
 * be used. Inherited object could overwrite this to write 
 * special content or headers.//from   www. jav a2 s  .  c  o  m
 * 
 * @param resp
 *            the resp
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
@JsonIgnore
public void write(HttpServletResponse resp) throws IOException {
    if (mode != null && mode.getContentType() != null)
        resp.setContentType(mode.getContentType());
    OutputStream out = resp.getOutputStream();
    String json = toString();
    IOUtils.write(json, out, resp.getCharacterEncoding());
    IOUtils.closeQuietly(out);
}