Example usage for java.io CharArrayWriter writeTo

List of usage examples for java.io CharArrayWriter writeTo

Introduction

In this page you can find the example usage for java.io CharArrayWriter writeTo.

Prototype

public void writeTo(Writer out) throws IOException 

Source Link

Document

Writes the contents of the buffer to another character stream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    String str = "from java2s.com!";

    CharArrayWriter chw = new CharArrayWriter();

    // create destination character array writer
    CharArrayWriter chwd = new CharArrayWriter();

    chw.write(str);/*w w w.  j  av a2 s.c o  m*/

    chw.writeTo(chwd);

    // print the destination buffer content as string
    System.out.println(chwd.toString());

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    String str = "from java2s.com!";

    CharArrayWriter chw = new CharArrayWriter();

    // create destination character array writer
    CharArrayWriter chwd = new CharArrayWriter();

    chw.write(str);//from  ww  w .  j  ava  2s.co m

    chw.writeTo(chwd);

    // print the destination buffer content as string
    System.out.println(chwd.toString());
    System.out.println(chwd.size());

}

From source file:CharArrayWriterDemo.java

public static void main(String args[]) throws IOException {
    CharArrayWriter f = new CharArrayWriter();
    String s = "This should end up in the array";
    char buf[] = new char[s.length()];
    s.getChars(0, s.length(), buf, 0);//from ww w.  jav a2s .com
    f.write(buf);
    System.out.println(f.toString());

    char c[] = f.toCharArray();
    for (int i = 0; i < c.length; i++) {
        System.out.print(c[i]);
    }

    FileWriter f2 = new FileWriter("test.txt");
    f.writeTo(f2);
    f2.close();
    f.reset();
    for (int i = 0; i < 3; i++)
        f.write('X');
}

From source file:org.apache.struts2.views.freemarker.FreemarkerResult.java

/**
 * Execute this result, using the specified template locationArg.
 * <p/>//from  www  .ja va2  s  . c o m
 * The template locationArg has already been interoplated for any variable substitutions
 * <p/>
 * this method obtains the freemarker configuration and the object wrapper from the provided hooks.
 * It them implements the template processing workflow by calling the hooks for
 * preTemplateProcess and postTemplateProcess
 */
public void doExecute(String locationArg, ActionInvocation invocation) throws IOException, TemplateException {
    this.location = locationArg;
    this.invocation = invocation;
    this.configuration = getConfiguration();
    this.wrapper = getObjectWrapper();

    ActionContext ctx = invocation.getInvocationContext();
    HttpServletRequest req = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);

    if (!locationArg.startsWith("/")) {
        String base = ResourceUtil.getResourceBase(req);
        locationArg = base + "/" + locationArg;
    }

    Template template = configuration.getTemplate(locationArg, deduceLocale());
    TemplateModel model = createModel();

    // Give subclasses a chance to hook into preprocessing
    if (preTemplateProcess(template, model)) {
        try {
            // Process the template
            Writer writer = getWriter();
            if (isWriteIfCompleted() || configuration
                    .getTemplateExceptionHandler() == TemplateExceptionHandler.RETHROW_HANDLER) {
                CharArrayWriter parentCharArrayWriter = (CharArrayWriter) req
                        .getAttribute(PARENT_TEMPLATE_WRITER);
                boolean isTopTemplate = false;
                if (isTopTemplate = (parentCharArrayWriter == null)) {
                    //this is the top template
                    parentCharArrayWriter = new CharArrayWriter();
                    //set it in the request because when the "action" tag is used a new VS and ActionContext is created
                    req.setAttribute(PARENT_TEMPLATE_WRITER, parentCharArrayWriter);
                }

                try {
                    template.process(model, parentCharArrayWriter);

                    if (isTopTemplate) {
                        parentCharArrayWriter.flush();
                        parentCharArrayWriter.writeTo(writer);
                    }
                } catch (TemplateException e) {
                    if (LOG.isErrorEnabled()) {
                        LOG.error("Error processing Freemarker result!", e);
                    }
                    throw e;
                } catch (IOException e) {
                    if (LOG.isErrorEnabled()) {
                        LOG.error("Error processing Freemarker result!", e);
                    }
                    throw e;
                } finally {
                    if (isTopTemplate && parentCharArrayWriter != null) {
                        req.removeAttribute(PARENT_TEMPLATE_WRITER);
                        parentCharArrayWriter.close();
                    }
                }
            } else {
                template.process(model, writer);
            }
        } finally {
            // Give subclasses a chance to hook into postprocessing
            postTemplateProcess(template, model);
        }
    }
}

From source file:com.aliyun.odps.rodps.ROdps.java

public boolean setLogPath(String log_path) throws IOException {
    String fileName = ROdps.class.getClassLoader().getResource("log4j.properties").getPath();
    String mode = "loghome";
    File file = new File(fileName);
    BufferedReader reader = null;
    try {//from www  .j  a v a2s  . co  m
        reader = new BufferedReader(new FileReader(file));
        CharArrayWriter tempStream = new CharArrayWriter();
        String tempString = null;
        int line = 1;
        while ((tempString = reader.readLine()) != null) {
            if (tempString.contains(mode) && (!tempString.contains("${" + mode + "}"))) {
                tempString = tempString.substring(0, tempString.indexOf('=') + 1) + log_path;
            }
            tempStream.write(tempString);
            tempStream.append(System.getProperty("line.separator"));
        }
        reader.close();
        FileWriter out = new FileWriter(fileName);
        tempStream.writeTo(out);
        out.close();

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e1) {
            }
        }
    }
    return true;
}

From source file:com.snowplowanalytics.snowplow.collectors.clojure.SnowplowAccessLogValve.java

/**
 * Log the specified message to the log file, switching files if the date
 * has changed since the previous log call.
 *
 * @param message Message to be logged/*from  w  w  w .  ja  v a  2 s. co  m*/
 */
@Override
public void log(CharArrayWriter message) {

    rotate();

    /* In case something external rotated the file instead */
    if (checkExists) {
        synchronized (this) {
            if (currentLogFile != null && !currentLogFile.exists()) {
                try {
                    close(false);
                } catch (Throwable e) {
                    ExceptionUtils.handleThrowable(e);
                    log.info(sm.getString("accessLogValve.closeFail"), e);
                }

                /* Make sure date is correct */
                dateStamp = fileDateFormatter.format(new Date(System.currentTimeMillis()));

                open();
            }
        }
    }

    // Log this message
    try {
        synchronized (this) {
            if (writer != null) {
                message.writeTo(writer);
                writer.println("");
                if (!buffered) {
                    writer.flush();
                }
            }
        }
    } catch (IOException ioe) {
        log.warn(sm.getString("accessLogValve.writeFail", message.toString()), ioe);
    }
}