Java Externalizable example

17 January 2013
By Gonçalo Marques
In this tutorial you will learn how to implement a custom serialization mechanism by implementing the Externalizable interface.

Introduction

The Externalizable interface provides the necessary means for implementing a custom serialization mechanism. Implementing the Externalizable interface means that we must override some of its methods, namely the writeExternal and readExternal methods. These methods will be called when you serialize (or deserialize) a given instance. If you know about the Serializable interface you may have already noticed that there are some differences. When we implement the Serializable interface we don't need to implement any method: The serialization takes place automatically. Even if we want to implement our own serialization mechanism by implementing the Serializable interface (yes it is also possible by defining writeObject and readObject methods) we don't need to override or implement any method. The JVM calls the serialization methods from our class using reflection. In early JVM implementations reflection performance was kind of slow so the Externalizable interface was also used to overcome this disadvantage.

Environment:

  1. Ubuntu 12.04
  2. JDK 1.7.0.09

A simple class

Let's define a simple class to use in this example:

User class implementing the Externalizable interface
package com.byteslounge.serialization;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class User implements Externalizable {
  
  private int id;
  private String username;
  private String email;
  
  public User(int id, String username, String email) {
    this.id = id;
    this.username = username;
    this.email = email;
  }
  
  public User(){
    
  }

  public int getId() {
    return id;
  }

  public String getUsername() {
    return username;
  }

  public String getEmail() {
    return email;
  }

  @Override
  public void writeExternal(ObjectOutput out) throws IOException {
    out.writeInt(id);
    out.writeObject(username);
    out.writeObject(email);
  }

  @Override
  public void readExternal(ObjectInput in) throws IOException,
      ClassNotFoundException {
    id = in.readInt();
    username = (String) in.readObject();
    email = (String) in.readObject();
  }
  
}


Note that we are implementing the Externalizable interface and overriding writeExternal and readExternal methods. We must also define a default constructor when we implement the Externalizable interface. It we fail to do so the serialization will fail.

Testing

Let's create a simple test class:

Main.java

package com.byteslounge.serialization;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {

  public static void main(String[] args) 
    throws Exception {
    
    User userWrite = new User(1, "johndoe", "John Doe");
    FileOutputStream fos = new FileOutputStream("testfile");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(userWrite);
    oos.flush();
    oos.close();

    User userRead;
    FileInputStream fis = new FileInputStream("testfile");
    ObjectInputStream ois = new ObjectInputStream(fis);
    userRead = (User)ois.readObject();
    ois.close();
    
    System.out.println("username: " + userRead.getUsername());

  }

}


When we run the test the following output is generated:

username: johndoe

The example source code is available at the end of this page.

Download source code from this article

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: https://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: