== Why is this an issue?

An `ObjectOutputStream` writes primitive data types and graphs of Java objects to an `OutputStream`.
The objects can be read (reconstituted) using an `ObjectInputStream`.

When `ObjectOutputStream` is used with files opened in append mode, it can cause data corruption and unexpected behavior.
This is because when `ObjectOutputStream` is created, it writes metadata to the output stream, which can conflict with the existing
metadata when the file is opened in append mode. This can lead to errors and data loss.

When used with serialization, an `ObjectOutputStream` first writes the serialization stream header. This header should appear
once per file at the beginning.
When you're trying to read your object(s) back from the file, only the first one will be read successfully, and a `StreamCorruptedException`
will be thrown after that.

== How to fix it

Open the file to use the default action (writes stream header).

=== Code examples

==== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
FileOutputStream fos = new FileOutputStream(fileName , true);  // fos opened in append mode
ObjectOutputStream out = new ObjectOutputStream(fos);  // Noncompliant
----


==== Compliant solution

[source,java,diff-id=1,diff-type=compliant]
----
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream out = new ObjectOutputStream(fos);
----

== Resources
=== Articles & blog posts
* https://docs.jboss.org/jbossas/javadoc/4.0.2/org/jboss/util/stream/AppendingObjectOutputStream.java.html[JBoss - AppendingObjectOutputStream]

=== Documentation
* https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/io/ObjectOutputStream.html[Oracle SE 20 - ObjectOutputStream]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

This file was opened in append mode.


endif::env-github,rspecator-view[]
