Java - ObjectOutputStream writeStreamHeader() method



Description

The Java ObjectOutputStream writeStreamHeader() method is provided so subclasses can append or prepend their own header to the stream. It writes the magic number and version to the stream.

  • It's automatically called when an ObjectOutputStream is created.

  • It writes standard stream metadata (0xACED0005 by default).

  • You can override it in a subclass to −

    • Add custom headers

    • Write version information

    • Integrate with custom protocols

Declaration

Following is the declaration for java.io.ObjectOutputStream.writeStreamHeader() method.

public void writeStreamHeader()

Parameters

  • NA

Return Value

This method does not return a value.

Exception

  • IOException − If I/O errors occur while writing to the underlying stream.

Example - Add a custom version tag at the beginning of the stream

The following example shows the usage of ObjectOutputStream writeStreamHeader() method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      try (CustomStream oos = new CustomStream(new FileOutputStream("header1.ser"))) {
         oos.writeObject("Hello with custom header");
      }
   }

   static class CustomStream extends ObjectOutputStream {
      public CustomStream(OutputStream out) throws IOException {
         super(out);
      }

      @Override
      protected void writeStreamHeader() throws IOException {
         super.writeStreamHeader(); // keep default header
         writeUTF("stream-version:1.0"); // add custom tag
         System.out.println("Custom header written: stream-version:1.0");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Custom header written: stream-version:1.0

Explanation

  • Writes default Java stream header.

  • Then adds a custom version string.

  • Useful for version-aware deserialization later.

Example - Write a completely custom header (skip default)

The following example shows the usage of ObjectOutputStream writeStreamHeader() method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      try (RawHeaderStream oos = new RawHeaderStream(new FileOutputStream("header2.ser"))) {
         oos.writeObject("Custom-only header");
      }
   }

   static class RawHeaderStream extends ObjectOutputStream {
      public RawHeaderStream(OutputStream out) throws IOException {
         super(out);
      }

      @Override
      protected void writeStreamHeader() throws IOException {
         // Skip default header and write your own 4-byte magic
         writeInt(0xDEADBEEF); // custom marker
         System.out.println("Wrote raw header: 0xDEADBEEF");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Wrote raw header: 0xDEADBEEF

Explanation

  • Replaces the default Java header entirely.

  • Useful when interoperating with non-Java readers or enforcing a custom file format.

  • Requires custom ObjectInputStream to handle it.

Example - Add a timestamp to the stream header

The following example shows the usage of ObjectOutputStream writeStreamHeader() method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      try (TimestampedStream oos = new TimestampedStream(new FileOutputStream("header3.ser"))) {
         oos.writeObject("Stream with timestamp");
      }
   }

   static class TimestampedStream extends ObjectOutputStream {
      public TimestampedStream(OutputStream out) throws IOException {
         super(out);
      }

      @Override
      protected void writeStreamHeader() throws IOException {
         super.writeStreamHeader(); // retain default
         writeLong(System.currentTimeMillis()); // custom timestamp
         System.out.println("Wrote timestamp to header.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Wrote timestamp to header.

Explanation

  • Keeps standard header, then writes a timestamp.

  • Useful for tracking when a stream was created.

  • The corresponding ObjectInputStream must read this timestamp before readObject().

java_io_objectoutputstream.htm
Advertisements