Example usage for java.io StringWriter flush

List of usage examples for java.io StringWriter flush

Introduction

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

Prototype

public void flush() 

Source Link

Document

Flush the stream.

Usage

From source file:Main.java

public static String getStackTrace(Throwable t) {
    StringWriter stringWritter = new StringWriter();
    PrintWriter printWritter = new PrintWriter(stringWritter, true);
    t.printStackTrace(printWritter);//from   w  w w  .  j ava  2  s  .co  m
    printWritter.flush();
    stringWritter.flush();
    return stringWritter.toString();
}

From source file:org.qi4j.library.staticlet.files.TestData.java

public static synchronized File deployDocRoot(File buildDirectory) throws IOException {
    // Unzip staticlet-docroot.zip to ~/target/ --------------------------------------------------------------------
    String testResourcesDirectory = System.getProperty("testResourcesDirectory");
    FileUtils.forceMkdir(buildDirectory);
    int buffSize = 1024 * 64;
    ZipFile docRootZip = new ZipFile(new File(testResourcesDirectory, "staticlet-docroot.zip"));
    Enumeration<? extends ZipEntry> entries = docRootZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry eachEntry = entries.nextElement();
        File out = new File(buildDirectory, eachEntry.getName());
        FileUtils.forceMkdir(out.getParentFile());
        if (!eachEntry.isDirectory()) {
            BufferedInputStream input = new BufferedInputStream(docRootZip.getInputStream(eachEntry));
            byte[] buffer = new byte[buffSize];
            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(out), buffSize);
            while (input.read(buffer, 0, buffSize) != -1) {
                output.write(buffer, 0, buffSize);
            }/* ww w . j a v a 2s. com*/
            output.flush();
            output.close();
            input.close();
        }
    }
    docRootZip.close();

    // Load test docroot content in memory for unit tests ----------------------------------------------------------
    File docRoot = new File(buildDirectory, "staticlet-docroot");

    // text-plain.txt
    StringWriter sw = new StringWriter();
    IOUtils.copy(new FileInputStream(new File(docRoot, TEXT_PLAIN)), sw, "UTF-8");
    sw.flush();
    TEXT_PLAIN_TEXT = sw.toString().trim();

    return docRoot;
}

From source file:org.apache.streams.sysomos.util.SysomosUtils.java

/**
 * Queries the sysomos URL and provides the response as a String.
 *
 * @param url the Sysomos URL to query//from w w  w  .j  a va2s  .  c  om
 * @return valid XML String
 */
public static String queryUrl(URL url) {
    try {
        HttpURLConnection cn = (HttpURLConnection) url.openConnection();
        cn.setRequestMethod("GET");
        cn.addRequestProperty("Content-Type", "text/xml;charset=UTF-8");
        cn.setDoInput(true);
        cn.setDoOutput(false);
        StringWriter writer = new StringWriter();
        IOUtils.copy(new InputStreamReader(cn.getInputStream()), writer);
        writer.flush();

        String xmlResponse = writer.toString();
        if (StringUtils.isEmpty(xmlResponse)) {
            throw new SysomosException(
                    "XML Response from Sysomos was empty : " + xmlResponse + "\n" + cn.getResponseMessage(),
                    cn.getResponseCode());
        }
        return xmlResponse;
    } catch (IOException ex) {
        LOGGER.error("Error executing request : {}", ex, url.toString());
        String message = ex.getMessage();
        Matcher match = CODE_PATTERN.matcher(message);
        if (match.find()) {
            int errorCode = Integer.parseInt(match.group(1));
            throw new SysomosException(message, ex, errorCode);
        } else {
            throw new SysomosException(ex.getMessage(), ex);
        }
    }
}

From source file:com.github.fge.jackson.JacksonUtils.java

/**
 * Pretty print a JSON value/*from  w w  w  .  jav  a2s .  c om*/
 *
 * @param node the JSON value to print
 * @return the pretty printed value as a string
 * @see #newMapper()
 */
public static String prettyPrint(final JsonNode node) {
    final StringWriter writer = new StringWriter();

    try {
        WRITER.writeValue(writer, node);
        writer.flush();
    } catch (JsonGenerationException e) {
        throw new RuntimeException("How did I get there??", e);
    } catch (JsonMappingException e) {
        throw new RuntimeException("How did I get there??", e);
    } catch (IOException ignored) {
        // cannot happen
    }

    return writer.toString();
}

From source file:org.n52.io.geojson.JSONUtils.java

public static String print(final JsonNode node) {
    final StringWriter writer = new StringWriter();
    try {/*from www.j a v a  2s  . co  m*/
        print(writer, node);
        writer.flush();
    } catch (IOException e) {
        // cannot happen
    } finally {
        try {
            writer.close();
        } catch (IOException ioe) {
            LOGGER.error("Error while colsing closeable!", ioe);
        }
    }
    return writer.toString();
}

From source file:org.n52.iceland.util.JSONUtils.java

public static String print(JsonNode node) {
    final StringWriter writer = new StringWriter();
    try {// w w  w.j  a  v  a2  s.co m
        print(writer, node);
        writer.flush();
    } catch (IOException e) {
        // cannot happen
    } finally {
        try {
            Closeables.close(writer, true);
        } catch (IOException ioe) {
            LOGGER.error("Error while colsing closeable!", ioe);
        }
    }
    return writer.toString();
}

From source file:Main.java

/**
 * Converts a dom document to an xml String.
 * /*from   w w  w  .ja  va 2s .c om*/
 * @param document
 * @return
 * @see XMLSerializer
 */
public static String toXML(final Document document) {
    final XMLSerializer xmlSerializer = new XMLSerializer();

    final OutputFormat outputFormat = new OutputFormat(document);
    outputFormat.setOmitXMLDeclaration(false);
    outputFormat.setEncoding("UTF-8");
    outputFormat.setIndenting(true);

    final StringWriter stringWriter = new StringWriter();
    xmlSerializer.setOutputCharStream(stringWriter);
    xmlSerializer.setOutputFormat(outputFormat);
    try {
        xmlSerializer.serialize(document);
        stringWriter.flush();
        stringWriter.close();
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }

    return stringWriter.toString();
}

From source file:Main.java

public static String toXmlString(Document doc) throws Exception {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    writer.flush();

    return writer.toString();
}

From source file:com.heliosapm.tsdblite.handlers.websock.WebSocketServerHandler.java

private static void sendWebSockError(final ChannelHandlerContext ctx, final Number rid, final String session,
        final String error, final Throwable t) {
    final String ts;
    if (t != null) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        sw.flush();
        t.printStackTrace(pw);//from  ww  w  . ja  v a 2s.  c o m
        ts = sw.toString();
    } else {
        ts = null;
    }

    ctx.writeAndFlush(new TextWebSocketFrame(JSON.serializeToBuf(
            FluentMap.newMap(MapType.LINK, String.class, Object.class).fput("error", error).fput("rid", rid)
                    .sfput("session", session).sfput("trace", ts).asMap(LinkedHashMap.class))));

}

From source file:ch.ralscha.extdirectspring.util.ExtDirectSpringUtil.java

/**
 * Converts a stacktrace into a String//  ww w.ja v a2  s .c  o  m
 *
 * @param t a Throwable
 * @return the whole stacktrace in a String
 */
public static String getStackTrace(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    t.printStackTrace(pw);
    pw.flush();
    sw.flush();
    return sw.toString();
}