Class GamePersistence

java.lang.Object
org.xxdc.oss.example.GamePersistence

public class GamePersistence extends Object
Provides methods for saving and loading a Game object to/from a file.

The saveTo(File, Game) method writes the provided Game object to the specified file. The loadFrom(File) method reads a Game object from the specified file and returns it.

The GamePersistence.GamePersistenceFilter class is used to filter the objects that can be loaded from the file, rejecting any loaded classes with more than 1000 object references to prevent deserialization attacks.

  • Constructor Details

    • GamePersistence

      public GamePersistence()
  • Method Details

    • saveTo

      public void saveTo(File gameFile, Game game) throws IOException
      Saves the provided Game object to the specified file.
      Parameters:
      gameFile - the file to save the game state to
      game - the Game object to save
      Throws:
      IOException - if an I/O error occurs while writing the game state to the file
    • loadFrom

      public Game loadFrom(File gameFile) throws IOException, ClassNotFoundException
      Reads a Game object from the specified file and returns it.
      Parameters:
      gameFile - the file to load the game state from
      Returns:
      the Game object read from the file
      Throws:
      IOException - if an I/O error occurs while reading the game state from the file
      ClassNotFoundException - if the Game class cannot be found