Example usage for java.io StringWriter write

List of usage examples for java.io StringWriter write

Introduction

In this page you can find the example usage for java.io StringWriter write.

Prototype

public void write(String str) 

Source Link

Document

Write a string.

Usage

From source file:com.rhfung.P2PDictionary.DataConnection.java

private String GetDictionaryAsTextFile() {
    StringWriter writer = new StringWriter();

    List<DataEntry> entries; // make a local copy for access without worry about changes thereafter
    this.dataLock.readLock().lock();
    try {/*ww w  .  j ava2 s.  c  om*/
        entries = new Vector<DataEntry>(this.data.size());
        //entries.AddRange(this.data.Values.Where(x => x.subscribed));
        entries.addAll(this.data.values());
    } finally {
        this.dataLock.readLock().unlock();
    }

    // write count of data entries
    writer.write(DATA_NAMESPACE + "/\t" + this.local_uid + "\t0\tRW\t" + entries.size() + "\t" + this.local_uid
            + NEWLINE);

    // write each data entry, converting simple data immediately
    // (pretend i don't know about these non-subscribed entries)
    for (DataEntry d : entries) {
        String permissions = "";

        if (d.subscribed) {

            if (DataMissing.isSingleton(d.value)) {
                permissions = "-W";
            } else {
                permissions = "RW";
            }
        } else {

            if (DataMissing.isSingleton(d.value)) {
                permissions = "--";
            } else {
                permissions = "=-";

            }

        }

        writer.write(d.key + "\t" + d.lastOwnerID + "\t" + d.lastOwnerRevision + "\t" + permissions + "\t"
                + d.getMime() + d.GetMimeSimpleData() + "\t" + GetStringOf(d.senderPath) + NEWLINE);

    }

    return writer.toString();
}

From source file:org.kawanfw.sql.jdbc.ResultSetHttp.java

/**
 * Retrieves the value of the designated column in the current row of this
 * <code>ResultSet</code> object as a <code>java.io.Reader</code> object.
 * /*from www  . j a va 2s . co m*/
 * @return a <code>java.io.Reader</code> object that contains the column
 *         value; if the value is SQL <code>NULL</code>, the value returned
 *         is <code>null</code> in the Java programming language.
 * @param columnIndex
 *            the first column is 1, the second is 2, ...
 * @exception SQLException
 *                if the columnIndex is not valid; if a database access
 *                error occurs or this method is called on a closed result
 *                set
 * @since 1.2
 */
@Override
public java.io.Reader getCharacterStream(int columnIndex) throws SQLException {
    testIfClosed();

    String value = getValueOfList(columnIndex, true);

    if (value == null) {
        // Not sure what to do
        throw new SQLException("Column Index is out of bound: " + columnIndex);
    }

    Reader reader = null;

    // Check if we must get the byte array from an input stream
    if (value.startsWith(TransportConverter.KAWANFW_BYTES_STREAM_FILE)) {
        String remoteFile = StringUtils.substringAfter(value, TransportConverter.KAWANFW_BYTES_STREAM_FILE);
        reader = getReaderFromRemoteFile(remoteFile);
    } else {
        String stringValue = getString(columnIndex);

        debug("Reader in String!");

        if (stringValue == null) {
            return null;
        } else {
            try {
                // Put back clean CR_LF
                BufferedReader bufferedReader = new BufferedReader(new StringReader(stringValue));
                StringWriter stringWriter = new StringWriter();

                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    stringWriter.write(line + CR_LF);
                }

                String cleaned = stringWriter.toString();
                reader = new StringReader(cleaned);

            } catch (Exception e) {
                throw new SQLException(e.getMessage(), e);
            }
        }

    }

    return reader;
}

From source file:org.apache.axis2.wsdl.codegen.emitter.AxisServiceBasedMultiLanguageEmitter.java

/**
 * debugging method - write the output to the debugger
 *
 * @param description/*from www.  java 2  s.  c  o  m*/
 * @param doc
 */
