Refine search
/** * Prints full exception stack trace, from top to root cause, into a String. */ public static String exceptionChainToString(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); while (t != null) { t.printStackTrace(pw); t = t.getCause(); } pw.flush(); sw.flush(); return sw.toString(); }
@Override public String toJson(Object obj) { StringWriter writer = new StringWriter(); try { JsonGenerator generator = objectMapper.getFactory().createGenerator(writer); objectMapper.writeValue(generator, obj); writer.flush(); writer.close(); generator.close(); return writer.getBuffer().toString(); } catch (IOException e) { throw new InvalidJsonException(e); } }
/** returns stacktrace as a String */ public static String stringStackTrace(Throwable e) { StringWriter sw = new StringWriter(500); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); pw.flush(); sw.flush(); return sw.toString(); }
String contentTypeAsString = contentTypeToString(contentType); if (data instanceof Closure) { StringWriter out = new StringWriter(); PrintWriter writer = new PrintWriter(out); ((Closure) data).call(writer); writer.close(); out.flush(); data = out; } else if (data instanceof Writable) { StringWriter out = new StringWriter(); ((Writable) data).writeTo(out); out.flush(); data = out; } else if (data instanceof Reader && !(data instanceof BufferedReader)) { StringWriter out = new StringWriter(); DefaultGroovyMethods.leftShift(out, (BufferedReader) data);
private void addStackTrace(Map<String, Object> errorAttributes, Throwable error) { StringWriter stackTrace = new StringWriter(); error.printStackTrace(new PrintWriter(stackTrace)); stackTrace.flush(); errorAttributes.put("trace", stackTrace.toString()); }
private StringWriter sw = new StringWriter(); sw.flush();
private static void outputByWriter(Consumer<StringWriter> printer, PrintWriter out) { StringWriter output = new StringWriter(); printer.accept(output); output.flush(); String escapedXml = StringEscapeUtils.escapeHtml4(output.toString()); String[] lines = escapedXml.split("\n"); out.print("<div><pre>"); for (String line : lines) { int numSpaces = 0; while (numSpaces < line.length() && line.charAt(numSpaces) == ' ') { out.print(" "); ++numSpaces; } out.print(line.substring(numSpaces)); out.print("\n"); } out.print("</pre></div>"); }
System.out.println(System.currentTimeMillis() - start); start = System.currentTimeMillis(); StringWriter writer = new StringWriter(); parse.execute(writer, parent); writer.flush(); int total = 0; while (true) { parse.execute(new StringWriter(), parent); total++; if (System.currentTimeMillis() - start > TIME * 1000) break;
private void addStackTrace(Map<String, Object> errorAttributes, Throwable error) { StringWriter stackTrace = new StringWriter(); error.printStackTrace(new PrintWriter(stackTrace)); stackTrace.flush(); errorAttributes.put("trace", stackTrace.toString()); }
@Override public String toJson(Object obj) { StringWriter writer = new StringWriter(); try { JsonGenerator generator = objectMapper.getFactory().createGenerator(writer); objectMapper.writeValue(generator, obj); writer.flush(); writer.close(); generator.close(); return writer.getBuffer().toString(); } catch (IOException e) { throw new InvalidJsonException(); } }
protected String getResourceContents(org.springframework.core.io.Resource resource) throws IOException { StringWriter writer = null; String contents; try { writer = new StringWriter(); IOUtils.copy(resource.getInputStream(), writer, "UTF-8"); contents = writer.toString(); } finally { if(writer != null) { writer.flush(); writer.close(); } } return contents; }
StringWriter out = new StringWriter(); PrintWriter writer = new PrintWriter(out); ((Closure) data).call(writer); writer.close(); out.flush(); data = out; } else if (data instanceof Writable) { StringWriter out = new StringWriter(); ((Writable) data).writeTo(out); out.flush(); data = out; } else if (data instanceof Reader && !(data instanceof BufferedReader)) { StringWriter out = new StringWriter(); DefaultGroovyMethods.leftShift(out, (BufferedReader) data);
/** * @param resource * @return the UTF-8 String represetation of the contents of the resource */ protected String getResourceContents(Resource resource) throws IOException { StringWriter writer = null; try { writer = new StringWriter(); IOUtils.copy(resource.getInputStream(), writer, "UTF-8"); return writer.toString(); } finally { if (writer != null) { writer.flush(); writer.close(); } } }
@Override public Object render(Map<String, Object> context) { StringWriter writer = new StringWriter(); mustache.execute(writer, context); writer.flush(); return writer; } }
@Override public String toJson(Object value) { StringWriter sw = new StringWriter(); try { JsonGenerator jgen = objectMapper.getFactory().createGenerator(sw); boolean success = serializer.writeObject(value, jgen, true); jgen.flush(); sw.flush(); if (success) { return sw.toString(); } else { return value.toString(); } } catch (IOException e) { throw new RuntimeException(e); } }
@Override public String generate(Request request, int status, String reasonPhrase, String description, Throwable exception) { final ImmutableMap.Builder<String, Object> modelBuilder = ImmutableMap.builder(); modelBuilder.put("reason", StringEscapeUtils.escapeHtml4(reasonPhrase)); if (description != null) { modelBuilder.put("description", StringEscapeUtils.escapeHtml4(description)); } if (exception != null) { String stacktrace = ""; try (final StringWriter stringWriter = new StringWriter(); final PrintWriter printWriter = new PrintWriter(stringWriter)) { exception.printStackTrace(printWriter); printWriter.flush(); stringWriter.flush(); stacktrace = stringWriter.toString(); } catch (IOException ignored) { // Ignore } modelBuilder .put("exception", StringEscapeUtils.escapeHtml4(exception.getMessage())) .put("stacktrace", StringEscapeUtils.escapeHtml4(stacktrace)); } return engine.transform(template, modelBuilder.build()); } }
protected static String loadString(URL url) throws IOException { InputStream is = url.openStream(); if (is == null) { return null; } try { InputStreamReader reader = new InputStreamReader(is); StringWriter writer = new StringWriter(); final char[] buffer = new char[4096]; int n; while ( -1 != ( n = reader.read( buffer ) ) ) { writer.write( buffer, 0, n ); } writer.flush(); return writer.toString(); } finally { is.close(); } }
public String render() { Configuration freemarkerConfiguration = new Configuration(); Map<String, Object> model = new HashMap<>(); model.put("pageSpec", this); try { Template template = new Template("report-main", new InputStreamReader(getClass().getResourceAsStream("/generator/page-spec.gspec.ftl")), freemarkerConfiguration); StringWriter sw = new StringWriter(); template.process(model, sw); sw.flush(); sw.close(); return sw.toString(); } catch (Exception ex) { throw new RuntimeException("Error rendering template", ex); } } }
public String toJsonString() throws IOException { StringWriter stringWriter = new StringWriter(); new JsonWriter(stringWriter).writeMap(properties); stringWriter.flush(); return stringWriter.toString(); }