Example usage for java.io Writer flush

List of usage examples for java.io Writer flush

Introduction

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

Prototype

public abstract void flush() throws IOException;

Source Link

Document

Flushes the stream.

Usage

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignProvider.java

private static void resourceInToOut(InputStream in, String sourceEncoding, OutputStream out,
        String targetEncoding) throws UnsupportedEncodingException, IOException {
    if (sourceEncoding != null && targetEncoding != null) {
        Reader inReader = new InputStreamReader(in, sourceEncoding);
        Writer outWriter = new OutputStreamWriter(out, targetEncoding);
        WGUtils.inToOut(inReader, outWriter, 4096);
        inReader.close();/*from w  w  w. ja  va2 s .  c o m*/
        outWriter.flush();
        outWriter.close();
    } else {
        WGUtils.inToOut(in, out, 4096);
        in.close();
        out.flush();
        out.close();
    }
}

From source file:io.undertow.server.handlers.HttpTunnelingViaConnectTestCase.java

@Test
public void testConnectViaProxy() throws Exception {

    final HttpHost proxy = new HttpHost(DefaultServer.getHostAddress("default"),
            DefaultServer.getHostPort("default") + 1, "http");
    final HttpHost target = new HttpHost(DefaultServer.getHostAddress("default"),
            DefaultServer.getHostPort("default"), "http");
    ProxyClient proxyClient = new ProxyClient();
    Socket socket = proxyClient.tunnel(proxy, target, new UsernamePasswordCredentials("a", "b"));
    try {/*from  w w  w .ja  v  a2 s  .c om*/
        Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET);
        out.write("GET / HTTP/1.1\r\n");
        out.write("Host: " + target.toHostString() + "\r\n");
        out.write("Connection: close\r\n");
        out.write("\r\n");
        out.flush();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET));
        String line = null;
        boolean found = false;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
            if (line.equals("MyHeader: MyValue")) {
                found = true;
            }
        }
        Assert.assertTrue(found);
    } finally {
        socket.close();
    }
}

From source file:com.netxforge.oss2.config.LinkdConfigFactory.java

/** {@inheritDoc} */
protected synchronized void saveXml(String xml) throws IOException {
    if (xml != null) {
        long timestamp = System.currentTimeMillis();
        final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.LINKD_CONFIG_FILE_NAME);
        LogUtils.debugf(LinkdConfigFactory.class, "saveXml: saving config file at %d: %s", timestamp,
                cfgFile.getPath());/*from  www  .  j  a va 2  s  .  c  o m*/
        final Writer fileWriter = new OutputStreamWriter(new FileOutputStream(cfgFile), "UTF-8");
        fileWriter.write(xml);
        fileWriter.flush();
        fileWriter.close();
        LogUtils.debugf(LinkdConfigFactory.class, "saveXml: finished saving config file: %s",
                cfgFile.getPath());
    }
}

From source file:org.pentaho.chart.plugin.jfreechart.outputs.JFreeChartOutput.java

public Writer persistMap(Writer outputStream, String mapName) throws PersistenceException {
    if (outputStream == null) {
        outputStream = new StringWriter();
    }//from  w ww .  jav  a2 s.  co m

    final String mapString = getMap(mapName);
    try {
        outputStream.write(mapString);
        outputStream.flush();
    } catch (IOException e) {
        throw new PersistenceException(e);
    }
    return outputStream;
}

From source file:com.kysoft.cpsi.audit.controller.SelfCheckAjaxUploadController.java

/**
 * //w  w  w  . jav a2s.  co m
 * @param response
 * @param state
 */
@SuppressWarnings("unused")
private void responseMessage(HttpServletResponse response, State state) {
    response.setCharacterEncoding(HuskyConstants.ENCODING_UTF8);
    response.setContentType("text/html; charset=UTF-8");
    Writer writer = null;
    try {
        writer = response.getWriter();
        writer.write("{\"code\":" + state.getCode() + ",\"message\":\"" + state.getMessage() + "\"}");
        writer.flush();
        writer.close();
    } catch (Exception e) {
    } finally {
        IOUtils.closeQuietly(writer);
    }
}

From source file:org.matsim.contrib.parking.parkingchoice.lib.GeneralLib.java

/**
 * Write out a list of Strings//w w w .j a v a  2s . c om
 * 
 * after each String in the list a "\n" is added.
 * 
 * @param list
 * @param fileName
 */
