Welcome to MSDN Blogs Sign in | Join | Help

Demonstrating Object Serialization

Assuming you know the rules for Object Serialization, my aim in this article is to write an app in J# which does Object Serialization and then I shall decipher the serialized data.

 

Below is the app which does the object serialization. This app is inspired from :

 

// Demonstrates Object Serialization

 

import java.io.*;

import java.util.*;

 

class Store implements Serializable {

  private int n;

  public Store(int n) { this.n = n; }

  public String toString() { return Integer.toString(n); }

}

 

public class link implements Serializable {

  private static int next_int = 0;

  private Store[] d = {

    new Store(next_int++),

    new Store(next_int++),

    new Store(next_int++)

  };

  private link next;

  private char c;

  // Value of i == number of segments

  public link(int i, char x) {

    c = x;

    if(--i > 0)

      next = new link(i, (char)(x + 1));

  }

  public link() {}

 

  public static void main(String[] args) throws ClassNotFoundException, IOException {

    link w = new link(4, 'a');

 

    ObjectOutputStream out = new ObjectOutputStream(

      new FileOutputStream("link.out"));

 

    out.writeObject(w);

    out.close();

  }

}

 

 

Now using the app from my previous post, when I try to read the contents of the output file link.out, I get the following result:

 

 

>file_bytes.exe link.out

 

ac  ed  0  5  73  72  0  4  6c  69  6e  6b  c8  f1  6d  e  38  aa  2f  ca  2  0  3  43  0  1  63  5b  0  1  64  74  0  8  5b  4c  53  74  6f  72  65  3b  4c  0  4  6e  65  78  74  74  0  6  4c  6c  69  6e  6b  3b  78  70  0  61  75  72  0  8  5b  4c  53  74  6f  72  65  3b  29  20  b6  39  0  37  55  a0  2  0  0  78  70  0  0  0  3  73  72  0  5  53  74  6f  72  65  ee  f5  85  2f  82  b5  ab  5a  2  0  1  49  0  1  6e  78  70  0  0  0  0  73  71  0  7e  0  6  0  0  0  1  73  71  0  7e  0  6  0  0  0  2  73  71  0  7e  0  0  0  62  75  71  0  7e  0  4  0  0  0  3  73  71  0  7e  0  6  0  0  0  3  73  71  0  7e  0  6  0  0  0  4  73  71  0  7e  0  6  0  0  0  5  73  71  0  7e  0  0  0  63  75  71  0  7e  0  4  0  0  0  3  73  71  0  7e  0  6  0  0  0  6  73  71  0  7e  0  6  0  0  0  7  73  71  0  7e  0  6  0  0  0  8  73  71  0  7e  0  0  0  64  75  71  0  7e  0  4  0  0  0  3  73  71  0  7e  0  6  0  0  0  9  73  71  0  7e  0  6  0  0  0  a  73  71  0  7e  0  6  0  0  0  b  70 

Total no of bytes read :286

 

Now, let us decipher this data. I would use “à” as a notation for “implies”

 

 

ac ed à STREAM MAGIC

0 5  à STREAM VERSION

 

73 à Terminal constant for OBJECT

72 à Terminal constant for CLASSDESC

 

0  4 à length of the class name (“link”)

 

6c  69  6e  6b  à ‘l’  ‘i’  ‘n’  ‘k’

 

c8  f1  6d  e  38  aa  2f  ca  à serialVersionUID

 

2 à serializable flag

 

0  3 à No of serializable fields in class “link”

 

43 à  ‘C’ , primitive type code for Char

 

0  1 à length of the char field name i.e 1 here

 

63 à ‘c’ , name of the char field

 

