So what is the max length of a String?

K

Kaidi

Anyone has any idea?
The reason is: when I try to use the writeUTF of ObjectOutputStream to
write out a String, I get the error of UTFDataFormatException
exception.
I traced it to the ObjectOutputStream class, and found the lines where
the exeception happens look like:

void writeUTF(String s, long utflen) throws IOException
{
if (utflen > 0xFFFFL) {
throw new UTFDataFormatException();
}
......
....
}

So it only allows the String to be at max 0xFFFFL? Why has such a
restriction?
PS: actually I am not writing out a too large String, it is only a
String which is the content of a 90K plain text file!

Thanks.
 
C

Chris Smith

Kaidi said:
Anyone has any idea?
The reason is: when I try to use the writeUTF of ObjectOutputStream to
write out a String, I get the error of UTFDataFormatException
exception.
I traced it to the ObjectOutputStream class, and found the lines where
the exeception happens look like:

void writeUTF(String s, long utflen) throws IOException
{
if (utflen > 0xFFFFL) {
throw new UTFDataFormatException();
}
.....
...
}

So it only allows the String to be at max 0xFFFFL? Why has such a
restriction?

A String can be up to 2 billion or so characters long (2^31 - 1,
actually). However, a string longer than 65535 (2^16 - 1) characters
cannot be written in the format used by DataOutputStream.writeUTF.
That's because the format used by writeUTF is byte-counted, and leaves
only a 16-bit space for the count. You will need to find a different
format to write in.
PS: actually I am not writing out a too large String, it is only a
String which is the content of a 90K plain text file!

Are you sure you want the binary format used by writeUTF? Perhaps you
mean to write a plain text String instead? The latter would be fairly
simple.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
K

Kaidi

Chris Smith said:
A String can be up to 2 billion or so characters long (2^31 - 1,
actually). However, a string longer than 65535 (2^16 - 1) characters
cannot be written in the format used by DataOutputStream.writeUTF.
That's because the format used by writeUTF is byte-counted, and leaves
only a 16-bit space for the count. You will need to find a different
format to write in.


Are you sure you want the binary format used by writeUTF? Perhaps you
mean to write a plain text String instead? The latter would be fairly
simple.
Thanks for your post.
Yes, I actually may not need the writeUTF to write out the string,
however,
the problem is: if I use writeUTF, then I can use readUTF to read it
back
easily.

Do you have any recommendation for this situation: if I need to write
many int, double, String into one file, and need to read them back in
the same order, what method should I use?
At first, I just write them as object (write/readObject), but I findd
it is just too slow as Java will check the relations/reference of each
object.
Now I use writeInt, writeDouble, writeUTF, and read them back, which
have the above problem if the String is too long. Any other better way
to do it?
(If suppose the String maybe in any coding/language, what would be the
difference?)

Thanks a lot.
 
C

Chris Smith

Kaidi said:
Do you have any recommendation for this situation: if I need to write
many int, double, String into one file, and need to read them back in
the same order, what method should I use?

Yep, you want writeUTF then. So you'll need a work-around for this
problem. For example, you can do the following:

1. Cut the String into slices of length no greater than 65535.
2. First write to the stream the number of slices, using writeInt.
3. Next write each slice in turn.

Then when reading, reverse that: read the number of slices, and read
each slice in turn, appending them to form the result.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Michael Borgwardt

Kaidi said:
Do you have any recommendation for this situation: if I need to write
many int, double, String into one file, and need to read them back in
the same order, what method should I use?

Build your own readUTF() clone that uses an int instead of a short for the
length of the following byte array. Very easy to do.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top