Synchronize object serialization(Android) - java

tl;dr:
I have two threads, one of them serealizes object, and another tries to read object. How do I avoid collisions, i.e. synchronize access to file?
More info:
I have Service which uses AsyncTask to fetch data and then serializes object:
#Override
protected Boolean doInBackground(Void... params) {
FeedItem currentItem = mParser.parseFeed();
Util.saveItem(UpdateService.this, currentItem);
}
Object serialization:
public class Util {
private static final String sFileName = "feedobject";
public static FeedItem loadItem(Context context) {
FeedItem result = null;
try {
FileInputStream fis = context.openFileInput(sFileName);
ObjectInputStream is = new ObjectInputStream(fis);
result = (FeedItem) is.readObject();
is.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return result;
}
public static void saveItem(Context context, FeedItem item) {
FileOutputStream fos;
try {
fos = context.openFileOutput(sFileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(item);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I also try to read object from Activity, i.e. from UI thread.
mTextView.setText(Html.fromHtml(Util.loadItem(this).message));

If you can rename files under Android, then you could write the object to a temporary file "feedobject.t" and then rename it to be "feedobject" which should be atomic.
private static final String sFileName = "feedobject";
private static final String tempFileName = "feedobject.t";
...
File tempFile = new File(tempFileName);
... // write the file
tempFile.renameTo(new File(sFileName));
If this doesn't work then you will be forced to have a synchronized lock. The consumer should lock and wait for the producer to finish writing. The producer needs to lock to make sure it is writing only when the consumer is not reading.
You could just make the saveItem and loadItem methods be synchronized which would lock the class:
public static synchronized FeedItem loadItem(Context context) {
...
public static synchronized void saveItem(Context context, FeedItem item) {
...
Better would be to create a lock object and lock on that:
private static final Object itemLock = new Object();
Then in the loadItem and saveItem methods you'd lock on that object:
public static FeedItem loadItem(Context context) {
synchronized (itemLock) {
// do the reading...
}
Hope this helps.

Related

Why is this Class' object not serialized properly in different processes?

Context
I made a Java application, and need to run two instances of that application, synchronizing some of their attributes via socket each time there's some change. To communicate those changes, Serializable objects are sent through a socket using ObjectStreams (input and output) using read/writeUTF() for an identifier, and read/writeObject() and flush(). The app is the exact same .jar, run twice with some changes like having different ports and ip (if necessary).
Problem
I noticed that objects of some of my classes (e.g. Notification) were sent and received without any troubles, but objects from another class (RegisteredUsers) weren't sent (or received) properly. So I ran some tests to send objects between the two apps and found that the object is being sent and isn't null, it's attribute (a HashMap<String,User>) is also being sent and isn't null, but is always empty.
So I decided to scale it down to what the problem was exactly: I'm trying to write an object through a Stream, and read it in a different process of the same .jar, and with most classes it seems to work, but it doesn't with one.
There seems to be something I'm missing or don't understand about this serialization process, if the object is written and read during the execution of the same process it works, but not if this object is read on another instance of the same app. I even added a HashMap to Notification with the same creation process, but it still works, I really don't get it, what am I missing?
Code
I have taken some code from the bigger app and trimmed it down to the basic problem if anyone wants to test it. To reproduce the errors, run Main1, which will create the two files with an object persisted in each one (one with a Notification object and the other with a RegisteredUsers object) and shows their information, then Main2, which reads them from the files and shows their information, and the problem should be printed. That being that reg3's HashMap is empty and thus neither of the Users are registered.
Main1
public class Main1 {
public static void main(String[] args) {
String regFile = "registry.txt";
String notificationFile = "notification.txt";
Persistence pers = new Persistence();
RegisteredUsers reg1 = new RegisteredUsers();
RegisteredUsers reg2 = new RegisteredUsers();
reg1.register("Name1", "127.0.0.1");
reg1.register("Name2", "127.0.0.1");
try {
pers.writeReg(reg1, regFile);
} catch (IOException e) {
System.out.println("Error writing registry.");
}
try {
reg2 = pers.readReg(regFile);
} catch (IOException e) {
System.out.println("Error reading registry.");
}
System.out.println("Original registry: ");
System.out.println(reg1.isRegistered("Name1") + " " + reg1.isRegistered("Name2"));
System.out.println("Registry read from file: ");
System.out.println(reg2.isRegistered("Name1") + " " + reg2.isRegistered("Name2"));
Notification noti1 = new Notification("Name", "127.0.0.1");
Notification noti2 = new Notification(); //not necesary but it's the way it's done in the bigger app.
try {
pers.writeNotif(noti1, notificationFile);
} catch (IOException e) {
System.out.println("Error writing notification.");
}
try {
noti2 = pers.readNotif(notificationFile);
} catch (IOException e) {
System.out.println("Error reading notification.");
}
System.out.println("Original notification: ");
System.out.println(noti1.getAttributes().get(0) + " " + noti1.getAttributes().get(1));
System.out.println(noti1.getMap());
System.out.println("Notification read from file: ");
System.out.println(noti2.getAttributes().get(0) + " " + noti2.getAttributes().get(1));
System.out.println(noti2.getMap());
}
}
Main2
public class Main2 {
public static void main(String[] args) {
String regFile = "registry.txt";
String notificationFile = "notification.txt";
Persistence pers = new Persistence();
RegisteredUsers reg3 = new RegisteredUsers();
try {
reg3 = pers.readReg(regFile);
} catch (IOException e) {
System.out.println("Error reading registry.");
}
if (reg3 == null) {
System.out.println("reg3 is null");
}
if (reg3.getMap() == null)
System.out.println("reg3 has a null map");
if (reg3.getMap().isEmpty())
System.out.println("reg3 has an empty map");
System.out.println("Registry read from file on another process: ");
System.out.println(reg3.isRegistered("Name1") + " " + reg3.isRegistered("Name2"));
Notification noti3 = new Notification(); //not necesary but it's the way it's done in the bigger app.
try {
noti3 = pers.readNotif(notificationFile);
} catch (IOException e) {
System.out.println("Error reading notification.");
}
System.out.println("Notification read from file on another process: ");
System.out.println(noti3.getAttributes().get(0) + " " + noti3.getAttributes().get(1));
System.out.println(noti3.getMap());
}
}
A Class to persist the objects in the files:
public class Persistence {
public void writeReg(RegisteredUsers regus, String file) throws IOException {
try(FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);) {
oos.writeObject(regus);
oos.flush();
}
}
public RegisteredUsers readReg(String file) throws IOException {
RegisteredUsers regus = null;
try(FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);) {
regus = (RegisteredUsers) ois.readObject();
} catch (ClassNotFoundException e) {
System.out.println("Wrong class.");
}
return regus;
}
public void writeNotif(Notification regus, String file) throws IOException {
try(FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);) {
oos.writeObject(regus);
oos.flush();
}
}
public Notification readNotif(String file) throws IOException {
Notification notif = null;
try(FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);) {
notif = (Notification) ois.readObject();
} catch (ClassNotFoundException e) {
System.out.println("Wrong class.");
}
return notif;
}
}
RegisteredUsers
public class RegisteredUsers implements Serializable {
private static HashMap<String, User> users;
public RegisteredUsers() {
users = new HashMap<String, User>();
}
public HashMap<String, User> getMap() {
return users;
}
public boolean isRegistered(String name) {
User us = users.get(name);
return us != null;
}
public void register(String name, String ip) {
users.put(name, new User(name, ip, false));
}
}
Notification
public class Notification implements Serializable {
private ArrayList<String> attributes;
private HashMap<String, User> map = new HashMap<>();
public Notification() {
}
public Notification(String name, String ip) {
attributes = new ArrayList<String>();
attributes.add(0, name);
attributes.add(1, ip);
map.put(ip, new User(name, ip, false));
}
public ArrayList<String> getAttributes() {
return attributes;
}
public HashMap<String, User> getMap() {
return map;
}
}
User
public class User implements Serializable {
private String name;
private String ip;
private boolean connection_state;
public User(String name, String ip, boolean connection_state) {
this.name = name;
this.ip = ip;
this.connection_state = connection_state;
}
}
In java static fields are implicitly transient, and transient fields are not serialized.
If you modify the RegisterdUsers to
public class RegisteredUsers implements Serializable {
private HashMap<String, User> users; // static modifier is removed
...
}
The serialization will work.

Deserialized object fields are null

I have successfully serialized my custom object but when I deserialize it this happens:
-Custom object is NOT NULL
-All fields are NULL
I know that I have successfully serialized my custom object because I have read the serialization file and it look fine.
Here is my code:
public class Preferences implements Serializable {
private static Preferences instance;
public static final long serialVersionUID = 3358037972944864859L;
public String accessToken;
protected Object readResolve() {
return getInstance();
}
private Preferences() {
}
private synchronized static void synchronize() {
if(instance == null) {
instance = new Preferences();
}
}
public static Preferences getInstance() {
if(instance == null) {
Preferences.synchronize();
}
return instance;
}
public void save(File file) {
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fos);
Preferences tempInstance = Preferences.getInstance();
out.writeObject(tempInstance);
out.close();
fos.close();
}catch(IOException e) {
e.printStackTrace();
}
}
public void load(File file) {
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fis);
if(file.length() > 0) {
Preferences tempInstance = (Preferences) in.readObject();
Log.e("", String.valueOf(tempInstance == null)); //prints FALSE
Log.e("", String.valueOf(tempInstance.accessToken == null)); //prints TRUE
}
in.close();
fis.close();
}catch(IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Here is my testing code:
public class CustomActivity extends AppCompatActivity {
private File dir = new File(Environment.getExternalStorageDirectory(), ".app");
private File backup = new File(dir, "backup.ser");
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
Log.e("APPLICATION", "START");
super.onCreate(savedInstanceState);
if(this instanceof ActivityLogin) {
if(!dir.exists()) {
dir.mkdirs();
}
Preferences.getInstance().load(backup);
}
}
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
try {
backup.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Preferences.getInstance().save(backup);
Log.e("APPLICATION", "STOP");
}
}
Any thoughts on what might be a problem?
You have this method in your class:
protected Object readResolve() {
return getInstance();
}
This tells the serialization mechanism: whenever you deserialize a Preferences instance, replace it by the one returned by getInstance(). So, if youc call load() and the instance of your Preferences has a null accessToken, then the deserialized preferences will have a null accessToken too, since they are the same object.
Add
System.out.println(tempInstance == this);
to your logging statements (or whatever you use in android to log), and you'll see.

Using same Map in several classes after Serialization?

My app is saving a hashmap before it stops and when it starts again loads the same hashmap so changes could be made to it. I am using Serialization.
Storage class:
public class Storage {
private Map<String, String> storage;
private String projectStorageFilePath;
public Storage() {
this.storage = new ConcurrentHashMap<String, String>();
makeDir();
}
/**
* If the file in which the map objects will be saved doesn't exist in the
* user home directory it creates it.
*/
private void makeDir() {
File projectHomeDir = new File(System.getProperty("user.home"), ".TestMap");
String projectHomeDirPath = projectHomeDir.getAbsolutePath();
File projectStorageFile = new File(projectHomeDirPath, "storage.save");
projectStorageFilePath = projectStorageFile.getAbsolutePath();
if (!projectHomeDir.exists()) {
projectHomeDir.mkdir();
try {
projectStorageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
#SuppressWarnings("unchecked")
public boolean load() {
boolean isLoaded = false;
ObjectInputStream ois = null;
try {
File file = new File(projectStorageFilePath);
if (file.length() != 0) {
//loading the map
ois = new ObjectInputStream(new FileInputStream(file));
storage = (ConcurrentHashMap<String, String>) ois.readObject();
isLoaded = true;
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (null != ois) {
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return isLoaded;
}
public boolean save() {
boolean isSaved = false;
ObjectOutputStream oos = null;
try {
//saving
oos = new ObjectOutputStream(new FileOutputStream(projectStorageFilePath));
oos.writeObject(storage);
isSaved = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != oos) {
oos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return isSaved;
}
public Map<String, String> getStorage() {
return this.storage;
}
}
The class in which I am trying to do something with that hashmap:
public class DoSomethingWithMap {
private Map<String, String> storage;
public DoSomethingWithMap(Map<String, String> storage) {
this.storage = storage;
}
public void addToMap(String key, String value) {
this.storage.put(key, value);
}
public void printMap() {
System.out.println(this.storage);
}
}
When I run it the first time it works fine:
public class Main {
public static void main(String[] args) {
Storage s = new Storage();
DoSomethingWithMap something = new DoSomethingWithMap(s.getStorage());
if (s.load()) {
System.out.println(s.getStorage());
}
something.addToMap("2", "test2");
something.addToMap("4", "test4");
something.addToMap("5", "test5");
if (s.save()) {
System.out.println(s.getStorage());
}
}
}
Output:
{} //empty map which is ok because it has never been saved before
{3=test3, 4=test4, 5=test5} //changes during runtime are saved
The problem is when I start Main again and try to make changes to the saved map:
public static void main(String[] args) {
Storage s = new Storage();
DoSomethingWithMap something = new DoSomethingWithMap(s.getStorage());
if (s.load()) {
System.out.println(s.getStorage());
}
something.printMap();
something.addToMap("6", "newTest");
something.addToMap("7", "newTest");
something.addToMap("8", "newTest");
something.printMap();
if (s.save()) {
System.out.println(s.getStorage());
}
}
Output:
{3=test3, 4=test4, 5=test5} //loading the map works fine
{} //here it should be same as previous line but is not
{6=newTest, 7=newTest, 8=newTest} //DoSomethingWithMap.printMap is printing only the changes during runtime
{3=test3, 4=test4, 5=test5} // changes during runtime are not saved
It is obvious DoSomethingWithMap class is not using the map which was given to it. Why? Which map is using? How I can fix that?
Thank you.
You are creating a new instance of the Map in your load method:
storage = (ConcurrentHashMap<String, String>) ois.readObject();
To fix you can clear the current map and then add all the values from the loaded one:
//loading the map
ois = new ObjectInputStream(new FileInputStream(file));
storage.clear();
storage.putAll((ConcurrentHashMap<String, String>) ois.readObject());
To prevent such error in the future, you could make those fields final and thus you will get error reports.

Cannot write to file ArrayList with class objects

I've reasearched a lot of websites and I couldn't find answear. I'm trying to write to .txt file my ArrayList which constains class objects. Every time I try to do it I`m getting exception. With reading is the same problem. Here is my code:
public static void write()
{
try
{
FileOutputStream out = new FileOutputStream("clients.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
oout.writeObject(lista);
oout.close();
}
catch(Exception ioe)
{
System.out.println("writing Error!");
welcome();
}
}
public static void read()
{
try
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("clients.txt"));
lista = (List<Client>) ois.readObject();
}
catch (ClassNotFoundException ex)
{
System.out.println("Koniec pliku");
}
catch(IOException ioe)
{
System.out.println("Error!");
welcome();
}
}
I guess you're looking for the Serializable interface of Java. In order to save objects you're class have to implement it.
The question is: What execatly do you want to save? The content of the list so that you can save it in a file and load it afterwards?
This simple example works for me (for the scenario I mention above):
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public User(String name, int ag) {
this.name = name;
this.age = age;
}
#Override
public String toString() {
return (this.name + ' ' + this.age);
}
}
public class Main {
private static List<User> l;
public static void main(String[] args) {
l = new ArrayList<User>();
user1 = new User("John", 22);
user2 = new User("Jo", 33);
l.add(user1);
l.add(user2);
write();
}
public static void write() {
try {
FileOutputStream fos = new FileOutputStream("testout.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(l);
oos.close();
} catch (Exception ioe) {
System.out.println("writing Error!");
}
}
}
Ok I have changed a bit (not each function just the read and write functionality) and this work.
Link to Code.
One important thing is that the Scanner class is not serializable. Therefore, you have to make it static for example.

java static variable serialization

How are the values of static variables persisted during serialization(If at all persisted). I have read similar questions on stack where it says that static variables are inherently transient i.e their state or current values is not serialized.
I was just doing a very simple example where i serialized a class and saved it to a file and then again reconstructed the class from the file.Surprisingly I find that the value of the static variable at and when the serialization happened is saved.
How does this happen. Is this because the class template along with it's instance information is saved during serialization. Here is the code snippet -
public class ChildClass implements Serializable, Cloneable{
/**
*
*/
private static final long serialVersionUID = 5041762167843978459L;
private static int staticState;
int state = 0;
public ChildClass(int state){
this.state = state;
staticState = 10001;
}
public String toString() {
return "state" + state + " " + "static state " + staticState;
}
public static void setStaticState(int state) {
staticState = state;
}
and here is my main class
public class TestRunner {
/**
* #param args
*/
public static void main(String[] args) {
new TestRunner().run();
}
public TestRunner() {
}
public void run() {
ChildClass c = new ChildClass(101);
ChildClass.setStaticState(999999);
FileOutputStream fo = null;
ObjectOutputStream os = null;
File file = new File("F:\\test");
try {
fo = new FileOutputStream(file);
os = new ObjectOutputStream(fo);
os.writeObject(c);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(null != os)os.close();
if(null != fo) fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileInputStream fi = null;
ObjectInputStream ois = null;
ChildClass streamed;
try {
fi = new FileInputStream(file);
ois = new ObjectInputStream(fi);
Object o = ois.readObject();
if(o instanceof ChildClass){
streamed = (ChildClass)o;
//ChildClass cloned = streamed.clone();
System.out.println(streamed.toString());
}
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(null != ois)ois.close();
if(null != fi) fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Note : There is nothing wrong with the code. I just am wondering how the value of the static variable 'staticState' in the class 'ChildClass' gets saved. Will the state be saved if i transmit this serialized data over a network then
The static field value was not serialized. The output is printing the new value of the static field simply because you modified it to 999999 but you never reset its value to the old one before de-serizalizing. Since the field is static, the new value is reflected in any instance of ChildClass.
To properly assert that the field is not serialized, reset the value to 10001 before de-serializing the object, and you will notice that its value is not 999999.
...
ChildClass.setStaticState(10001);
FileInputStream fi = null;
ObjectInputStream ois = null;
ChildClass streamed;
...
// when de-serializing, the below will print "state101 static state 10001"
System.out.println(streamed.toString());

Categories