public static void writeList(ArrayList<String> list, String fileName) {
    Writer writer = null;
    if (fileName.toLowerCase(Locale.ROOT).endsWith(".gz")) {
        writer = IOUtils.getBufferedWriter(fileName);
    }

    try {
        if (writer == null) {
            FileOutputStream fos = new FileOutputStream(fileName);
            writer = new OutputStreamWriter(fos);
        }
        char[] charArray = Lists.getCharsOfAllArrayItemsWithNewLineCharacterInbetween(list);
        writer.write(charArray);

        writer.flush();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.ku.brc.specify.extras.FishBaseInfoGetter.java

/**
 * Performs a "generic" HTTP request and fill member variable with results use
 * "getDigirResultsetStr" to get the results as a String
 *
 *//*  www  . j ava2s .  c  o  m*/
public void getDOMDoc(final String url, final InfoType infoType) {
    dom = null;
    String fileName = tmpDir + File.separator + genus + "_" + species + "_" + infoType.toString() + ".xml";
    //System.out.println(fileName);
    File file = new File(fileName);
    if (file.exists()) {
        try {
            dom = XMLHelper.readFileToDOM4J(file);

        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FishBaseInfoGetter.class, ex);
            ex.printStackTrace();
            status = ErrorCode.Error;
        }

    } else {
        ////System.out.println("http://www.fishbase.org.ph/webservice/Species/SpeciesSummary.asp?Genus=Etheostoma&Species=ramseyi");
        //System.out.println(url);
        byte[] bytes = super.doHTTPRequest(url);

        data = new String(bytes);
        int inx = data.indexOf("<?");
        if (inx > -1) {
            data = data.substring(inx, data.length());

            //System.out.println(data);
            try {
                Writer output = new BufferedWriter(new FileWriter(file));
                output.write(data);
                output.flush();
                output.close();

                // Is is cheating and slow, but I will do it for now
                // XXX FIXME!
                dom = XMLHelper.readFileToDOM4J(file);

            } catch (Exception ex) {
                edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
                edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FishBaseInfoGetter.class, ex);
                ex.printStackTrace();
                status = ErrorCode.Error;
            }

        } else {
            status = ErrorCode.Error;
            data = null;
        }

    }
}

From source file:org.fcrepo.http.api.responses.StreamingBaseHtmlProvider.java

@Override
public void writeTo(final RdfStream rdfStream, final Class<?> type, final Type genericType,
        final Annotation[] annotations, final MediaType mediaType,
        final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException {

    try {/* ww  w  .  ja v a 2 s .c  o m*/
        final RdfStream nsRdfStream = new NamespaceRdfContext(rdfStream.session());

        rdfStream.namespaces(nsRdfStream.namespaces());

        final Node subject = VIEW_HELPERS.getContentNode(rdfStream.topic());

        final Model model = rdfStream.asModel();

        final Template nodeTypeTemplate = getTemplate(model, subject, Arrays.asList(annotations));

        final Context context = getContext(model, subject);

        // the contract of MessageBodyWriter<T> is _not_ to close the stream
        // after writing to it
        final Writer outWriter = new OutputStreamWriter(entityStream);
        nodeTypeTemplate.merge(context, outWriter);
        outWriter.flush();

    } catch (final RepositoryException e) {
        throw new WebApplicationException(e);
    }

}

From source file:com.atilika.kuromoji.benchmark.Benchmark.java

private void writeRecord(Writer writer, String record) throws IOException {
    writer.write(record);//from www  .j  av a  2 s .c  om
    writer.write('\n');
    writer.flush();

    if (outputStatistics) {
        System.out.println(record);
    }
}

From source file:com.github.rwitzel.streamflyer.experimental.stateful.util.IdleModifierStatePerformanceTest.java

private void assertOverheadByWriter(String input, double expectedMaxSpentTime, double expectedMaxOverhead)
        throws Exception {

    // setup: create modifier and writer
    Writer originalWriter = new StringWriter();
    ModifyingWriter writer = new ModifyingWriter(originalWriter, createIdleModifier());

    // write the stream to an output stream
    long start = System.currentTimeMillis();
    for (int index = 0; index < input.length(); index++) {
        writer.append(input.charAt(index));
    }/*w w w. j a v  a2  s  .  c  o  m*/
    writer.flush();
    writer.close();
    long end = System.currentTimeMillis();

    originalWriter.toString();

    Writer writer2 = new StringWriter();

    // read the stream into an output stream
    long start2 = System.currentTimeMillis();
    for (int index = 0; index < input.length(); index++) {
        writer2.append(input.charAt(index));
    }
    writer2.flush();
    writer2.close();
    long end2 = System.currentTimeMillis();

    long overhead = (end - start) - (end2 - start2);

    assertTime(end - start, expectedMaxSpentTime, "Time spent by ModifyingWriter:");
    assertTime(overhead, expectedMaxOverhead, "Overhead   by ModifyingWriter:");
}