Efficient Record Serialization – Java I/O: Part I

Efficient Record Serialization

Example 20.6 shows an example of serializing and deserializing an instance of a record class (ยง5.14, p. 299). It is worth noting that both processes on records are very efficient as the state components of a record entirely describe the state values to serialize, and as the canonical constructor of a record class is always used to create the complete state of a record, the canonical constructor is also always used during deserialization. By design, selective and customized serialization, discussed later in this section, are not allowed for records.

Example 20.6 Object Serialization

Click here to view code image

import java.io.Serializable;
import java.time.Year;
/** A record class that represents a CD. */
public record CD(String artist, String title, int noOfTracks,
                 Year year, Genre genre) implements Serializable {
  public enum Genre implements Serializable {POP, JAZZ, OTHER}
}

Click here to view code image

//Reading and Writing Objects
import java.io.*;
import java.time.Year;
import java.util.Arrays;
public class ObjectSerializationDemo {
  void writeData() {                                    // (1)
    try (// Set up the output stream:
        FileOutputStream outputFile = new FileOutputStream(“obj-storage.dat”);
        ObjectOutputStream outputStream = new ObjectOutputStream(outputFile)) {
      // Write data:
      String[] strArray = {“Seven”, “Eight”, “Six”};
      long num = 2014;
      int[] intArray = {1, 3, 1949};
      String commonStr = strArray[2];                  // “Six”
      CD oneCD = new CD(“Jaav”, “Java Jive”, 8, Year.of(2017), CD.Genre.POP);
      outputStream.writeObject(strArray);
      outputStream.writeLong(num);
      outputStream.writeObject(intArray);
      outputStream.writeObject(commonStr);
      outputStream.writeObject(oneCD);
    } catch (FileNotFoundException e) {
      System.err.println(“File not found: ” + e);
    } catch (IOException e) {
      System.err.println(“Write error: ” + e);
    }
  }
  void readData() {                                     // (2)
    try (// Set up the input stream:
        FileInputStream inputFile = new FileInputStream(“obj-storage.dat”);
        ObjectInputStream inputStream = new ObjectInputStream(inputFile)) {
      // Read the data:
      String[] strArray = (String[]) inputStream.readObject();
      long num = inputStream.readLong();
      int[] intArray = (int[]) inputStream.readObject();
      String commonStr = (String) inputStream.readObject();
      CD oneCD = (CD) inputStream.readObject();
      // Write data to the standard output stream:
      System.out.println(Arrays.toString(strArray));
      System.out.println(num);
      System.out.println(Arrays.toString(intArray));
      System.out.println(commonStr);
      System.out.println(oneCD);
    } catch (FileNotFoundException e) {
      System.err.println(“File not found: ” + e);
    } catch (EOFException e) {
      System.err.println(“End of stream: ” + e);
    } catch (IOException e) {
      System.err.println(“Read error: ” + e);
    } catch (ClassNotFoundException e) {
      System.err.println(“Class not found: ” + e);
    }
  }
  public static void main(String[] args) {
    ObjectSerializationDemo demo = new ObjectSerializationDemo();
    demo.writeData();
    demo.readData();
  }
}

Output from the program:

Click here to view code image [Seven, Eight, Six]
2014
[1, 3, 1949]
Six
CD[artist=Jaav, title=Java Jive, noOfTracks=8, year=2017, genre=POP]