Refine search
/** * read string. * * @param reader Reader instance. * @return String. * @throws IOException */ public static String read(Reader reader) throws IOException { StringWriter writer = new StringWriter(); try { write(reader, writer); return writer.getBuffer().toString(); } finally { writer.close(); } }
public static String getClassicStackTrace( Throwable e ) { StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter( stringWriter ); e.printStackTrace( printWriter ); String string = stringWriter.toString(); try { stringWriter.close(); } catch ( IOException ioe ) { // is this really required? } return string; }
public static String serialize(Properties properties) throws IOException { StringWriter outputWriter = new StringWriter(); properties.store(outputWriter, ""); String rst = outputWriter.toString(); outputWriter.close(); return rst; }
/** * read string. * * @param reader Reader instance. * @return String. * @throws IOException */ public static String read(Reader reader) throws IOException { StringWriter writer = new StringWriter(); try { write(reader, writer); return writer.getBuffer().toString(); } finally { writer.close(); } }
/** * Set the Throwable associated with this LogRecord. When this method * is called, the stack trace in a String-based format is made * available via the getThrownStackTrace() method. * * @param thrown A Throwable to associate with this LogRecord. * @see #getThrown() * @see #getThrownStackTrace() */ public void setThrown(Throwable thrown) { if (thrown == null) { return; } _thrown = thrown; StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); thrown.printStackTrace(out); out.flush(); _thrownStackTrace = sw.toString(); try { out.close(); sw.close(); } catch (IOException e) { // Do nothing, this should not happen as it is StringWriter. } out = null; sw = null; }
/** * json string. * * @param obj object. * @return json string. * @throws IOException */ public static String json(Object obj) throws IOException { if (obj == null) { return NULL; } StringWriter sw = new StringWriter(); try { json(obj, sw); return sw.getBuffer().toString(); } finally { sw.close(); } }
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; }
/** * json string. * * @param obj object. * @return json string. * @throws IOException */ public static String json(Object obj) throws IOException { if (obj == null) { return NULL; } StringWriter sw = new StringWriter(); try { json(obj, sw); return sw.getBuffer().toString(); } finally { sw.close(); } }
private String readResource(String name) { StringWriter sw = new StringWriter(); InputStreamReader reader = new InputStreamReader(ClassLoader.getSystemResourceAsStream(name)); char[] chars = new char[1024]; int read; try { while ((read = reader.read(chars)) != -1) { sw.write(chars, 0, read); } sw.close(); } catch (IOException e) { throw new RuntimeException(e); } return sw.toString(); }
/** * json string. * * @param obj object. * @param properties property name array. * @return json string. * @throws IOException */ public static String json(Object obj, String[] properties) throws IOException { if (obj == null) { return NULL; } StringWriter sw = new StringWriter(); try { json(obj, properties, sw); return sw.getBuffer().toString(); } finally { sw.close(); } }
/** * @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(); } } }
/** * json string. * * @param obj object. * @param properties property name array. * @return json string. * @throws IOException */ public static String json(Object obj, String[] properties) throws IOException { if (obj == null) { return NULL; } StringWriter sw = new StringWriter(); try { json(obj, properties, sw); return sw.getBuffer().toString(); } finally { sw.close(); } }
/** * Format a stack trace in a human-readable format. * * @param throwable The exception/throwable whose stack trace to format. */ @Nullable public static String formatStacktrace(Throwable throwable) { final StringWriter stringWriter = new StringWriter(); final PrintWriter printWriter = new PrintWriter(stringWriter); String output = null; try { throwable.printStackTrace(printWriter); } finally { printWriter.close(); try { output = stringWriter.toString(); stringWriter.close(); } catch (final IOException ignored) { // This would mean that closing failed which doesn't concern us. } } return output; } }
public static String getStackTrace(Throwable t) { String stackTrace = null; try { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw); pw.close(); sw.close(); stackTrace = sw.getBuffer().toString(); } catch (Exception ex) { } return stackTrace; }
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); } } }
@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); } }
/** * Converts a stack trace to string output. * * @param t the stack trace to convert. * * @return a string version of the stack trace. */ public static String stackTraceToString(final Throwable t) { final StringWriter stringWriter = new StringWriter(); final PrintWriter printWriter = new PrintWriter(stringWriter, true); t.printStackTrace(printWriter); printWriter.flush(); stringWriter.flush(); printWriter.close(); try { stringWriter.close(); } catch (IOException e) { // Do nothing } return stringWriter.toString(); }
public static String createJsonFrom( Object data ) throws JsonBuildRuntimeException { try { StringWriter writer = new StringWriter(); try { JsonGenerator generator = OBJECT_MAPPER.getJsonFactory() .createJsonGenerator( writer ) .useDefaultPrettyPrinter(); writeValue( generator, data ); } finally { writer.close(); } return writer.getBuffer().toString(); } catch ( IOException e ) { throw new JsonBuildRuntimeException( e ); } }