1

I refer to ObjectStreamConstants.STREAM_MAGIC in the interface ObjectStreamConstants, which is defined in the JDK source like so:

/**
 * Magic number that is written to the stream header.
 */
final static short STREAM_MAGIC = (short)0xaced;

What is significance of this magic number, and how is it related to Java serialization?

Is there any particular reason that it is a short and has the value 0xaced? I've tried to find more information, but I didn't get anything that helped me understand better.

halfer
  • 19,824
  • 17
  • 99
  • 186
CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • 1
    Check out the [Java Object Serialization Protocol](https://docs.oracle.com/javase/6/docs/platform/serialization/spec/protocol.html). – Avi Aug 23 '19 at 18:32
  • 1
    If you look at the hexdump of any serialization output (an example is provided in Avi's link), it will begin with `ac ed`, which is `ObjectStreamConstants.STREAM_MAGIC`. – MultiplyByZer0 Aug 23 '19 at 18:41
  • Thanks for your response. I got it, however , I wanted to print the "aced". How can I do that? I created a simple class which Implemented Serialization. Then did Serialization using `ObjectOutputStream`. Where can I see "aced". – CuriousMind Aug 23 '19 at 19:20
  • 1
    @CuriousMind Use this code: `ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(thing); byte[] bytes = baos.toByteArray();`, and then pass `bytes` to a `byte[]` to hex string converter, which can be found in [this question](https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java). – MultiplyByZer0 Aug 23 '19 at 19:47
  • @MultiplyByZer0: Got it. Thanks for your help – CuriousMind Aug 23 '19 at 19:55

1 Answers1

1

STREAM_MAGIC denotes start of serialzed content. You can find more details at https://docs.oracle.com/javase/1.5.0/docs/guide/serialization/spec/protocol.html