Example usage for java.io Writer toString

List of usage examples for java.io Writer toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.maodian.flyingcat.xmpp.state.SASLCommandTest.java

@Test
public void testPlainMechanismSuccess() throws Exception {
    String inXML = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>AGp1bGlldAByMG0zMG15cjBtMzA=</auth>";

    Writer writer = new StringWriter();
    XMLStreamReader xmlsr1 = newXMLStreamReader(new StringReader(inXML));
    XMLStreamWriter xmlsw = newXMLStreamWriter(writer);
    // skipt start document
    xmlsr1.nextTag();//  w  ww .j  a  v  a  2 s.  com
    State state = cmd.execute(xmlsr1, xmlsw);

    Reader reader = new StringReader(writer.toString());
    XMLStreamReader xmlsr = XMLInputFactoryHolder.getXMLInputFactory().createXMLStreamReader(reader);

    xmlsr.next();
    QName qname = new QName(XmppNamespace.SASL, "success");
    assertEquals(qname, xmlsr.getName());
    assertTrue(state instanceof AuthenticatedStreamState);
}

From source file:br.com.autonomiccs.autonomic.plugin.common.utils.ShellCommandUtils.java

/**
 * It executes the specified shell command and wait for the
 * end of command execution to continue with the application
 * flow./*from  w w w  .  j a  v a  2s.c o m*/
 *
 * If an exception happens, it will get logged and the flow of execution continues.
 * This method will not break the flow of execution if an expected exception happens.
 *
 * @param command
 *            The command that will be executed.
 * @return
 *         A <code>String</code> that is the result from
 *         command executed.
 */
public String executeCommand(String command) {
    Writer output = new StringWriter();
    try {
        Process p = Runtime.getRuntime().exec(command);
        p.waitFor();
        IOUtils.copy(p.getInputStream(), output);
    } catch (IOException | InterruptedException e) {
        logger.error(String.format("An error happened while executing command[%s]", command), e);
    }
    return output.toString();
}

From source file:ddf.catalog.transformer.input.tika.TikaInputTransformer.java

private String transformToXml(String xhtml) {
    LOGGER.debug("Transforming xhtml to xml.");
    try {/*  w w w .  j a  va2s  . c  om*/
        Writer xml = new StringWriter();
        Transformer transformer = templates.newTransformer();
        transformer.transform(new StreamSource(new StringReader(xhtml)), new StreamResult(xml));
        return xml.toString();
    } catch (TransformerException e) {
        LOGGER.warn("Unable to transform metadata from XHTML to XML.", e);
        return xhtml;
    }
}

From source file:org.apache.manifoldcf.scriptengine.ScriptParser.java

public static String convertToString(HttpResponse httpResponse) throws IOException {
    HttpEntity entity = httpResponse.getEntity();
    if (entity != null) {
        InputStream is = entity.getContent();
        try {//from w  w w .  ja v a 2 s.com
            Charset charSet;
            try {
                ContentType ct = ContentType.get(entity);
                if (ct == null)
                    charSet = StandardCharsets.UTF_8;
                else
                    charSet = ct.getCharset();
            } catch (ParseException e) {
                charSet = StandardCharsets.UTF_8;
            }
            char[] buffer = new char[65536];
            Reader r = new InputStreamReader(is, charSet);
            Writer w = new StringWriter();
            try {
                while (true) {
                    int amt = r.read(buffer);
                    if (amt == -1)
                        break;
                    w.write(buffer, 0, amt);
                }
            } finally {
                w.flush();
            }
            return w.toString();
        } finally {
            is.close();
        }
    }
    return "";
}

From source file:com.eclipsesource.jaxrs.consumer.example.GitHubUserProvider.java

public String convertStreamToString(InputStream is) throws IOException {
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {//from   w ww . ja va  2s.co m
        Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }
    return writer.toString();
}

From source file:org.gytheio.messaging.amqp.AmqpDirectEndpoint.java