protected void debugLogDocument(String description, Document doc) {
    if (log.isDebugEnabled()) {
        try {
            DOMSource source = new DOMSource(doc);
            StringWriter swrite = new StringWriter();
            swrite.write(description);
            swrite.write("\n");
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty("omit-xml-declaration", "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(source, new StreamResult(swrite));

            log.debug(swrite.toString());

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

From source file:org.apache.maven.plugin.javadoc.AbstractFixJavadocMojo.java

/**
 * Add in the buffer a default Javadoc for the given class:
 * <br/>//from w w w.  jav a  2s .c  o  m
 * <code>
 * <font color="#808080">1</font>&nbsp;<font color="#ffffff">&nbsp;</font>
 * <font color="#3f5fbf">&#47;&#42;&#42;</font><br />
 * <font color="#808080">2</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&nbsp;{Comment&nbsp;based&nbsp;on&nbsp;the&nbsp;method&nbsp;name}</font><br />
 * <font color="#808080">3</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;</font><br />
 * <font color="#808080">4</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&nbsp;</font><font color="#7f9fbf">@param&nbsp;</font>
 * <font color="#3f5fbf">X&nbsp;{added&nbsp;if&nbsp;addMissingParam}</font><br />
 * <font color="#808080">5</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&nbsp;</font><font color="#7f9fbf">@return&nbsp;</font>
 * <font color="#3f5fbf">X&nbsp;{added&nbsp;if&nbsp;addMissingReturn}</font><br />
 * <font color="#808080">6</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&nbsp;</font><font color="#7f9fbf">@throws&nbsp;</font>
 * <font color="#3f5fbf">X&nbsp;{added&nbsp;if&nbsp;addMissingThrows}</font><br />
 * <font color="#808080">7</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&nbsp;</font><font color="#7f9fbf">@since&nbsp;</font>
 * <font color="#3f5fbf">X&nbsp;{added&nbsp;if&nbsp;addMissingSince&nbsp;and&nbsp;new&nbsp;classes
 * from&nbsp;previous&nbsp;version}</font><br />
 * <font color="#808080">8</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&#47;</font><br />
 * <font color="#808080">9</font>&nbsp;<font color="#7f0055"><b>public&nbsp;</b></font>
 * <font color="#7f0055"><b>void&nbsp;</b></font><font color="#000000">dummyMethod</font>
 * <font color="#000000">(&nbsp;</font><font color="#000000">String&nbsp;s&nbsp;</font>
 * <font color="#000000">){}</font>
 * </code>
 *
 * @param buffer     not null
 * @param javaMethod not null
 * @param indent     not null
 * @throws MojoExecutionException if any
 * @see #getDefaultMethodJavadocComment(JavaMethod)
 * @see #appendDefaultSinceTag(StringBuilder, String)
 */
private void addDefaultMethodComment(final StringWriter stringWriter, final JavaMethod javaMethod,
        final String indent) throws MojoExecutionException {
    StringBuilder sb = new StringBuilder();

    // special case
    if (isInherited(javaMethod)) {
        sb.append(indent).append(INHERITED_JAVADOC);
        sb.append(EOL);

        stringWriter.write(sb.toString());
        return;
    }

    sb.append(indent).append(START_JAVADOC);
    sb.append(EOL);
    sb.append(indent).append(SEPARATOR_JAVADOC);
    sb.append(getDefaultMethodJavadocComment(javaMethod));
    sb.append(EOL);

    boolean separatorAdded = false;
    if (fixTag(PARAM_TAG)) {
        if (javaMethod.getParameters() != null) {
            for (int i = 0; i < javaMethod.getParameters().length; i++) {
                JavaParameter javaParameter = javaMethod.getParameters()[i];

                separatorAdded = appendDefaultParamTag(sb, indent, separatorAdded, javaParameter);
            }
        }
        // is generic?
        if (javaMethod.getTypeParameters() != null) {
            for (int i = 0; i < javaMethod.getTypeParameters().length; i++) {
                TypeVariable typeParam = javaMethod.getTypeParameters()[i];

                separatorAdded = appendDefaultParamTag(sb, indent, separatorAdded, typeParam);
            }
        }
    }
    if (fixTag(RETURN_TAG) && javaMethod.getReturns() != null && !javaMethod.getReturns().isVoid()) {
        separatorAdded = appendDefaultReturnTag(sb, indent, separatorAdded, javaMethod);
    }
    if (fixTag(THROWS_TAG) && javaMethod.getExceptions() != null && javaMethod.getExceptions().length > 0) {
        for (int i = 0; i < javaMethod.getExceptions().length; i++) {
            Type exception = javaMethod.getExceptions()[i];

            separatorAdded = appendDefaultThrowsTag(sb, indent, separatorAdded, exception);
        }
    }
    if (fixTag(SINCE_TAG) && isNewMethodFromLastRevision(javaMethod)) {
        separatorAdded = appendDefaultSinceTag(sb, indent, separatorAdded);
    }

    sb.append(indent).append(" ").append(END_JAVADOC);
    sb.append(EOL);

    stringWriter.write(sb.toString());
}

From source file:com.github.gekoh.yagen.ddl.CreateDDL.java

private void writeI18NDetailViewTriggerCreateString(Dialect dialect, StringBuffer buf, String i18nDetailTblName,
        String i18nTblName, String i18nFKColName, Set<String> columns) {
    VelocityContext context = new VelocityContext();
    String triggerBaseName = i18nDetailTblName.length() > MAX_LEN_OBJECT_NAME - 4
            ? i18nDetailTblName.substring(0, MAX_LEN_OBJECT_NAME - 4)
            : i18nDetailTblName;//from   w w  w .  j a v  a  2  s  . c o m

    context.put("dialect", dialect);
    context.put("i18nDetailTblName", i18nDetailTblName);
    context.put("i18nTblName", i18nTblName);
    context.put("i18nFKColName", i18nFKColName);
    context.put("columns", new ArrayList(columns));

    StringWriter wr = new StringWriter();
    if (isOracle(dialect)) {
        String objectName = triggerBaseName + "_TRG";
        context.put("objectName", objectName);

        setNewOldVar(dialect, context);

        mergeTemplateFromResource("I18NDetailViewTrigger.vm.pl.sql", wr, context);

        getProfile().duplex(ObjectType.TRIGGER, objectName, wr.toString());

        buf.append(STATEMENT_SEPARATOR).append(wr.toString()).append("\n/\n");
    } else if (isPostgreSql(dialect)) {
        String triggerName = triggerBaseName + "_TRG";
        String objectName = triggerName + "_function";
        context.put("objectName", objectName);

        setNewOldVar(dialect, context);

        mergeTemplateFromResource("I18NDetailViewTrigger.vm.pl.sql", wr, context);

        buf.append(STATEMENT_SEPARATOR).append(wr.toString()).append("\n/\n");

        buf.append(STATEMENT_SEPARATOR).append("create trigger ").append(triggerName).append("\n")
                .append("instead of insert or update or delete on ").append(i18nDetailTblName).append("\n")
                .append("for each row\n").append("execute procedure ").append(objectName).append("()");
    } else {
        try {
            wr.append(STATEMENT_SEPARATOR);
            writeTriggerSingleOperation(dialect, wr, "I18NDetailViewTrigger.hsqldb.vm.pl.sql", context,
                    triggerBaseName, "_it", "I");
            wr.write("\n/\n");
            wr.append(STATEMENT_SEPARATOR);
            writeTriggerSingleOperation(dialect, wr, "I18NDetailViewTrigger.hsqldb.vm.pl.sql", context,
                    triggerBaseName, "_it", "U");
            wr.write("\n/\n");
            wr.append(STATEMENT_SEPARATOR);
            writeTriggerSingleOperation(dialect, wr, "I18NDetailViewTrigger.hsqldb.vm.pl.sql", context,
                    triggerBaseName, "_it", "D");
            wr.write("\n/\n");

            buf.append(wr.toString());
        } catch (IOException e) {
            LOG.error("error writing audit triggers", e);
        }
    }
}

From source file:org.apache.maven.plugin.javadoc.AbstractFixJavadocMojo.java

/**
 * Take care of block or single comments between Javadoc comment and entity declaration ie:
 * <br/>/*from w w w. j  a va2 s .  c  om*/
 * <code>
 * <font color="#808080">1</font>&nbsp;<font color="#ffffff">&nbsp;</font>
 * <font color="#3f5fbf">&#47;&#42;&#42;</font><br />
 * <font color="#808080">2</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&nbsp;{Javadoc&nbsp;Comment}</font><br />
 * <font color="#808080">3</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&#47;</font><br />
 * <font color="#808080">4</font>&nbsp;<font color="#ffffff">&nbsp;</font>
 * <font color="#3f7f5f">&#47;&#42;</font><br />
 * <font color="#808080">5</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f7f5f">&#42;&nbsp;{Block&nbsp;Comment}</font><br />
 * <font color="#808080">6</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f7f5f">&#42;&#47;</font><br />
 * <font color="#808080">7</font>&nbsp;<font color="#ffffff">&nbsp;</font>
 * <font color="#3f7f5f">&#47;&#47;&nbsp;{Single&nbsp;comment}</font><br />
 * <font color="#808080">8</font>&nbsp;<font color="#ffffff">&nbsp;</font>
 * <font color="#7f0055"><b>public&nbsp;</b></font><font color="#7f0055"><b>void&nbsp;</b></font>
 * <font color="#000000">dummyMethod</font><font color="#000000">(&nbsp;</font>
 * <font color="#000000">String&nbsp;s&nbsp;</font><font color="#000000">){}</font>
 * </code>
 *
 * @param stringWriter    not null
 * @param originalContent not null
 * @param entity          not null
 * @throws IOException if any
 * @see #extractOriginalJavadoc(String, AbstractJavaEntity)
 */
private void takeCareSingleComment(final StringWriter stringWriter, final String originalContent,
        final AbstractInheritableJavaEntity entity) throws IOException {
    if (entity.getComment() == null) {
        return;
    }

    String javadocComment = trimRight(extractOriginalJavadoc(originalContent, entity));
    String extraComment = javadocComment.substring(javadocComment.indexOf(END_JAVADOC) + END_JAVADOC.length());
    if (StringUtils.isNotEmpty(extraComment)) {
        if (extraComment.contains(EOL)) {
            stringWriter.write(extraComment.substring(extraComment.indexOf(EOL) + EOL.length()));
        } else {
            stringWriter.write(extraComment);
        }
        stringWriter.write(EOL);
    }
}

From source file:org.apache.maven.plugin.javadoc.AbstractFixJavadocMojo.java

/**
 * @param stringWriter    not null//from ww w  .j a v a2s . co  m
 * @param originalContent not null
 * @param entity          not null
 * @param indent          not null
 * @throws MojoExecutionException if any
 * @throws IOException            if any
 */
private void updateEntityComment(final StringWriter stringWriter, final String originalContent,
        final AbstractInheritableJavaEntity entity, final String indent)
        throws MojoExecutionException, IOException {
    String s = stringWriter.toString();
    int i = s.lastIndexOf(START_JAVADOC);
    if (i != -1) {
        String tmp = s.substring(0, i);
        if (tmp.lastIndexOf(EOL) != -1) {
            tmp = tmp.substring(0, tmp.lastIndexOf(EOL));
        }
        stringWriter.getBuffer().delete(0, stringWriter.getBuffer().length());
        stringWriter.write(tmp);
        stringWriter.write(EOL);
    }

    updateJavadocComment(stringWriter, originalContent, entity, indent);
}

From source file:org.apache.maven.plugin.javadoc.AbstractFixJavadocMojo.java

/**
 * Add a default Javadoc for the given class, i.e.:
 * <br/>//  w  w  w.  java  2s.c  o  m
 * <code>
 * <font color="#808080">1</font>&nbsp;<font color="#ffffff">&nbsp;</font>
 * <font color="#3f5fbf">&#47;&#42;&#42;</font><br />
 * <font color="#808080">2</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&nbsp;{Comment&nbsp;based&nbsp;on&nbsp;the&nbsp;class&nbsp;name}</font><br />
 * <font color="#808080">3</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;</font><br />
 * <font color="#808080">4</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&nbsp;</font><font color="#7f9fbf">@author&nbsp;</font>
 * <font color="#3f5fbf">X&nbsp;{added&nbsp;if&nbsp;addMissingAuthor}</font><br />
 * <font color="#808080">5</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&nbsp;</font><font color="#7f9fbf">@version&nbsp;</font>
 * <font color="#3f5fbf">X&nbsp;{added&nbsp;if&nbsp;addMissingVersion}</font><br />
 * <font color="#808080">6</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&nbsp;</font><font color="#7f9fbf">@since&nbsp;</font>
 * <font color="#3f5fbf">X&nbsp;{added&nbsp;if&nbsp;addMissingSince&nbsp;and&nbsp;new&nbsp;classes
 * from&nbsp;previous&nbsp;version}</font><br />
 * <font color="#808080">7</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#42;&#47;</font><br />
 * <font color="#808080">8</font>&nbsp;<font color="#7f0055"><b>public&nbsp;class&nbsp;</b></font>
 * <font color="#000000">DummyClass&nbsp;</font><font color="#000000">{}</font></code>
 * </code>
 *
 * @param buffer    not null
 * @param javaClass not null
 * @param indent    not null
 * @see #getDefaultClassJavadocComment(JavaClass)
 * @see #appendDefaultAuthorTag(StringBuilder, String)
 * @see #appendDefaultSinceTag(StringBuilder, String)
 * @see #appendDefaultVersionTag(StringBuilder, String)
 */
private void addDefaultClassComment(final StringWriter stringWriter, final JavaClass javaClass,
        final String indent) {
    StringBuilder sb = new StringBuilder();

    sb.append(indent).append(START_JAVADOC);
    sb.append(EOL);
    sb.append(indent).append(SEPARATOR_JAVADOC);
    sb.append(getDefaultClassJavadocComment(javaClass));
    sb.append(EOL);

    appendSeparator(sb, indent);

    appendDefaultAuthorTag(sb, indent);

    appendDefaultVersionTag(sb, indent);

    if (fixTag(SINCE_TAG)) {
        if (!ignoreClirr) {
            if (isNewClassFromLastVersion(javaClass)) {
                appendDefaultSinceTag(sb, indent);
            }
        } else {
            appendDefaultSinceTag(sb, indent);
            addSinceClasses(javaClass);
        }
    }

    sb.append(indent).append(" ").append(END_JAVADOC);
    sb.append(EOL);

    stringWriter.write(sb.toString());
}

From source file:org.apache.maven.plugin.javadoc.AbstractFixJavadocMojo.java

/**
 * @param stringWriter    not null/*from   w w w  .  j  av a2  s.  c o  m*/
 * @param originalContent not null
 * @param entity          not null
 * @param indent          not null
 * @throws MojoExecutionException if any
 * @throws IOException            if any
 */
private void updateJavadocComment(final StringWriter stringWriter, final String originalContent,
        final AbstractInheritableJavaEntity entity, final String indent)
        throws MojoExecutionException, IOException {
    if (entity.getComment() == null && (entity.getTags() == null || entity.getTags().length == 0)) {
        return;
    }

    boolean isJavaMethod = false;
    if (entity instanceof JavaMethod) {
        isJavaMethod = true;
    }

    StringBuilder sb = new StringBuilder();

    // special case for inherited method
    if (isJavaMethod) {
        JavaMethod javaMethod = (JavaMethod) entity;

        if (isInherited(javaMethod)) {
            // QDOX-154 could be empty
            if (StringUtils.isEmpty(javaMethod.getComment())) {
                sb.append(indent).append(INHERITED_JAVADOC);
                sb.append(EOL);
                stringWriter.write(sb.toString());
                return;
            }

            String javadoc = getJavadocComment(originalContent, javaMethod);

            // case: /** {@inheritDoc} */ or no tags
            if (hasInheritedTag(javadoc)
                    && (javaMethod.getTags() == null || javaMethod.getTags().length == 0)) {
                sb.append(indent).append(INHERITED_JAVADOC);
                sb.append(EOL);
                stringWriter.write(sb.toString());
                return;
            }

            if (javadoc.contains(START_JAVADOC)) {
                javadoc = javadoc.substring(javadoc.indexOf(START_JAVADOC) + START_JAVADOC.length());
            }
            if (javadoc.contains(END_JAVADOC)) {
                javadoc = javadoc.substring(0, javadoc.indexOf(END_JAVADOC));
            }

            sb.append(indent).append(START_JAVADOC);
            sb.append(EOL);
            if (!javadoc.contains(INHERITED_TAG)) {
                sb.append(indent).append(SEPARATOR_JAVADOC).append(INHERITED_TAG);
                sb.append(EOL);
                appendSeparator(sb, indent);
            }
            javadoc = removeLastEmptyJavadocLines(javadoc);
            javadoc = alignIndentationJavadocLines(javadoc, indent);
            sb.append(javadoc);
            sb.append(EOL);
            if (javaMethod.getTags() != null) {
                for (int i = 0; i < javaMethod.getTags().length; i++) {
                    DocletTag docletTag = javaMethod.getTags()[i];

                    // Voluntary ignore these tags
                    if (JavadocUtil.equals(docletTag.getName(), PARAM_TAG, RETURN_TAG, THROWS_TAG)) {
                        continue;
                    }

                    String s = getJavadocComment(originalContent, entity, docletTag);
                    s = removeLastEmptyJavadocLines(s);
                    s = alignIndentationJavadocLines(s, indent);
                    sb.append(s);
                    sb.append(EOL);
                }
            }
            sb.append(indent).append(" ").append(END_JAVADOC);
            sb.append(EOL);

            if (hasInheritedTag(sb.toString().trim())) {
                sb = new StringBuilder();
                sb.append(indent).append(INHERITED_JAVADOC);
                sb.append(EOL);
                stringWriter.write(sb.toString());
                return;
            }

            stringWriter.write(sb.toString());
            return;
        }
    }

    sb.append(indent).append(START_JAVADOC);
    sb.append(EOL);

    // comment
    if (StringUtils.isNotEmpty(entity.getComment())) {
        updateJavadocComment(sb, originalContent, entity, indent);
    } else {
        addDefaultJavadocComment(sb, entity, indent, isJavaMethod);
    }

    // tags
    if (entity.getTags() != null && entity.getTags().length > 0) {
        updateJavadocTags(sb, originalContent, entity, indent, isJavaMethod);
    } else {
        addDefaultJavadocTags(sb, entity, indent, isJavaMethod);
    }

    sb = new StringBuilder(removeLastEmptyJavadocLines(sb.toString())).append(EOL);

    sb.append(indent).append(" ").append(END_JAVADOC);
    sb.append(EOL);

    stringWriter.write(sb.toString());
}

From source file:com.kodemore.utility.Kmu.java

public static String normalizeLineEnds(String source, String eol) {
    String line;/* w ww. j a v  a 2s.c  o  m*/
    BufferedReader in = null;
    StringWriter out = null;

    try {
        if (source == null)
            return null;

        StringReader sr = new StringReader(source);
        in = new BufferedReader(sr);
        out = new StringWriter();

        while (true) {
            line = in.readLine();
            if (line == null)
                break;

            out.write(line);
            out.write(eol);
        }

        return out.toString();
    } catch (IOException ex) {
        throw toRuntime(ex);
    } finally {
        closeSafely(in);
        closeSafely(out);
    }
}