title: Serialization/deserialization ClassCastException: x cannot be cast to java.io.ObjectStreamClass
body: <p>Using Java's native serialization, I'm intermittently seeing ClassCastExceptions </p>
  
  <pre><code>java.lang.ClassCastException: myCompany.MyClass$MembershipServiceMethod cannot be cast to java.io.ObjectStreamClass
  </code></pre>
  
  <p>or (less frequently)</p>
  
  <pre><code>java.lang.ClassCastException: java.lang.String cannot be cast to java.io.ObjectStreamClass
  </code></pre>
  
  <p>when I deserialize objects of a particular immutable class. That is to say, the exception is always thrown for particular serialized representations, but most objects can be successfully serialized and deserialized.</p>
  
  <pre><code>public final class ServiceInteractionImpl implements ServiceInteraction, Serializable {
  private static final long serialVersionUID = 1L;
  
  private final InteractionSource source;
  private final long startTime;
  private final InteractionType type;
  private final ServiceMethod method;
  private final long duration;
  private final String accompanyingMessage;
  
  public ServiceInteractionImpl(final ServiceMethod method,
                                    final InteractionSource source,
                                    final InteractionType type,
                                    final long startTime,
                                    final long duration,
                                    final String accompanyingMessage) {
              this.source = source;
              this.startTime = startTime;
              this.duration = duration;
              this.type = type;
              this.method = method;
              this.accompanyingMessage = accompanyingMessage;
      }
  
      ...
  
      getters and canonical methods
  }
  </code></pre>
  
  <p>where InteractionSource, InteractionType and ServiceMethod are enums. 
  I can't replicate this in my local environment running junit tests that serialize and deserialize millions of objects.</p>
  
  <p>Here is the writing code</p>
  
  <pre><code>    fileLock.lock();
      try {
          final File recordFile = new File(recordFileName);
          boolean appendToFile = false;
  
          if (recordFile.exists()) {
              appendToFile = true;
          }
  
          final OutputStream fileOutputStream = new FileOutputStream(recordFileName, true);
          final OutputStream buffer = new BufferedOutputStream(fileOutputStream);
  
          ObjectOutput output;
          if (appendToFile) {
              output = new AppendableObjectOutputStream(buffer);
          } else {
              output = new ObjectOutputStream(buffer);
          }
  
          int localSerializedInteractionsCount = 0;
  
          try {
              for (final ServiceInteraction interaction : tempCollection) {
                  output.writeObject(interaction);
                  output.flush();
                  localSerializedInteractionsCount++;
                  rollBackCollection.remove(interaction);
              }
          } finally {
              output.close();
              serializedInteractionCount += localSerializedInteractionsCount;
          }
      } catch (IOException e) {
          LOGGER.error("could not write to file", e);
          //some roll-back code
      } finally {
          fileLock.unlock();
      }
  </code></pre>
  
  <p>see <a href="https://stackoverflow.com/questions/1194656/appending-to-an-objectoutputstream">Appending to an ObjectOutputStream</a> for the AppendableObjectOutputStream</p>
  
  <p>Any help much appreciated.</p>