5b à ‘[‘ , object type code for array

 

0  1 à length of the array field name i.e 1 here

 

64 à ‘d’ , name of the array field

 

74 à Terminal constant for STRING

 

0  8 à Length of type name is 8

 

5b  4c  53  74  6f  72  65  3b  à ‘[‘  ‘L’  ‘S’  ‘t’  ‘o’  ‘r’  ‘e’  ‘;’ (field descriptor format)

 

4c à ‘L’ , object type code for object

 

0  4 à length of the object field name i.e 4 here

 

6e  65  78  74  à ‘n’  ‘e’  ‘x’  ‘t’

 

74 à Terminal constant for STRING

 

0  6 à Length of type name is 6

 

4c  6c  69  6e  6b  3b à ‘L’  ‘l’  ‘i’  ‘n’  ‘k’  ‘;’ (field descriptor format)

 

78  à Terminal constant for ENDBLOCKDATA

70  à Terminal constant for NULL (null reference for super class)

 

0  61  à char value is ‘a’

 

75  à Terminal constant for ARRAY

72  à Terminal constant for CLASSDESC

 

0  8 à Length of type name is 8

 

5b  4c  53  74  6f  72  65  3b  à ‘[‘  ‘L’  ‘S’  ‘t’  ‘o’  ‘r’  ‘e’  ‘;’ (field descriptor format)

 

29  20  b6  39  0  37  55  a0  à serialVersionUID

 

2 à serializable flag

 

0  0  à No of serializable fields in the array class

 

78  à Terminal constant for ENDBLOCKDATA

70  à Terminal constant for NULL (null reference for super class)

 

0  0  0  3  à No of fields in the array

 

73 à Terminal constant for OBJECT

72 à Terminal constant for CLASSDESC

 

0  5 à String length of “Store”

 

53  74  6f  72  65  à ‘S’  ‘t’  ‘o’  ‘r’  ‘e’

 

ee  f5  85  2f  82  b5  ab  5a  à serialVersionUID

 

2 à serializable flag for Store class

 

0  1 à No of serializable fields in Store class

 

49  à ‘I’ , primitive type code for integer

 

0  1 à length of the char field name i.e 1 here

 

6e à ‘n’ , name of the char field in Store class

 

78  à Terminal constant for ENDBLOCKDATA

70  à Terminal constant for NULL (null reference for super class)

 

0  0  0  0 à int value of the field ‘n’ ( which is 0 here )

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  1 à int value of the field ‘n’ ( which is 1 here )

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  2  à int value of the field ‘n’ ( which is 2 here )

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  0  à handle to the link class

 

0  62  à char value is ‘b’

 

75 à Terminal constant for ARRAY

71 à Terminal constant for REFERENCE

 

0  7e  0  4  à handle to the array class

 

0  0  0  3  à No of fields in the array

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  3  à int value of the field ‘n’ ( which is 3 here )

 

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  4  à int value of the field ‘n’ ( which is 4 here )

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  5  à int value of the field ‘n’ ( which is 5 here )

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  0  à handle to the link class

 

0  63  à char value is ‘c’

 

75 à Terminal constant for ARRAY

71 à Terminal constant for REFERENCE

 

0  7e  0  4  à handle to the array class

 

0  0  0  3  à No of fields in the array

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  6  à int value of the field ‘n’ ( which is 6 here )

 

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  7  à int value of the field ‘n’ ( which is 7 here )

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  8  à int value of the field ‘n’ ( which is 8 here )

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  0  à handle to the link class

 

0  64  à char value is ‘d’

 

75 à Terminal constant for ARRAY

71 à Terminal constant for REFERENCE

 

0  7e  0  4  à handle to the array class

 

0  0  0  3  à No of fields in the array

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  9  à int value of the field ‘n’ ( which is 9 here )

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  a  à int value of the field ‘n’ ( which is 10 here )

 

73 à Terminal constant for OBJECT

71  à Terminal constant for REFERENCE

 

0  7e  0  6  à handle to the Store class

 

0  0  0  b  à int value of the field ‘n’ ( which is 11 here )

 

70 à Terminal constant for NULL ( for the “next” member of this link object)

 

 

 

Posted by premk | 1 Comments

Display Raw Contents of a file

Sometimes you just want to see the raw contents of a file. This is especially useful when you don’t have any cool editor/program which can read that file and present the contents in some useful manner. Like you may want to see contents of a zipped file or contents of a file that contains serialized data of an object.

 

The following J# program reads the content of the file and outputs the same on the standard output console. All the bytes read are written in hex format. It also tells you the no of bytes read from the file.

 

 

 

import java.io.*;

 

public class file_bytes

{

      public static void main(String[] args) throws FileNotFoundException, IOException

      {

            FileInputStream fi = new FileInputStream(args[0]);

            int read_byte;

            int length = 0;

            while ((read_byte = fi.read()) != -1)

            {

                  System.out.print(Integer.toHexString(read_byte) + "  ");

                  length++;

            }

            System.out.print("\n");

            System.out.print("Total no of bytes read :" + length);

 

      }

}