JDK-4194583 : java.io.DataInputStream.readUTF/DataOutputStream.writeUTF operate on single byte
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: x86
  • Submitted: 1998-12-03
  • Updated: 1999-04-19
  • Resolved: 1999-04-19
Related Reports
Duplicate :  
Relates :  
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
10-06-2004

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
03-12-1998

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