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:com.github.rwitzel.streamflyer.core.ModifiableReaderUnitTest.java

private void assertModificationByWriter(String input, String expectedOutput, long positionForModification,
        Change modificationToApply) throws Exception {

    // setup: create modifier and reader
    StringWriter stringWriter = new StringWriter();
    Writer writer = createWriter(stringWriter, positionForModification, modificationToApply);

    // read the stream into an output stream
    for (int index = 0; index < input.length(); index++) {
        writer.append(input.charAt(index));
    }/*from  w ww .ja  va 2  s.c o  m*/
    writer.flush();
    writer.close();

    String foundOutput = stringWriter.toString();

    // compare the expected result with the found result
    assertEquals(expectedOutput, foundOutput);
}

From source file:com.polyvi.xface.extension.XExtensionManager.java

/**
 * ???umeng?//from ww w  .  j ava 2s.  c  o m
 * 
 * @param e
 */
public void reportError(Exception e) {
    try {
        // ?Writer
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        e.printStackTrace(printWriter);
        printWriter.flush();
        writer.flush();
        String stackTrackInfo = writer.toString();
        printWriter.close();
        writer.close();
        MobclickAgent.reportError(mExtensionContext.getSystemContext().getContext(), stackTrackInfo);
    } catch (IOException ioE) {
        XLog.e(CLASS_NAME, ioE.getMessage());
    } catch (Exception genericE) {
        XLog.e(CLASS_NAME, genericE.getMessage());
    }
}

From source file:org.fao.fenix.wds.web.rest.faostat.FAOSTATExporter.java

@POST
@Path("/htmltable")
public Response createExcelFromHTML(@FormParam("data") final String data) {

    // Initiate the stream
    StreamingOutput stream = new StreamingOutput() {

        @Override//from   w  ww.j  a v  a 2s  .  c  o m
        public void write(OutputStream os) throws IOException, WebApplicationException {

            // Write to the stream
            Writer writer = new BufferedWriter(new OutputStreamWriter(os));
            writer.write(data);
            writer.flush();

        }

    };

    // Wrap result
    ResponseBuilder builder = Response.ok(stream);
    builder.header("Content-Disposition", "attachment; filename=" + UUID.randomUUID().toString() + ".xls");

    // Stream Excel
    return builder.build();

}

From source file:edu.vt.middleware.ldap.dsml.AbstractDsml.java

/**
 * This will write the supplied LDAP result to the supplied writer in the form
 * of DSML.// w  w  w .j  a  v  a  2  s. com
 *
 * @param  result  <code>LdapResult</code>
 * @param  writer  <code>Writer</code> to write to
 *
 * @throws  IOException  if an error occurs while writing
 */
public void outputDsml(final LdapResult result, final Writer writer) throws IOException {
    final XMLWriter xmlWriter = new XMLWriter(writer, OutputFormat.createPrettyPrint());
    xmlWriter.write(createDsml(result));
    writer.flush();
}

From source file:com.sun.faban.harness.webclient.Uploader.java

/**
 * Updates the tags file.//from  w  ww. j av a  2  s  .c  om
 * @param req
 * @param resp
 * @throws java.io.IOException
 */
public void updateTagsFile(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String tags = req.getParameter("tags");
    String runId = req.getParameter("runId");
    RunResult result = RunResult.getInstance(new RunId(runId));
    StringBuilder formattedTags = new StringBuilder();
    File runTagFile = new File(Config.OUT_DIR + runId + "/META-INF/tags");
    if (tags != null && !"".equals(tags)) {
        StringTokenizer t = new StringTokenizer(tags, " \n,");
        ArrayList<String> tagList = new ArrayList<String>(t.countTokens());
        while (t.hasMoreTokens()) {
            String nextT = t.nextToken().trim();
            if (nextT != null && !"".equals(nextT)) {
                formattedTags.append(nextT + "\n");
                tagList.add(nextT);
            }
        }
        FileHelper.writeContentToFile(formattedTags.toString(), runTagFile);
        result.tags = tagList.toArray(new String[tagList.size()]);
    }
    try {
        uploadTags(runId);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Uploader.class.getName()).log(Level.SEVERE, null, ex);
    }
    Writer w = resp.getWriter();
    w.write("Tags updating completed");
    w.flush();
    w.close();
}

From source file:edu.vt.middleware.ldap.dsml.AbstractDsml.java

/**
 * This will write the supplied LDAP search results to the supplied writer in
 * the form of DSML./* w  ww .ja  va2 s  .  c  o  m*/
 *
 * @param  results  <code>Iterator</code> of LDAP search results
 * @param  writer  <code>Writer</code> to write to
 *
 * @throws  IOException  if an error occurs while writing
 */
