JDK-4025564 : ObjectInputStream.readObject() failures when size of Object
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io:serialization
  • Affected Version: 1.1,1.1.2
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS:
    generic,solaris_2.5.1,windows_95,windows_nt generic,solaris_2.5.1,windows_95,windows_nt
  • CPU: generic,x86,sparc
  • Submitted: 1997-01-13
  • Updated: 1997-10-17
  • Resolved: 1997-10-17
Related Reports
Duplicate :  
Relates :  
Relates :  
Description

Name: el35337			Date: 01/13/97

Please compile this source and run.
---- source --------------------------------------------------------
  import java.io.*;

Please compile this source and run.
---- source --------------------------------------------------------
  import java.io.*;

  public class SerializeTest
  {
  // static int size = 65535; // <-- OK
   static int size = 65536; // <-- NG

  public static void main(String[] args)
  {
  try
    {
    StringBuffer sb = new StringBuffer();
    String in;
    String out;
    File objfile = new File("test.obj");
    File txtfile = new File("test.txt");

    for(int i=0;i<size;i++)
      sb.append('a');

    out = sb.toString();

    ObjectOutputStream os =
      new ObjectOutputStream(new FileOutputStream(objfile));
    os.writeObject(out);
    os.flush();
    os.close();
    System.err.println("write: " + out.length() + " chars.");

    ObjectInputStream is =
      new ObjectInputStream(new FileInputStream(objfile));
    in = (String)is.readObject();
    is.close();
    System.err.println("read:  " + in.length() + " chars.");

    FileOutputStream f = new FileOutputStream(txtfile);
    f.write(in.getBytes());
    f.flush();
    f.close();
    }
  catch (Exception e)
    {
    System.err.println(e.getMessage());
    e.printStackTrace(System.err);
    }
   }
  }
---- end of source --------------------------------------------------

Fujitsu       fujitsu!fb.se.fujitsu.co.jp!###@###.###

======================================================================

Comments
EVALUATION This error occurs because on writing the length is incorrerctly encoded. Object serialization uses DataOutputStream.writeUTF to store strings. There are a two problems with that implementation. First the JLS specifies that if the length of the encoded string is > 65535 an UTFDataFormatError is thrown. Second, because the encoding may use 1, 2 or 3 bytes per character of the array the conditions under which the exception occurs are data dependent. If the characters are ASCII the length is 65535. If the values are greater than ~49000 only 21845 characters can be stored. What DataOutput really needs is an extensible length that can correctly represent arbitrary or at least much greater length strings. The simplest short term change that is needed is to throw UTFDataFormatException when the encoding is greater than 65535.
11-06-2004

WORK AROUND A workaround for long strings to to serialize the string.toCharArray object instead of the string itself. These character arrays can then easily be read and strings created.
11-06-2004

PUBLIC COMMENTS Serializing java.lang.String objects is done using Java's modified UTF-8 format. That format restricts the encoded length of the string to 65536 bytes. A java.io.UTFDataFormatException is thrown for strings that encode to more than 65536 bytes.
10-06-2004