Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-2253

Serialization and Deserialization of SimpleDesktopCache throws NPE

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Blocker
    • Resolution: Fixed
    • Affects Version/s: 7.0.1
    • Fix Version/s: 7.0.2
    • Component/s: None
    • Labels:
      None

      Description

      The Serialization works fine, but the deserialization is the problem. Here is a little test case.

      SimpleDesktopCacheSerializationTest.java
      import java.io.ByteArrayInputStream;
      import java.io.ByteArrayOutputStream;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      
      import org.zkoss.zk.ui.impl.SimpleDesktopCache;
      import org.zkoss.zk.ui.util.Configuration;
      
      public class SimpleDesktopCacheSerializationUnitTest {
      
      	public static void main(final String... args) {
      		final Configuration config = new Configuration();
      		final SimpleDesktopCache sdc = new SimpleDesktopCache(config);
      		try {
      			read(write(sdc));
      		} catch (final Exception e) {
      			e.printStackTrace();
      		}
      	}
      
      	public static byte[] write(final Object object) throws Exception {
      		final ByteArrayOutputStream binOut = new ByteArrayOutputStream();
      		final ObjectOutputStream out = new ObjectOutputStream(binOut);
      		out.writeObject(object);
      		out.flush();
      		out.close();
      		return binOut.toByteArray();
      	}
      
      	public static Object read(final byte[] data) throws Exception {
      		final ByteArrayInputStream bIn = new ByteArrayInputStream(data);
      		final ObjectInputStream in = new ObjectInputStream(bIn);
      		return in.readObject();
      	}
      
      }
      

      The problem is in the the methode readObject(final java.io.ObjectInputStream s) of the inner class SimpleDesktopCache$Cache. You try to disable expunge for the deserialization process.
      1. The variable _expungeDisabled is null because deserialization is not finished -> There goes the NPE
      2. There is no need to disable expunge before deserialization
      3. You need to enable expunge after deserialization

      My code recommendation for readObject(final java.io.ObjectInputStream s) would be:

      SimpleDesktopCacheSerializationFix
      	private void readObject(final java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
      		s.defaultReadObject();
      		disableExpunge(false);
      	}
      

      I hope this could help,

      Greetings salbader

        Attachments

          Activity

            People

            Assignee:
            jumperchen jumperchen
            Reporter:
            salbader salbader
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: