Example usage for java.io OutputStreamWriter OutputStreamWriter

List of usage examples for java.io OutputStreamWriter OutputStreamWriter

Introduction

In this page you can find the example usage for java.io OutputStreamWriter OutputStreamWriter.

Prototype

public OutputStreamWriter(OutputStream out, CharsetEncoder enc) 

Source Link

Document

Creates an OutputStreamWriter that uses the given charset encoder.

Usage

From source file:edu.emory.mathcs.nlp.util.IOUtilsTest.java

@Test
public void fileNonStdFileSystem() throws Exception {
    FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
    Path testFile = fs.getPath("/foo.txt");
    try (Writer writer = Files.newBufferedWriter(testFile)) {
        writer.write(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS);
    }//from   w  ww.jav a2 s  .c  o  m

    try (InputStream is = IOUtils.createArtifactInputStream(testFile)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    String uri = testFile.toUri().toString();
    try (InputStream is = IOUtils.createArtifactInputStream(uri)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    Path testFilexz = fs.getPath("/foo.txt.xz");
    try (Writer writer = new OutputStreamWriter(
            new XZOutputStream(Files.newOutputStream(testFilexz), new LZMA2Options()), UTF_8)) {
        writer.write(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS);
    }

    try (InputStream is = IOUtils.createArtifactInputStream(testFilexz)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    uri = testFilexz.toUri().toString();
    try (InputStream is = IOUtils.createArtifactInputStream(uri)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    Path testFilegz = fs.getPath("/foo.txt.gz");
    try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(Files.newOutputStream(testFilegz)),
            UTF_8)) {
        writer.write(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS);
    }

    try (InputStream is = IOUtils.createArtifactInputStream(testFilegz)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    uri = testFilegz.toUri().toString();
    try (InputStream is = IOUtils.createArtifactInputStream(uri)) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    // test plain old file system
    try (InputStream is = IOUtils.createArtifactInputStream("src/test/resources/a/test/some.txt")) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }

    // test classpath
    try (InputStream is = IOUtils.createArtifactInputStream("a/test/some.txt")) {
        String contents = org.apache.commons.io.IOUtils.toString(is, "utf-8");
        assertEquals(THIS_IS_THE_CEREAL_SHOT_FROM_GUNS, contents);
    }
}

From source file:net.lightbody.bmp.proxy.jetty.jetty.servlet.ServletWriter.java

ServletWriter(OutputStream os, String encoding) throws IOException {
    super((os instanceof HttpOutputStream) ? ((HttpOutputStream) os).getWriter(encoding)
            : new OutputStreamWriter(os, encoding));
    this.os = os;
    this.encoding = encoding;
}

From source file:fr.itinerennes.onebusaway.bundle.tasks.GenerateTripsCsvTask.java

/**
 * {@inheritDoc}/*from  ww w .j  av a2s . c o m*/
 * 
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {

    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), CHARSET));

        // output trips count
        out.write(String.valueOf(gtfsDao.getAllTrips().size()));
        out.newLine();

        for (final Trip trip : gtfsDao.getAllTrips()) {

            out.write(trip.getId().toString());
            out.write(';');
            out.write(trip.getRoute().getId().toString());
            out.write(';');
            out.write(trip.getTripHeadsign());
            out.write(';');
            out.write(trip.getDirectionId());
            out.write(';');
            out.newLine();
        }

    } catch (final FileNotFoundException e) {
        LOGGER.error("output file not found", e);
    } catch (final IOException e) {
        LOGGER.error("can't write to output file", e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}