public void outputDsml(final Iterator<SearchResult> results, final Writer writer) throws IOException {
    final XMLWriter xmlWriter = new XMLWriter(writer, OutputFormat.createPrettyPrint());
    xmlWriter.write(createDsml(results));
    writer.flush();
}

From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java

/**
 * Test robustness.//from ww w  .j  ava  2  s . c  o m
 * 
 * @throws Exception
 *             When the test failed.
 */
@Test
public void testMissingArgument() throws Exception {
    final Agent agent = new org.kjkoster.zapcat.zabbix.ZabbixAgent();
    // give the agent some time to open the port
    Thread.sleep(100);
    final Socket socket = new Socket(InetAddress.getLocalHost(),
            org.kjkoster.zapcat.zabbix.ZabbixAgent.DEFAULT_PORT);

    final Writer out = new OutputStreamWriter(socket.getOutputStream());
    out.write("jmx\n");
    out.flush();

    final InputStream in = socket.getInputStream();
    final byte[] buffer = new byte[1024];
    final int read = in.read(buffer);
    assertEquals(29, read);

    assertEquals('Z', buffer[0]);
    assertEquals('B', buffer[1]);
    assertEquals('X', buffer[2]);
    assertEquals('D', buffer[3]);

    assertEquals('N', buffer[17]);
    assertEquals('O', buffer[18]);
    assertEquals('T', buffer[19]);

    // we'll take the rest for granted...

    socket.close();
    agent.stop();
}

From source file:org.lanark.jsr303js.taglib.JSR303JSCodebaseTag.java

/**
 * Copies the chars from in to out and then closes in but
 * leaves out open.//from   w w  w  .  j  ava2 s .c  o m
 *
 * @param in the input reader
 * @param out the output writer
 * @throws IOException if there is an io exception
 */
private void copy(Reader in, Writer out) throws IOException {
    Assert.notNull(in, "No Reader specified");
    Assert.notNull(out, "No Writer specified");
    try {
        char[] buffer = new char[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            logger.warn("Could not close Reader", ex);
        }
    }
}

From source file:org.springmodules.validation.valang.javascript.taglib.ValangCodebaseTag.java

/**
 * Copies the chars from in to out and then closes in but leaves out open.
 *///from   www.ja va  2s. co m
private void copy(Reader in, Writer out) throws IOException {
    Assert.notNull(in, "No Reader specified");
    Assert.notNull(out, "No Writer specified");
    try {
        char[] buffer = new char[1024];
        int bytesRead;

        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            logger.warn("Could not close Reader", ex);
        }
    }
}

From source file:com.joliciel.talismane.machineLearning.AbstractMachineLearningModel.java

@Override
public final void persist(File modelFile) {
    try {/*from w  ww. jav a 2 s . com*/
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(modelFile, false));
        Writer writer = new BufferedWriter(new OutputStreamWriter(zos, "UTF-8"));

        zos.putNextEntry(new ZipEntry("algorithm.txt"));
        writer.write(this.getAlgorithm().name());
        writer.flush();
        zos.flush();

        for (String descriptorKey : descriptors.keySet()) {
            zos.putNextEntry(new ZipEntry(descriptorKey + "_descriptors.txt"));
            List<String> descriptorList = descriptors.get(descriptorKey);
            for (String descriptor : descriptorList) {
                writer.write(descriptor + "\n");
                writer.flush();
            }
            zos.flush();

        }

        zos.putNextEntry(new ZipEntry("attributes.txt"));
        for (String name : this.modelAttributes.keySet()) {
            String value = this.modelAttributes.get(name);
            writer.write(name + "\t" + value + "\n");
            writer.flush();
        }

        for (String name : this.dependencies.keySet()) {
            Object dependency = this.dependencies.get(name);
            zos.putNextEntry(new ZipEntry(name + "_dependency.obj"));
            ObjectOutputStream oos = new ObjectOutputStream(zos);
            try {
                oos.writeObject(dependency);
            } finally {
                oos.flush();
            }
            zos.flush();
        }

        this.persistOtherEntries(zos);

        if (this.externalResources != null) {
            zos.putNextEntry(new ZipEntry("externalResources.obj"));
            ObjectOutputStream oos = new ObjectOutputStream(zos);
            try {
                oos.writeObject(externalResources);
            } finally {
                oos.flush();
            }
            zos.flush();
        }

        this.writeDataToStream(zos);

        zos.putNextEntry(new ZipEntry("model.bin"));
        this.writeModelToStream(zos);
        zos.flush();

        zos.close();
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    }
}