You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Benedikt Ritter (JIRA)" <ji...@apache.org> on 2014/04/25 15:14:15 UTC
[jira] [Comment Edited] (LANG-993) Add zero copy write methods to
StrBuilder
[ https://issues.apache.org/jira/browse/LANG-993?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13980969#comment-13980969 ]
Benedikt Ritter edited comment on LANG-993 at 4/25/14 1:13 PM:
---------------------------------------------------------------
[~ash2k]: sorry for the late reply. I was a bit busy lately. I understand your concern, but I like the symmetry of only having {{writeTo(Appendable)}} and {{read(Readable)}}. How about checking the actual type of the Appendable and if it a Writer/StringBuilder/-Buffer, downcast and call the method that accepts the array?
If we go this direction it my make sende to rename the methods to:
* {{appendTo(Appenedable)}} (may be confusing because it is so close to {{append(...)}}
* {{readFrom(Readable)}} EDIT: apparently in your patch in LANG-994 it is already called readFrom
WDYT?
was (Author: britter):
[~ash2k]: sorry for the late reply. I was a bit busy lately. I understand your concern, but I like the symmetry of only having {{writeTo(Appendable)}} and {{read(Readable)}}. How about checking the actual type of the Appendable and if it a Writer/StringBuilder/-Buffer, downcast and call the method that accepts the array?
If we go this direction it my make sende to rename the methods to:
* {{appendTo(Appenedable)}} (may be confusing because it is so close to {{append(...)}}
* {{readFrom(Readable)}}
WDYT?
> Add zero copy write methods to StrBuilder
> -----------------------------------------
>
> Key: LANG-993
> URL: https://issues.apache.org/jira/browse/LANG-993
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.text.*
> Affects Versions: 3.3.1
> Reporter: Mikhail Mazursky
> Fix For: Review Patch, Discussion, 3.4
>
> Attachments: LANG-993.patch
>
>
> Currently I have the following usecase:
> {code}
> StrBuilder builder = new StrBuilder();
> // add lots of stuff to builder
> // in multiple invocations in several classes
> // writer cannot be used directly
> builder.append(...);
> Writer writer = ....;
> CharStreams.copy(builder.asReader(), writer);
> {code}
> [CharStreams|https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/io/CharStreams.java#177] is a class from Guava lib that copies data between reader and writer using temporary buffer.
> There is a problem with such approach - two additional copies are performed:
> 1) data is copied from the StrBuilder in chunks into temporary buffer (CharBuffer)
> 2) Writer.append(CharSequence) is called that is usually implemented as write(CharSequence.toString()) - i.e. it makes another copy of data and allocates an additional String object.
> I want to avoid those copies by writing the internal buffer of the StrBuilder directly to the writer. Also it is potentially more efficient because it performs one I/O call instead of many.
> So I propose to add the following methods:
> {code}
> public void writeTo(Writer writer) throws IOException {
> writer.write(buffer, 0, size);
> }
> public void writeTo(StringBuilder builder) {
> builder.append(buffer, 0, size);
> }
> public void writeTo(StringBuffer buffer) {
> buffer.append(this.buffer, 0, size);
> }
> {code}
> If there is interest I will provide patch (with JavaDocs and tests).
--
This message was sent by Atlassian JIRA
(v6.2#6252)