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:ru.xxlabaza.popa.pack.compress.CompressService.java

public String compress(String content, ContentType type) {
    if (!compressors.containsKey(type)) {
        throw new UnsupportedOperationException("Unsupported content type " + type.name());
    }//w  ww . j av a 2 s . c o  m

    Reader reader = new StringReader(content);
    Writer writer = new StringWriter();
    compressors.get(type).compress(reader, writer);

    return writer.toString();
}

From source file:ru.xxlabaza.popa.template.AbstractTemplater.java

@Override
@SneakyThrows/*from  w  w w  . ja  v a 2  s  . c o m*/
public String build(Path templatePath, Map<String, Object> mappings) {
    if (!isInitialized) {
        lazyInitialization();
        isInitialized = true;
    }
    Writer stringWriter = new StringWriter();
    write(templatePath, mappings, stringWriter);
    return stringWriter.toString();
}

From source file:org.apache.manifoldcf.crawler.connectors.meridio.CommonsHTTPSender.java

private static String getResponseBodyAsString(BackgroundHTTPThread methodThread)
        throws IOException, InterruptedException, HttpException {
    InputStream is = methodThread.getSafeInputStream();
    if (is != null) {
        try {/* w  w  w  . java2  s. c om*/
            String charSet = methodThread.getCharSet();
            if (charSet == null)
                charSet = "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:nz.net.orcon.kanban.automation.plugin.FreemarkerPlugin.java

@Override
public Map<String, Object> process(Action action, Map<String, Object> context) throws Exception {
    String resource = getResourceController().getResource((String) context.get("boardid"),
            action.getResource());/*w ww . j  av a 2 s .  com*/

    Configuration configuration = new Configuration();

    configuration.setEncoding(Locale.ENGLISH, "UTF-8");

    Template template = new Template(action.getResource(), new StringReader(resource), configuration);
    Writer output = new StringWriter();
    template.process(context, output);
    String result = output.toString();
    context.put(action.getResponse(), result);
    return context;
}

From source file:doc.doclets.WorkbenchHelpDoclet.java

/**
 * Generate documentation for all parameter fields of an actor.
 * /*from w  w w . j  a v a2 s  .  c  o  m*/
 * @param classDoc The ClassDoc for the class we are documenting.
 * @param fieldBaseClass The base class for the field we are documenting.
 * @param attrTemplate 
 * @return The documentation for all fields that are derived from the fieldBaseClass parameter.
 */
private static String _generateFieldDocumentation(final ClassDoc classDoc, String attrTemplate)
        throws Exception {
    Writer resultWriter = new StringWriter();
    try {
        Context velocityContext = new VelocityContext();
        velocityContext.put("actorName", _generateShortClassName(classDoc));
        velocityContext.put("allActorAttributes",
                _generateActorAttributeElements(classDoc, Class.forName("ptolemy.data.expr.Parameter")));

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

From source file:org.apache.manifoldcf.connectorcommon.common.CommonsHTTPSender.java

private static String getResponseBodyAsString(BackgroundHTTPThread methodThread)
        throws IOException, InterruptedException, HttpException {
    InputStream is = methodThread.getSafeInputStream();
    if (is != null) {
        try {/*from   w  w w  .j a va  2  s.  co m*/
            Charset charSet = methodThread.getCharSet();
            if (charSet == null)
                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:it.ozimov.springboot.templating.mail.service.PebbleTemplateService.java

@Override
public @NonNull String mergeTemplateIntoString(final @NonNull String templateReference,
        final @NonNull Map<String, Object> model) throws IOException, TemplateException {
    checkArgument(!isNullOrEmpty(templateReference.trim()), "The given templateName is null, empty or blank");
    checkArgument(Objects.equals(getFileExtension(templateReference), expectedTemplateExtension()),
            "Expected a Pebble template file with extension %s, while %s was given. To check "
                    + "the default extension look at 'pebble.suffix' in your application.properties file",
            expectedTemplateExtension(), getFileExtension(templateReference));

    try {/*from   w w w  .ja va 2s .co m*/
        final PebbleTemplate template = pebbleEngine.getTemplate(normalizeTemplateReference(templateReference));
        final Writer writer = new StringWriter();
        template.evaluate(writer, model);
        return writer.toString();
    } catch (PebbleException e) {
        throw new TemplateException(e);
    }
}

From source file:org.jongo.marshall.jackson.JacksonProcessor.java

public <T> String marshall(T obj) throws MarshallingException {
    try {//from   w w w  . ja v  a2  s .  com
        Writer writer = new StringWriter();
        mapper.writeValue(writer, obj);
        return writer.toString();
    } catch (Exception e) {
        String message = String.format("Unable to marshall json from: %s", obj);
        throw new MarshallingException(message, e);
    }
}

From source file:fm.last.peyote.cacti.PeyoteMarshallerTest.java

@Test
public void testGenerateXml() throws Exception {
    HashKeyGenerator hashGenerator = new DummyCactiHashKeyGenerator("0.8.7g");
    InputData inputData = new InputData();
    inputData.setUrl("http://<host>:8888/cacti/value/attribute/countXsMBean/AttributeMap/");
    inputData.setName("service getX requests");
    inputData.setCactiDataItems(new String[] { "xError", "xFound", "xNotFound" });
    PeyoteMarshaller peyote = new PeyoteMarshaller(new DataTemplateGenerator(hashGenerator),
            new GraphTemplateGenerator(hashGenerator));
    peyote.setInputData(inputData);// www .  j  av a  2  s .  co m

    Writer writer = new StringWriter();
    peyote.generateCactiDataTemplate(writer);
    String dataTemplate = writer.toString();
    String xmlDataOriginal = IOUtils
            .toString(new FileReader(new File("src/test/data/cacti_data_template_service_x_requests.xml")));
    assertEquals(xmlDataOriginal, dataTemplate);

    // TODO PD implement graph template, this will test it
    //     writer = new StringWriter();
    //     peyote.generateCactiGraphTemplate(writer);
    //     String graphTemplate = writer.toString();
    //     String xmlGraphOriginal = IOUtils.toString(new FileReader(new File(
    //     "src/test/data/cacti_graph_template_service_x_requests.xml")));
    //     assertEquals(xmlGraphOriginal, graphTemplate);
}

From source file:org.biokoframework.http.fields.impl.JsonFieldsParser.java

@Override
public Fields safelyParse(HttpServletRequest request) throws RequestNotSupportedException {
    Reader reader;//from w w w .j  a va 2s  .  co m
    try {
        if (request.getCharacterEncoding() == null) {
            request.setCharacterEncoding("utf-8");
        }
        reader = request.getReader();
        Writer writer = new StringWriter();
        IOUtils.copy(reader, writer);
        return Fields.fromJson(writer.toString());
    } catch (IOException exception) {
        // TODO log exception
        throw new RequestNotSupportedException(exception);
    }
}