StringBuilder for byte[]

R

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.
--
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
 
S

Sven Köhler

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
 
L

Lew

Sven said:
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
 
S

Sven Köhler

Am 18.01.2013 17:54, schrieb Lew:
Sven said:
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?

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
 
A

Arne Vajhøj

Sven said:
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
 
A

Arne Vajhøj

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
 
L

Lew

Arne said:
Lew said:
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.
 
A

Arne Vajhøj

Arne said:
Lew said:
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
 
A

Arne Vajhøj

Arne said:
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
 
R

Roedy Green

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
 
L

Lew

Roedy said:
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.
 
R

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

creating byte[] with subfields 14
video cards for Java 1
ctrl-c ctril-v 14
regex reserved chars 23
abbreviated generic syntax 14
probing SSL websites 1
jdk 1.7.0_13 is out 4
slick progress bar 5

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top