This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Sep 1, 2010 4:22 PM by EJP RSS

java.io.StreamCorruptedException: invalid type code: FF

843790 Newbie
Currently Being Moderated
Hi
When I meet the exception as shown int the title of this thread, I go to google, then I found some similar problem, here is a example:

[The same qustion|http://forums.sun.com/thread.jspa?threadID=5293295&start=30]

However I can not know how to change my own codes, So I ask for help .

The following is my codes:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import javax.imageio.ImageIO;

public class Clienter {
     public Clienter() {
          ArrayList<SerializedClass> list = new ArrayList<SerializedClass>();
          for (int i = 0; i < 10; i++) {
               SerializedClass sc = new SerializedClass(new File("D:/01.bmp"));
               list.add(sc);
          }
          try {
               // write the list
               ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("D:/test.style"));
               os.writeObject(list);
               os.close();
               // read the list
               ObjectInputStream is = new ObjectInputStream(new FileInputStream("D:/test.style"));
               ArrayList<SerializedClass> listn = (ArrayList<SerializedClass>) is.readObject();
               is.close();
               for (SerializedClass sc : listn) {
                    sc.paint();
               }
          } catch (FileNotFoundException e) {
               e.printStackTrace();
          } catch (IOException e) {
               e.printStackTrace();
          } catch (ClassNotFoundException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }

     public static void main(String[] args) {
          new Clienter();
     }
}

class SerializedClass implements Serializable {
     private WrapperBufferedImage1 image;

     public SerializedClass(File f) {
          try {
               BufferedImage b = ImageIO.read(f);
               image = new WrapperBufferedImage1(b);
          } catch (IOException e) {
          }
     }

     public void paint() {
          if (image == null)
               return;
          // Draw the bufferedImage,here use a simple example
          BufferedImage bi = image.getImage();
          System.out.println("at paint Method " + bi.getWidth());
     }
}

class WrapperBufferedImage implements Serializable {
     private BufferedImage bi = null;

     public WrapperBufferedImage(BufferedImage b) {
          this.bi = b;
     }

     public BufferedImage getImage() {
          return bi;
     }

     private void writeObject(ObjectOutputStream os) throws IOException {
          ImageIO.write(bi, "bmp", ImageIO.createImageOutputStream(os));
     }

     private void readObject(ObjectInputStream is) throws IOException {
          bi = ImageIO.read(ImageIO.createImageInputStream(is));
     }
}
What is the problem?
  • 1. Re: java.io.StreamCorruptedException: invalid type code: FF
    797177 Newbie
    Currently Being Moderated
    I ran your code after fixing a compile error (you are instantiating a class with a name WrapperBufferedImage1 but the class is named WrapperBufferedImage - without the number 1). In any event I did not encounter the exception. I am using JDK 1.6 (build 1.6.0_20-b02).
  • 2. Re: java.io.StreamCorruptedException: invalid type code: FF
    843790 Newbie
    Currently Being Moderated
    Thanks for your reply.
    I just find the error, however the thread is replied so I can not modify it.

    For the problem.
    I use jdk ( (build 1.6.0_18-b07).
    I wonder if I have to change my jdk, however our entire application is built under the jdk1.6.0_18.

    Is there any problems in my codes? Or if I can modify my codes to make it fit for my current jdk?
  • 3. Re: java.io.StreamCorruptedException: invalid type code: FF
    797177 Newbie
    Currently Being Moderated
    I doubt the problem is JDK version specific. I only provided the version I was using as an FYI. I suspect the problem is related to the bitmap file.
  • 4. Re: java.io.StreamCorruptedException: invalid type code: FF
    843790 Newbie
    Currently Being Moderated
    paternostro wrote:
    I suspect the problem is related to the bitmap file.
    Seems you are right, I download another bmp icon, and it works fine.

    The first bmp file I use is created by the Draw Software built in the Windows xp system.
    This is the file I used: [the file|http://www.box.net/shared/xpjc8ozpeu]
    I hope one can check it,
    So can you tell me why the different file would cause the problem?

    ALso, when I compile the codes, I got the notice:
    Note: Clienter.java use unchecked or unsafe operations. NOTE: To learn more, please use the-Xlint: unchecked recompile.
    Usually this is caused by the generic, but I declare the Arraylist<SerlizedClass> Explicitly,why?

    Edited by: apachemaven on 2010-8-31 ??2:32
  • 5. Re: java.io.StreamCorruptedException: invalid type code: FF
    797177 Newbie
    Currently Being Moderated
    apachemaven wrote:
    Note: Clienter.java use unchecked or unsafe operations. NOTE: To learn more, please use the-Xlint: unchecked recompile.
    Usually this is caused by the generic, but I declare the Arraylist<SerlizedClass> Explicitly,why?
    The warning has nothing to do with the declaration. The warning has to do with the unchecked cast on the value returned from readObject() method.
  • 6. Re: java.io.StreamCorruptedException: invalid type code: FF
    843790 Newbie
    Currently Being Moderated
    How about the image?
  • 7. Re: java.io.StreamCorruptedException: invalid type code: FF
    EJP Guru
    Currently Being Moderated
         private void writeObject(ObjectOutputStream os) throws IOException {
              ImageIO.write(bi, "bmp", ImageIO.createImageOutputStream(os));
         }
    
         private void readObject(ObjectInputStream is) throws IOException {
              bi = ImageIO.read(ImageIO.createImageInputStream(is));
         }
    }
         private void writeObject(ObjectOutputStream os) throws IOException {
                    os.defaultWriteObject();
              ImageIO.write(bi, "bmp", ImageIO.createImageOutputStream(os));
         }
    
         private void readObject(ObjectInputStream is) throws IOException {
                    is.defaultReadObject();
              bi = ImageIO.read(ImageIO.createImageInputStream(is));
         }
    }
  • 8. Re: java.io.StreamCorruptedException: invalid type code: FF
    843790 Newbie
    Currently Being Moderated
    os.defaultWriteObject();
    This will cause a exception: BufferedImage can not be serialized.
  • 9. Re: java.io.StreamCorruptedException: invalid type code: FF
    EJP Guru
    Currently Being Moderated
    So make it transient.
  • 10. Re: java.io.StreamCorruptedException: invalid type code: FF
    843790 Newbie
    Currently Being Moderated
    ejp wrote:
    So make it transient.
    The reason I wrapped a new class WrappedBufferedImage is to make the bufferedimage serialized, so make it transient will avoid the bufferedimage to be serialized.

    BTW, I found a similar question:
    [streamcorruptedexception-invalid-type-code-ac|http://stackoverflow.com/questions/2393179/streamcorruptedexception-invalid-type-code-ac]
    And a guy said:
    >
    The underlying problem is that you are using a new ObjectOutputStream to write to an existing ObjectInputStream>
    that you have already used a prior ObjectOutputStream to write to. These streams have headers which are written >and read by the respective constructors, so if you start another ObjectOutputStream you will write a new header, >which starts with - guess what? - 0xAC, and the existing ObjectInputStream isn't expecting another header at this >point so it barfs.
    In the Java Forums thread cited above, I should have left out the part about 'anew for each object at both ends': >that's just wasteful. Use a single OOS and OIS for the life of the socket. And don't use any other streams on the >socket. If you want to forget what you've written, use ObjectOutputStream.reset().
    But I can not know it clearly, is my problem caused by the same reason?
  • 11. Re: java.io.StreamCorruptedException: invalid type code: FF
    EJP Guru
    Currently Being Moderated
    [making] it transient will avoid the bufferedimage to be serialized.
    No it won't. You are serializing it yourself in your writeObject() method, and deserializing it yourself in your readObject() method.
    BTW, I found a similar question:
    [streamcorruptedexception-invalid-type-code-ac|http://stackoverflow.com/questions/2393179/streamcorruptedexception-invalid-type-code-ac]
    And a guy said:
    That 'guy' was me. This is a different problem. That was type code AC. This is type code FF. That problem was due to using muliple ObjectOutputStreams. You aren't doing that.

    You could always try what I'm telling you here, instead of just arguing about it.
  • 12. Re: java.io.StreamCorruptedException: invalid type code: FF
    843790 Newbie
    Currently Being Moderated
    ejp wrote:
    [making] it transient will avoid the bufferedimage to be serialized.
    No it won't. You are serializing it yourself in your writeObject() method, and deserializing it yourself in your readObject() method.
    Thanks, I just got it.
    You could always try what I'm telling you here, instead of just arguing about it.
    In fact I have tested what you told me yet , but the exception is the same as before.
    And what amazing me is that: the chance of the exception occur depend on the number of the SerilizedClass I add to the list.
    That to say:
    for(int i=0;i<num;i++) {
      SerilizedClass sc=new (....);
      list.add(sc);
    }
    If the num is no more than five or six(not exactly sure), the exception would not throwed.
    If the num is more than ten (also not exactly sure), the exception will be throwed.
  • 13. Re: java.io.StreamCorruptedException: invalid type code: FF
    EJP Guru
    Currently Being Moderated
    Try this:
         private void writeObject(ObjectOutputStream os) throws IOException {
                    os.defaultWriteObject();
              ImageIO.write(bi, "bmp", os);
         }
    
         private void readObject(ObjectInputStream is) throws IOException {
                    is.defaultReadObject();
              bi = ImageIO.read(is);
         }
    }
  • 14. Re: java.io.StreamCorruptedException: invalid type code: FF
    843790 Newbie
    Currently Being Moderated
    Thanks, it works now!!

    Can you tell me the reason?
1 2 Previous Next