public void send(Object message, String queueName) {
    try {//w w w  . ja  v a 2 s .  c  o m
        Writer strWriter = new StringWriter();
        objectMapper.writeValue(strWriter, message);
        String stringMessage = strWriter.toString();

        if (StringUtils.isEmpty(queueName)) {
            queueName = sendEndpoint;
        }

        TextMessage textMessage = getProducerSession().createTextMessage(stringMessage);

        if (logger.isTraceEnabled()) {
            logger.trace("Sending message to " + host + ":" + queueName + ": " + stringMessage);
        }
        getMessageProducer(queueName).send(textMessage);
    } catch (Exception e) {
        throw new MessagingException("Error sending message", e);
    }
}

From source file:RssAtomGenerationTest.java

@Test
public void testRssChannelGeneration() throws Exception {

    Channel channel = new Channel("rss_2.0");
    channel.setTitle("test");
    channel.setDescription("description");
    channel.setPubDate(Calendar.getInstance().getTime());
    channel.setLink("http://www.google.com");

    channel.setLanguage("nl");
    channel.setManagingEditor("managingeditor");
    channel.setWebMaster("webmaster");
    //channel.setCategories(Arrays.asList(new String[]{"test1, test2"}));
    channel.setGenerator("generator");
    channel.setCopyright("copyright");

    Writer writer = new StringWriter();
    WireFeedOutput output = new WireFeedOutput();
    output.output(channel, writer);//from   w w  w  .j av  a 2s .  com
    System.out.println(writer.toString());

    final SyndFeedImpl syndFeed = new SyndFeedImpl(channel, true);

    Writer writer1 = new StringWriter();
    SyndFeedOutput syndFeedOutput = new SyndFeedOutput();
    syndFeedOutput.output(syndFeed, writer1);

    System.out.println(writer1.toString());

}

From source file:java2typescript.jackson.module.StaticFieldExporterTest.java

@Test
public void testTypeScriptDefinition() throws IOException, IllegalArgumentException {
    Writer out = new StringWriter();

    ArrayList<Class<?>> classesToConvert = new ArrayList<Class<?>>();
    classesToConvert.add(TestClass.class);

    Module module = new Module("mod");
    new StaticFieldExporter(module, null).export(classesToConvert);

    module.write(out);//from   ww w . ja v  a  2  s.c om

    out.close();
    final String result = out.toString();
    System.out.println(result);
    assertTrue(result.contains("export class TestClassStatic"));
    assertTrue(result.contains("export enum ChangedEnumName"));
    assertTrue(result.contains("static MY_CONSTANT_STRING: string = 'Test';"));
    assertTrue(result.contains(
            "static MY_CONSTANT_ENUM_ARRAY_2: ChangedEnumName[] = [ ChangedEnumName.VAL1, ChangedEnumName.VAL2 ];"));
    assertFalse(result.contains("doNotExportAsStatic"));
}

From source file:de.kp.ames.web.core.rss.RssConsumer.java

/**
 * Get RSS feed in native representation
 * /*from www  . ja  v  a2 s.c om*/
 * @param method
 * @return
 */
public String getFeed(String method) {

    try {
        SyndFeed feed = execute(method);

        Writer writer = new StringWriter();

        SyndFeedOutput output = new SyndFeedOutput();
        output.output(feed, writer);

        String content = writer.toString();
        writer.close();

        return content;

    } catch (Exception e) {
        e.printStackTrace();

    } finally {
    }

    return null;

}

From source file:doc.doclets.WorkbenchHelpDoclet.java

private static String _generateClassLevelDocumentation(ClassDoc classDoc, String actorHelpTemplate)
        throws Exception {
    Writer resultWriter = new StringWriter();
    try {//from w  w  w .  j  av a  2s .  c  o  m
        Context velocityContext = new VelocityContext();
        velocityContext.put("actorName", _generateShortClassName(classDoc));
        velocityContext.put("actorClassName", _generateClassName(classDoc));
        velocityContext.put("allDocElements", _generateClassLevelDocumentationElements(classDoc));
        velocityContext.put("actorClassDoc", _inlineTagCommentText(classDoc));
        velocityContext.put("allActorAttributes",
                _generateActorAttributeElements(classDoc, Class.forName("ptolemy.data.expr.Parameter")));

        velocity.evaluate(velocityContext, resultWriter, "actor help template", actorHelpTemplate);
        return resultWriter.toString();
    } finally {
        try {
            resultWriter.close();
        } catch (Exception e) {
        }
    }
}