United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 4194583 java.io.DataInputStream.readUTF/DataOutputStream.writeUTF operate on single byte
4194583 : java.io.DataInputStream.readUTF/DataOutputStream.writeUTF operate on single byte

Details
Type:
Enhancement
Submit Date:
1998-12-03
Status:
Closed
Updated Date:
1999-04-19
Project Name:
JDK
Resolved Date:
1999-04-19
Component:
core-libs
OS:
generic
Sub-Component:
java.io
CPU:
x86
Priority:
P4
Resolution:
Duplicate
Affected Versions:
1.2.0
Fixed Versions:

Related Reports
Duplicate:
Relates:

Sub Tasks

Description
DataOutputStream.writeUTF computes single bytes of the resulting UTF and 
writes them to the stream one byte at a time.  That's okay for buffered 
output streams, but for ByteArrayOutputStreams (or unbuffered streams)
that results in a synchronized method call per byte (at least).

Similarly for DataInputStream.readUTF reading from a ByteArrayInputStream.

                                    

Comments
PUBLIC COMMENTS

DataInputStream.readUTF and DataOutputStream.writeUTF operate on single bytes
                                     
2004-06-10
SUGGESTED FIX

The suggested fix is to assemble the result of writeUTF into a local byte[]
and then do a single write(byte[], ...).  That copies the bytes an extra 
time (with System.arraycopy in ByteArrayOutputStream), but it avoids a lot 
of synchronized calls.

The parallel fix in DataInputStream.readUTF is, after you figure out how long 
the incoming UTF is, to do a readFully(byte[],...) to a local byte[] and 
then extract bytes from that array rather than calling readUnsignedByte() 
for each byte.

peter.kessler@Eng 1998-12-03
                                     
1998-12-03
EVALUATION

Yep.  Measure it first, though.  -- mr@eng 1998/12/16
                                     
182-06-12 0



Hardware and Software, Engineered to Work Together