Wednesday, February 9, 2011

Java: Serializing unknown Arraysize

If I safe an Array and reload it, is there a possibility to get the size if its unknown? Thanks

  • What do you mean by "unknown"? You can get the length of any java array with the length field.

    int[] myArray = deserializeSomeArray();
    int size = myArray.length;
    
  • I store an Array of my own Objects to a file. But i dont know how many objects are in these Array if I open it. I use the ObjectOutputStream. And Input to read. It works if i know the number of fields.

  • Hi, I think you need to supply some more information. How are you saving the array? Using an ObjectOutputStream?

    From Zarkonnen
  • No because the length of the array is just the size of memory allocated divided by the size of the object stored in it, and since no objects have a size of 0 you will always have a proper length, (which could be 0)

    From pfranza
  • If you use ObjectInputStream.readObject() to read the saved array, it will be reconstituted with the proper length and you can just read the size with array.length.

  • Attempting to read between the lines...

    If you are actually reading array, then (unlike C) all arrays know their length. Java is a safe language, so the length is necessary for bounds checking.

    MyType[] things = (MyType[])in.readObject();
    int len = things.length;
    

    Perhaps your difficulty is that you are doing custom (de)serialisation and are writing out individual elements of the array (hint: don't - use an array). In the case you need to catch OptionDataException to detect the end of the enclosing object's custom data:

    private static final MyType[] NOTHING = new MyType[0];
    
    private transient MyType[] things = NOTHING;
    
    private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject(); // Do not forget this call!
        for (MyType thing : things) {
            out.writeObject(thing);
        }
    }
    private void readObject(
        ObjectInputStream in
    ) throws IOException, ClassNotFoundException {
        in.defaultReadObject(); // Do not forget this call!
        List<MyType> things = new ArrayList<MyType>();
        try {
            for (;;) {
                things.add((MyType)in.readObject();
            }
        } catch (OptionalDataException exc) {
            // Okay - end of custom data.
        }
        this.things = things.toArray(NOTHING);
    }
    

    If you are going to do that sort of thing, it's much better to write out the number of objects you are going to read as an int before the actual data.

  • It sounds like you're serializing and storing the individual objects in the array (after much reading between the lines). Use the ObjectOutputStream to store the array itself. If the objects stored in the array are serializable, they'll be stored too. When you deserialize you'll get the entire array back intact.

    R. Bemrose : According to a book I have, trying to serialize an array of unserializable objects throws a NotSerializableException.
    Bill the Lizard : That's correct. I think it's safe to assume the objects that wellenreiter is using are serializable, I just thought my explanation was a little more clear with that thrown in. Other people having the same or similar problem will appreciate the extra information.
  • Ok, it was easier than i did it. My Question is answered. Thank you all a lot =)

0 comments:

Post a Comment