Example usage for java.io StringWriter toString

List of usage examples for java.io StringWriter toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Return the buffer's current value as a string.

Usage

From source file:Main.java

public static void openFeedback(Activity activity) {
    try {//from  www  . ja v  a 2 s  .c om
        throw new Exception();
    } catch (Exception e) {
        ApplicationErrorReport report = new ApplicationErrorReport();
        report.packageName = report.processName = activity.getApplication().getPackageName();
        report.time = System.currentTimeMillis();
        report.type = ApplicationErrorReport.TYPE_CRASH;
        report.systemApp = false;
        ApplicationErrorReport.CrashInfo crash = new ApplicationErrorReport.CrashInfo();
        crash.exceptionClassName = e.getClass().getSimpleName();
        crash.exceptionMessage = e.getMessage();
        StringWriter writer = new StringWriter();
        PrintWriter printer = new PrintWriter(writer);
        e.printStackTrace(printer);
        crash.stackTrace = writer.toString();
        StackTraceElement stack = e.getStackTrace()[0];
        crash.throwClassName = stack.getClassName();
        crash.throwFileName = stack.getFileName();
        crash.throwLineNumber = stack.getLineNumber();
        crash.throwMethodName = stack.getMethodName();
        report.crashInfo = crash;
        Intent intent = new Intent(Intent.ACTION_APP_ERROR);
        intent.putExtra(Intent.EXTRA_BUG_REPORT, report);
        activity.startActivity(intent);
    }
}

From source file:Main.java

/**
 * Converts an XML document or node to an equivalent string. Applies the
 * reverse operation of {@link #xmlStringToDocument(String)}.
 *
 * @param d the node to convert//from  w  w  w  . ja  va  2  s. c o  m
 * @return a String representation of the XML node
 * @throws TransformerFactoryConfigurationError if an error occurs
 * configuring the transformer factory
 * @throws TransformerException if an error occurs during the transformation
 */
static String xmlDocumentToString(Node d) throws TransformerFactoryConfigurationError, TransformerException {
    StringWriter writer = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.transform(new DOMSource(d), new StreamResult(writer));
    return writer.toString();
}

From source file:org.jboss.tools.web.pagereloader.internal.websocket.LiveReloadWebSocketHandler.java

private static String buildRefreshCommand(String path)
        throws JsonGenerationException, JsonMappingException, IOException {
    List<Object> command = new ArrayList<Object>();
    Map<String, Object> refreshArgs = new HashMap<String, Object>();
    command.add("refresh");
    refreshArgs.put("path", path);
    refreshArgs.put("apply_js_live", true);
    refreshArgs.put("apply_css_live", true);
    command.add(refreshArgs);/*from  w ww.ja v  a  2 s. c o  m*/
    StringWriter commandWriter = new StringWriter();
    objectMapper.writeValue(commandWriter, command);
    String cmd = commandWriter.toString();
    return cmd;
}

From source file:ar.com.zauber.commons.xmpp.message.XMPPMessageTest.java

/** carga un archivo del classpath local como string */
public static String getResult(final String name) {
    final InputStream is = XMPPMessageTest.class.getResourceAsStream(name);
    final StringWriter sw = new StringWriter();
    try {//  w w w.j  a v a  2s .c  om
        IOUtils.copy(is, sw);
        return sw.toString();
    } catch (final IOException e) {
        throw new UnhandledException(e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:Main.java

/**
 * Creates a {@link String} out of a {@link Node}
 * //from  www.  j a  v a  2  s . c  o  m
 * @param node
 *            never <code>null</code>
 * @return the node as string, never <code>null</code>
 * @throws Exception
 */
public static String asString(Node node) throws Exception {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
}

From source file:net.alpha.velocity.spring.support.VelocityEngineUtils.java

/**
 * Merge the specified Velocity template with the given model into a String.
 * <p>When using this method to prepare a text for a mail to be sent with Spring's
 * mail support, consider wrapping VelocityException in MailPreparationException.
 *
 * @param velocityEngine   VelocityEngine to work with
 * @param templateLocation the location of template, relative to Velocity's resource loader path
 * @param model            the Map that contains model names as keys and model objects as values
 * @return the result as String//from   w  w  w  . j a  v a 2  s. c  o m
 * @throws VelocityException if the template wasn't found or rendering failed
 * @see org.springframework.mail.MailPreparationException
 * @deprecated Use {@link #mergeTemplateIntoString(VelocityEngine, String, String, Map)}
 * instead, following Velocity 1.6's corresponding deprecation in its own API.
 */
@Deprecated
public static String mergeTemplateIntoString(VelocityEngine velocityEngine, String templateLocation,
        Map<String, Object> model) throws VelocityException {

    StringWriter result = new StringWriter();
    mergeTemplate(velocityEngine, templateLocation, model, result);
    return result.toString();
}

From source file:de.bmarwell.j9kwsolver.util.HttpConnectorFactory.java

/**
 * Returns the Body from the URI via http request.
 * @param uri the URI to get the body from.
 * @return the Body as String./*from   w w  w .  j a  va 2s.  co  m*/
 */
public static String getBodyFromRequest(final URI uri) {
    CloseableHttpResponse response = null;
    String responseBody = null;
    HttpGet httpGet = new HttpGet(uri);

    LOG.debug("Requesting URI: {}.", httpGet.getURI());

    try {
        response = httpClient.execute(httpGet);
        StringWriter writer = new StringWriter();
        IOUtils.copy(response.getEntity().getContent(), writer);
        responseBody = writer.toString();
    } catch (IOException e) {
        LOG.error("Fehler beim HTTP Request!", e);
    }

    return responseBody;
}

From source file:Main.java

public static String toString(Element e) {
    try {/*from   www .  ja v  a  2  s. c o m*/
        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer xform = tfactory.newTransformer();
        Source src = new DOMSource(e);
        java.io.StringWriter writer = new StringWriter();
        Result result = new javax.xml.transform.stream.StreamResult(writer);
        xform.transform(src, result);
        return writer.toString();
    } catch (Exception ex) {
        return "Unable to convert to string: " + ex.toString();
    }
}

From source file:Main.java

/**
 * <p>// w ww . j  a v a  2s . co m
 * Print the full stack trace of a <code>Throwable</code> object into a
 * string buffer and return the corresponding string.  
 * </p>
 */
public static String printStackTrace(Throwable t) {
    java.io.StringWriter strWriter = new java.io.StringWriter();
    java.io.PrintWriter prWriter = new java.io.PrintWriter(strWriter);
    t.printStackTrace(prWriter);
    prWriter.flush();
    return strWriter.toString();
}

From source file:Main.java

/**
 * To stack trace string string.//from   ww w .j av  a 2  s.c o  m
 *
 * @param throwable the throwable
 * @return the string
 */
public static String toStackTraceString(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    throwable.printStackTrace(pw);
    String stackTraceString = sw.toString();
    //Reduce data to 128KB so we don't get a TransactionTooLargeException when sending the intent.
    //The limit is 1MB on Android but some devices seem to have it lower.
    //See: http://developer.android.com/reference/android/os/TransactionTooLargeException.html
    //And: http://stackoverflow.com/questions/11451393/what-to-do-on-transactiontoolargeexception#comment46697371_12809171
    if (stackTraceString.length() > MAX_STACK_TRACE_SIZE) {
        String disclaimer = " [stack trace too large]";
        stackTraceString = stackTraceString.substring(0, MAX_STACK_TRACE_SIZE - disclaimer.length())
                + disclaimer;
    }
    return stackTraceString;
}