StringBuilder for byte[]

Discussion in 'Java' started by Roedy Green, Jan 18, 2013.

  1. Roedy Green

    Roedy Green Guest

    It would be a fairly simple task to write a StringBuilder or FastCat
    that worked with byte[]. I wonder if such a beast exists already, or
    if there would be much use for it.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The first 90% of the code accounts for the first 90% of the development time.
    The remaining 10% of the code accounts for the other 90% of the development
    time.
    ~ Tom Cargill Ninety-ninety Law
    Roedy Green, Jan 18, 2013
    #1
    1. Advertising

  2. Roedy Green

    Sven Köhler Guest

    Am 18.01.2013 09:40, schrieb Roedy Green:
    > It would be a fairly simple task to write a StringBuilder or FastCat
    > that worked with byte[]. I wonder if such a beast exists already, or
    > if there would be much use for it.


    ByteArrayOutputStream exists. Its write() method would be the equivalent
    of StringBuilder.append().

    Regards,
    Sven
    Sven Köhler, Jan 18, 2013
    #2
    1. Advertising

  3. Roedy Green

    Lew Guest

    Sven Köhler wrote:
    > Roedy Green:
    >> It would be a fairly simple task to write a StringBuilder or FastCat
    >> that worked with byte[]. I wonder if such a beast exists already, or
    >> if there would be much use for it.

    >
    > ByteArrayOutputStream exists. Its write() method would be the equivalent
    > of StringBuilder.append().


    Actually, not quite. The former needs a Writer to handle encoding.

    A better choice would be
    http://docs.oracle.com/javase/7/docs/api/java/io/CharArrayWriter.html

    --
    Lew
    Lew, Jan 18, 2013
    #3
  4. Roedy Green

    Sven Köhler Guest

    Am 18.01.2013 17:54, schrieb Lew:
    > Sven Köhler wrote:
    >> Roedy Green:
    >>> It would be a fairly simple task to write a StringBuilder or FastCat
    >>> that worked with byte[]. I wonder if such a beast exists already, or
    >>> if there would be much use for it.

    >>
    >> ByteArrayOutputStream exists. Its write() method would be the equivalent
    >> of StringBuilder.append().

    >
    > Actually, not quite. The former needs a Writer to handle encoding.


    Oh I see. You interpreted the question completely different.
    I was assuming, that the input was bytes, not characters.

    But anyway: wouldn't ByteArrayOutputStream + OutputStreamWriter be
    exactly the right thing if the input was characters?

    > A better choice would be
    > http://docs.oracle.com/javase/7/docs/api/java/io/CharArrayWriter.html


    It works with char[] internally. The question was about something that
    works with byte[] internally, I believe. Not sure, what that is good for
    though.


    Regards,
    Sven
    Sven Köhler, Jan 18, 2013
    #4
  5. Roedy Green

    Arne Vajhøj Guest

    On 1/18/2013 11:54 AM, Lew wrote:
    > Sven Köhler wrote:
    >> Roedy Green:
    >>> It would be a fairly simple task to write a StringBuilder or FastCat
    >>> that worked with byte[]. I wonder if such a beast exists already, or
    >>> if there would be much use for it.

    >>
    >> ByteArrayOutputStream exists. Its write() method would be the equivalent
    >> of StringBuilder.append().

    >
    > Actually, not quite. The former needs a Writer to handle encoding.
    >
    > A better choice would be
    > http://docs.oracle.com/javase/7/docs/api/java/io/CharArrayWriter.html


    But wasn't the point to work with byte[] instead of String/char[] ?

    Arne
    Arne Vajhøj, Jan 18, 2013
    #5
  6. Roedy Green

    Arne Vajhøj Guest

    On 1/18/2013 3:40 AM, Roedy Green wrote:
    > It would be a fairly simple task to write a StringBuilder or FastCat
    > that worked with byte[]. I wonder if such a beast exists already, or
    > if there would be much use for it.


    There are plenty of classes in java.io that writes bytes
    sequentially.

    ByteArrayOutputStream or DataOutputStream on top of
    ByteArrayOutputStream was two options.

    Arne
    Arne Vajhøj, Jan 18, 2013
    #6
  7. Roedy Green

    Lew Guest

    Arne Vajhøj wrote:
    > Lew wrote:
    >>> Roedy Green:
    >>>> It would be a fairly simple task to write a StringBuilder or FastCat
    >>>> that worked with byte[]. I wonder if such a beast exists already, or
    >>>> if there would be much use for it.


    >> A better choice would be
    >> http://docs.oracle.com/javase/7/docs/api/java/io/CharArrayWriter.html

    >
    > But wasn't the point to work with byte[] instead of String/char[] ?


    Was it?

    He said that the task is "to write a StringBuilder or FastCat". I don't know what a
    "FastCat" is, but a 'StringBuilder' needs encodings. So 'byte[]' is a case of being
    focused on implementation rather than goal, a flaw you yourself often notice in
    people's queries, Arne.

    So I have to ask, if your goal is to build a 'String', why would you use 'byte' when 'char'
    is for that purpose?

    I presume to conclude that Roedy screwed up on that one.

    --
    Lew
    Lew, Jan 19, 2013
    #7
  8. Roedy Green

    Arne Vajhøj Guest

    On 1/18/2013 8:07 PM, Lew wrote:
    > Arne Vajhøj wrote:
    >> Lew wrote:
    >>>> Roedy Green:
    >>>>> It would be a fairly simple task to write a StringBuilder or FastCat
    >>>>> that worked with byte[]. I wonder if such a beast exists already, or
    >>>>> if there would be much use for it.

    >
    >>> A better choice would be
    >>> http://docs.oracle.com/javase/7/docs/api/java/io/CharArrayWriter.html

    >>
    >> But wasn't the point to work with byte[] instead of String/char[] ?

    >
    > Was it?
    >
    > He said that the task is "to write a StringBuilder or FastCat".


    Not really.

    That was the first part of what he said that you just arbitrarily truncated.

    > I don't know what a
    > "FastCat" is, but a 'StringBuilder' needs encodings.


    True. But byte[] does not.

    > So 'byte[]' is a case of being
    > focused on implementation rather than goal,


    It asked for a StringBuilder for byte[]. That is a goal.

    It seems weird to me to assume that he want something else than
    what he asked for.

    > So I have to ask, if your goal is to build a 'String', why would you use 'byte' when 'char'
    > is for that purpose?


    There are not really any indication that he want to build a String.

    There is every indication that he want to build a byte array.

    When somebody ask for "Type1Something for Type2" it seems
    natural to assume that they want "Type2Something" and a
    ByteArrayBuilder would produce a byte array.

    > I presume to conclude that Roedy screwed up on that one.


    It happens.

    But I doubt it is the case this time.

    Arne
    Arne Vajhøj, Jan 19, 2013
    #8
  9. Roedy Green

    Arne Vajhøj Guest

    On 1/18/2013 8:27 PM, Arne Vajhøj wrote:
    > On 1/18/2013 8:07 PM, Lew wrote:
    >> Arne Vajhøj wrote:
    >>> Lew wrote:
    >>>>> Roedy Green:
    >>>>>> It would be a fairly simple task to write a StringBuilder or FastCat
    >>>>>> that worked with byte[]. I wonder if such a beast exists already, or
    >>>>>> if there would be much use for it.

    >>
    >>>> A better choice would be
    >>>> http://docs.oracle.com/javase/7/docs/api/java/io/CharArrayWriter.html
    >>>
    >>> But wasn't the point to work with byte[] instead of String/char[] ?

    >>
    >> Was it?
    >>
    >> He said that the task is "to write a StringBuilder or FastCat".

    >
    > Not really.
    >
    > That was the first part of what he said that you just arbitrarily
    > truncated.
    >
    >> I don't know what a
    >> "FastCat" is, but a 'StringBuilder' needs encodings.

    >
    > True. But byte[] does not.
    >
    >> So 'byte[]' is a case of
    >> being
    >> focused on implementation rather than goal,

    >
    > It asked for a StringBuilder for byte[]. That is a goal.
    >
    > It seems weird to me to assume that he want something else than
    > what he asked for.
    >
    >> So I have to ask, if your goal is to build a 'String', why would you
    >> use 'byte' when 'char'
    >> is for that purpose?

    >
    > There are not really any indication that he want to build a String.
    >
    > There is every indication that he want to build a byte array.
    >
    > When somebody ask for "Type1Something for Type2" it seems
    > natural to assume that they want "Type2Something" and a
    > ByteArrayBuilder would produce a byte array.
    >
    >> I presume to conclude that Roedy screwed up on that one.

    >
    > It happens.
    >
    > But I doubt it is the case this time.


    This is somewhat confirmed by his other question.

    Arne
    Arne Vajhøj, Jan 19, 2013
    #9
  10. Roedy Green

    Roedy Green Guest

    On Fri, 18 Jan 2013 17:07:16 -0800 (PST), Lew <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >He said that the task is "to write a StringBuilder or FastCat".


    What I intended was just something to concatenate byte[] into longer
    byte[], no encodings involved, not a StringBuilder than squirted out
    encoded bytes. It is a subset of what ByteArrayOutputStream does.

    A FastCat is like a StringBuilder but where you estimate the number
    fragments to be joined, not the total length of the result. It is more
    RAM efficient since it allocates the precise amount of space needed
    for the concatenated String.

    see http://mindprod.com/jgloss/stringbuilder.html
    where I discuss the merits of StringBuilder vs FastCat vs StringBuffer
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The first 90% of the code accounts for the first 90% of the development time.
    The remaining 10% of the code accounts for the other 90% of the development
    time.
    ~ Tom Cargill Ninety-ninety Law
    Roedy Green, Jan 19, 2013
    #10
  11. Roedy Green

    Lew Guest

    Roedy Green wrote:
    > Lew wrote, quoted or indirectly quoted someone who said :
    >> He said that the task is "to write a StringBuilder or FastCat".

    >
    > What I intended was just something to concatenate byte[] into longer
    > byte[], no encodings involved, not a StringBuilder than squirted out
    > encoded bytes. It is a subset of what ByteArrayOutputStream does.
    >
    > A FastCat is like a StringBuilder but where you estimate the number
    > fragments to be joined, not the total length of the result. It is more
    > RAM efficient since it allocates the precise amount of space needed
    > for the concatenated String.
    >
    > see http://mindprod.com/jgloss/stringbuilder.html
    >
    > where I discuss the merits of StringBuilder vs FastCat vs StringBuffer


    I stand corrected.

    --
    Lew
    Lew, Jan 19, 2013
    #11
  12. Roedy Green

    Roedy Green Guest

    On Sat, 19 Jan 2013 00:42:52 -0800 (PST), Lew <>
    wrote, quoted or indirectly quoted someone who said :

    >I stand corrected.


    I said something that was interpreted two different ways. That is my
    bad, not yours.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The first 90% of the code accounts for the first 90% of the development time.
    The remaining 10% of the code accounts for the other 90% of the development
    time.
    ~ Tom Cargill Ninety-ninety Law
    Roedy Green, Jan 20, 2013
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H.G.Srivatsa

    string vs stringbuilder

    H.G.Srivatsa, Aug 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    17,027
    Richard Grimes [MVP]
    Aug 25, 2005
  2. =?Utf-8?B?SGFuZGk=?=

    no stringbuilder in C#2.0?

    =?Utf-8?B?SGFuZGk=?=, Sep 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    988
    =?Utf-8?B?SGFuZGk=?=
    Sep 11, 2004
  3. ESPN Lover

    How to reset a StringBuilder?

    ESPN Lover, Oct 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    6,942
    ESPN Lover
    Oct 14, 2004
  4. darrel
    Replies:
    2
    Views:
    464
    Juan T. Llibre
    Jan 28, 2005
  5. =?Utf-8?B?SnVzdGlu?=

    Double Quotes in a stringbuilder

    =?Utf-8?B?SnVzdGlu?=, Mar 17, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    7,854
    Mythran
    Mar 18, 2005
Loading...

Share This Page