예제 #1
0
  /**
   * Deserializes data and and loads it on memory.
   *
   * @return True if it was correctly loaded. False otherwise.
   */
  public synchronized boolean deSerialize() {
    try {
      FileInputStream fileIn = context.openFileInput(FILE_NAME);
      ObjectInputStream in = new ObjectInputStream(fileIn);
      ElementsCollection.setInstance((ElementsCollection) in.readObject());
      in.close();
      fileIn.close();
      return true;
    } catch (IOException i) {
      i.printStackTrace();
      // PreferencesClass.getInstance(context).clearPreferences();
      return false;
    } catch (ClassNotFoundException c) {
      Log.d("Serializator", "Raised a ClassNotFoundException. Message: " + c.getMessage());
      c.printStackTrace();

      return false;
    }
  }
예제 #2
0
  /**
   * Serializes and stores user data.
   *
   * @return true when successful. False otherwise
   */
  public synchronized boolean serialize() {
    // TODO this is always returning true, need to check it
    boolean success = true;

    try {
      // Opens file for writing.
      FileOutputStream fileOut = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
      ObjectOutputStream out = new ObjectOutputStream(fileOut);
      out.writeObject(ElementsCollection.getInstance());
      out.close();
      fileOut.close();

      Log.d("Serializator", "Serialized data is saved in " + FILE_NAME);
    } catch (IOException i) {
      Log.d("Serializator", "Serialize failed. IOException. Message:" + i.getMessage());
      i.printStackTrace();
    }

    return success;
  }