StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, encoding); String theString = writer.toString();
StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); sw.toString(); // stack trace as a string
public static String toString(Throwable e) { StringWriter w = new StringWriter(); PrintWriter p = new PrintWriter(w); p.print(e.getClass().getName() + ": "); if (e.getMessage() != null) { p.print(e.getMessage() + "\n"); } p.println(); try { e.printStackTrace(p); return w.toString(); } finally { p.close(); } }
@Test public void readCorrect() throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); StAXSource source = new StAXSource(streamReader); StringWriter writer = new StringWriter(); transformer.transform(source, new StreamResult(writer)); Predicate<Node> nodeFilter = n -> n.getNodeType() != Node.DOCUMENT_TYPE_NODE && n.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE; assertThat(writer.toString(), isSimilarTo(XML).withNodeFilter(nodeFilter)); }
public static String read(Reader reader) { try { StringWriter writer = new StringWriter(); char[] buffer = new char[DEFAULT_BUFFER_SIZE]; int n = 0; while (-1 != (n = reader.read(buffer))) { writer.write(buffer, 0, n); } return writer.toString(); } catch (IOException ex) { throw new IllegalStateException("read error", ex); } }
@Test public void copyFromReader() throws IOException { String content = "content"; StringReader in = new StringReader(content); StringWriter out = new StringWriter(); int count = FileCopyUtils.copy(in, out); assertEquals(content.length(), count); assertEquals(content, out.toString()); }
@Test public void printStackTrace() { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); new CompositeException(new TestException()).printStackTrace(pw); assertTrue(sw.toString().contains("TestException")); }
private static String toString(org.w3c.dom.Node node) { try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); DOMSource domSource = new DOMSource(node); StringWriter out = new StringWriter(); transformer.transform(domSource, new StreamResult(out)); return out.toString(); } catch (TransformerException e) { throw new JSONException("xml node to string error", e); } }
public static String toString(Throwable e) { StringWriter w = new StringWriter(); PrintWriter p = new PrintWriter(w); p.print(e.getClass().getName() + ": "); if (e.getMessage() != null) { p.print(e.getMessage() + "\n"); } p.println(); try { e.printStackTrace(p); return w.toString(); } finally { p.close(); } }
@Test public void namespacePrefixes() throws Exception { Assume.assumeTrue(wwwSpringframeworkOrgIsAccessible()); StringWriter stringWriter = new StringWriter(); AbstractStaxHandler handler = createStaxHandler(new StreamResult(stringWriter)); xmlReader.setContentHandler(handler); xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); xmlReader.setFeature("http://xml.org/sax/features/namespaces", true); xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); xmlReader.parse(new InputSource(new StringReader(COMPLEX_XML))); assertThat(stringWriter.toString(), isSimilarTo(COMPLEX_XML).withNodeFilter(nodeFilter)); }
public static String getStackTrace(Throwable ex) { StringWriter buf = new StringWriter(); ex.printStackTrace(new PrintWriter(buf)); return buf.toString(); }
/** Copy the data from an {@link InputStream} to a string using the specified charset. * @param estimatedSize Used to allocate the output buffer to possibly avoid an array copy. * @param charset May be null to use the platform's default charset. */ public static String copyStreamToString (InputStream input, int estimatedSize, String charset) throws IOException { InputStreamReader reader = charset == null ? new InputStreamReader(input) : new InputStreamReader(input, charset); StringWriter writer = new StringWriter(Math.max(0, estimatedSize)); char[] buffer = new char[DEFAULT_BUFFER_SIZE]; int charsRead; while ((charsRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charsRead); } return writer.toString(); }
@Test public void marshalStaxResultXMLStreamWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); Result result = StaxUtils.createStaxResult(streamWriter); marshaller.marshal(flight, result); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }
@Test public void testExceptionStackTrace() { JMSException jmsEx = new JMSException("could not connect"); Exception innerEx = new Exception("host not found"); jmsEx.setLinkedException(innerEx); JmsException springJmsEx = JmsUtils.convertJmsAccessException(jmsEx); StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); springJmsEx.printStackTrace(out); String trace = sw.toString(); assertTrue("inner jms exception not found", trace.indexOf("host not found") > 0); }
TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); String str = buffer.toString();
private static String formatThrowable(Throwable t) { StringWriter buf = new StringWriter(); PrintWriter writer = new PrintWriter(buf); t.printStackTrace(); writer.flush(); return buf.toString(); }
@Test public void noNamespacePrefixes() throws Exception { Assume.assumeTrue(wwwSpringframeworkOrgIsAccessible()); StringWriter stringWriter = new StringWriter(); AbstractStaxHandler handler = createStaxHandler(new StreamResult(stringWriter)); xmlReader.setContentHandler(handler); xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); xmlReader.setFeature("http://xml.org/sax/features/namespaces", true); xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); xmlReader.parse(new InputSource(new StringReader(COMPLEX_XML))); assertThat(stringWriter.toString(), isSimilarTo(COMPLEX_XML).withNodeFilter(nodeFilter)); }
public String getLastStatementStatckTrace() { if (lastStatementStatckTraceException == null) { return null; } StringWriter buf = new StringWriter(); lastStatementStatckTraceException.printStackTrace(new PrintWriter(buf)); return buf.toString(); }
/** Copy the data from an {@link InputStream} to a string using the specified charset. * @param estimatedSize Used to allocate the output buffer to possibly avoid an array copy. * @param charset May be null to use the platform's default charset. */ public static String copyStreamToString (InputStream input, int estimatedSize, String charset) throws IOException { InputStreamReader reader = charset == null ? new InputStreamReader(input) : new InputStreamReader(input, charset); StringWriter writer = new StringWriter(Math.max(0, estimatedSize)); char[] buffer = new char[DEFAULT_BUFFER_SIZE]; int charsRead; while ((charsRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charsRead); } return writer.toString(); }
@Test public void marshalStaxResultStreamWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); Result result = StaxUtils.createStaxResult(streamWriter); marshaller.marshal(flights, result); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }