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.zenoss.zep.dao.impl.DaoUtils.java

private static String convertStreamToStr(InputStream is) throws IOException {
    if (is != null) {
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {// www . ja va 2 s. c o  m
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer, 0, 1024)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:Main.java

public static String getStringFromFile(String pathUrlName, boolean encode) {
    Writer writer = null;
    try {/*w  ww  .  j  av a2 s  .c o m*/
        InputStream is = new FileInputStream(new File(pathUrlName));

        writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader;
            if (encode) {
                reader = new BufferedReader(new InputStreamReader(is, HTML_ENCODING));
            } else {
                reader = new BufferedReader(new InputStreamReader(is));
            }
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            is.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return writer.toString();
}

From source file:doc.doclets.WorkbenchHelpDoclet.java

protected static void generateActorHelpTOC(final RootDoc root, final Collection<ClassDoc> classes)
        throws IOException, ClassNotFoundException {
    String template = FileUtils.readFileToString(new File(
            "C:\\data\\development\\workspaces\\isencia-workspaces\\passerelle-V7\\com.isencia.passerelle.doc.generator\\src\\main\\java\\tocTemplate.vm"));
    Map<String, String> actorClassNameMap = new TreeMap<String, String>();
    for (ClassDoc classDoc : classes) {
        actorClassNameMap.put(_generateShortClassName(classDoc), _generateClassName(classDoc));
    }/*www.  j a  v a2  s .com*/
    Writer resultWriter = new StringWriter();
    try {
        Context velocityContext = new VelocityContext();
        velocityContext.put("allActorClasses", actorClassNameMap);

        velocity.evaluate(velocityContext, resultWriter, "TOC template", template);
        String res = resultWriter.toString();
        _writeTOC(res);
    } finally {
        try {
            resultWriter.close();
        } catch (Exception e) {
        }
    }
}

From source file:doc.doclets.WorkbenchHelpDoclet.java

protected static void generateActorHelpHtmlTOC(final RootDoc root, final Collection<ClassDoc> classes)
        throws IOException, ClassNotFoundException {
    String template = FileUtils.readFileToString(new File(
            "C:\\data\\development\\workspaces\\isencia-workspaces\\passerelle-V7\\com.isencia.passerelle.doc.generator\\src\\main\\java\\htmlTocTemplate.vm"));
    Map<String, String> actorClassNameMap = new TreeMap<String, String>();
    for (ClassDoc classDoc : classes) {
        actorClassNameMap.put(_generateShortClassName(classDoc), _generateClassName(classDoc));
    }/*from  www. j  a va 2  s . c  o m*/
    Writer resultWriter = new StringWriter();
    try {
        Context velocityContext = new VelocityContext();
        velocityContext.put("allActorClasses", actorClassNameMap);

        velocity.evaluate(velocityContext, resultWriter, "TOC template", template);
        String res = resultWriter.toString();
        _writeHtmlTOC(res);
    } finally {
        try {
            resultWriter.close();
        } catch (Exception e) {
        }
    }
}

From source file:doc.doclets.WorkbenchHelpDoclet.java

protected static void generateActorHelpIndex(final RootDoc root, final Collection<ClassDoc> classes)
        throws IOException, ClassNotFoundException {
    String template = FileUtils.readFileToString(new File(
            "C:\\data\\development\\workspaces\\isencia-workspaces\\passerelle-V7\\com.isencia.passerelle.doc.generator\\src\\main\\java\\indexTemplate.vm"));
    Map<String, String> actorClassNameMap = new TreeMap<String, String>();
    for (ClassDoc classDoc : classes) {
        actorClassNameMap.put(_generateShortClassName(classDoc), _generateClassName(classDoc));
    }/*from ww  w  .jav a 2s. c  o m*/
    Writer resultWriter = new StringWriter();
    try {
        Context velocityContext = new VelocityContext();
        velocityContext.put("allActorClasses", actorClassNameMap);

        velocity.evaluate(velocityContext, resultWriter, "index template", template);
        String res = resultWriter.toString();
        _writeIndex(res);
    } finally {
        try {
            resultWriter.close();
        } catch (Exception e) {
        }
    }
}

From source file:org.clickframes.VelocityHelper.java

public static String resolveText(Map<String, Object> params, String text) {
    try {//from  www  .j  a va  2  s .  c  om
        initVelocity(null);
    } catch (Exception e1) {
        throw new RuntimeException("Could not initialize velocity", e1);
    }

    VelocityContext ctx = new VelocityContext();
    if (params != null) {
        for (String key : params.keySet()) {
            ctx.put(key, params.get(key));
        }
    }

    Writer w = new StringWriter();
    try {
        Velocity.evaluate(ctx, w, null, text);
    } catch (ParseErrorException e1) {
        throw new RuntimeException("Error while executing velocity for text: '" + text + "'", e1);
    } catch (MethodInvocationException e1) {
        throw new RuntimeException("Error while executing velocity", e1);
    } catch (ResourceNotFoundException e1) {
        throw new RuntimeException("Error while executing velocity", e1);
    } catch (IOException e1) {
        throw new RuntimeException("Error while executing velocity", e1);
    }

    try {
        w.close();
    } catch (IOException e) {
        throw new RuntimeException("Very unpredicable error", e);
    }

    return w.toString();
}

From source file:XmlWriter.java

static public void test1() throws IOException {

    Writer writer = new java.io.StringWriter();
    XmlWriter xmlwriter = new XmlWriter(writer);
    xmlwriter.writeElement("person").writeAttribute("name", "fred").writeAttribute("age", "12")
            .writeElement("phone").writeText("4254343").endElement().writeElement("friends").writeElement("bob")
            .endElement().writeElement("jim").endElement().endElement().endElement();
    xmlwriter.close();//from   www .ja  v  a 2  s .c om
    System.err.println(writer.toString());
}

From source file:com.bluetooth.activities.WiFiControl.java

/**
 * This function converts an InputStream in a String, this is needed to
 * serve the .htm file to the client./*w w w .j  a v a  2  s.  c o m*/
 * <p>
 * To convert the InputStream to String we use the Reader.read(char[]
 * buffer) method. We iterate until the Reader return -1 which means there's
 * no more data to read. We use the StringWriter class to produce the
 * string.
 */
protected static String openHTMLString(Context context, int id) {
    InputStream is = context.getResources().openRawResource(id);
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return writer.toString();
    } else {
        return "";
    }
}

From source file:com.ebay.jetstream.epl.EPLUtilities.java

/**
 * transformToJsonString - This method converts from incoming object to JSON string. Need/use of this method is, in
 * EPL we need to use pass Objects through window. But EPL allows primitives type alone. For that we 'll convert
 * Obejct to JsonString and pass it to the stream.
 * //  w ww. ja  v  a2  s.  c o m
 * @param obj
 * @return
 */
public static String transformToJsonString(Object obj) throws Exception {
    if (obj != null) {
        ObjectMapper mapper = new ObjectMapper();
        Writer writer = new StringWriter();
        mapper.writeValue(writer, obj);
        return writer.toString();
    }
    return null;
}

From source file:org.jongo.marshall.jackson.configuration.MappingTest.java

@Test
public void shouldNotAddBsonConfWithCustomMapper() throws Exception {
    Mapping.Builder builder = new Mapping.Builder(new ObjectMapper());
    Mapping mapping = builder.build();//from ww  w .j  av a  2  s .  c o m
    ObjectId id = ObjectId.get();//serialized using bson serializer
    Friend friend = new Friend(id, "John");

    Writer writer = new StringWriter();
    mapping.getWriter(friend).writeValue(writer, friend);

    assertThat(writer.toString()).contains("John");
}