I am writing objects as needed to a file using ObjectOutputStream. However, I am only able to read the first object that is written to the file, even though several objects are added to the file. This means that it just loops, because the user object is never null.
I am sure if this is the correct way to save pair values to text file, as I have to update the file periodically with new pairs. Any ideas on a better way to do this?
Here is the object class.
package com.example.testaware.offlineAuth;
import java.io.Serializable;
public class AuthenticatedUser implements Serializable {
public String ipAddress;
public String pubKey;
public AuthenticatedUser(){
}
public AuthenticatedUser(String ipAddress, String pubKey){
this.ipAddress=ipAddress;
this.pubKey=pubKey;
}
public String getIpAddress() {
return ipAddress;
}
public String getPubKey() {
return pubKey;
}
public void setIpAddress(String ipAddress){
this.ipAddress=ipAddress;
}
public void setPubKey(String pubKey){
this.pubKey=pubKey;
}
}
And writing and reading the objects.
package com.example.testaware.offlineAuth;
import android.util.Log;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.PublicKey;
import java.util.HashMap;
/*Methods used to verify user that server wants to connect to - mappings of pub key and mac/Ip- after done challenge response*/
public class VerifyUser {
private static String LOG = "LOG-Test-Aware-Verify-User";
// Used to check key provided and IP of connected client
public static boolean isAuthenticatedUser(String peerIP, String key){
String thisLine =null;
boolean cont= true;
boolean isAuthenticated=false;
while (cont) {
try {
FileInputStream fileIn = new FileInputStream("/data/data/com.example.testaware/authenticatedUsers.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
AuthenticatedUser user = (AuthenticatedUser) in.readObject();
String ip = user.getIpAddress();
String clientKey= user.getPubKey();
if(user!=null) { //TODO:find out why it only checks first object?
if (ip.equals(peerIP) && clientKey.equals(key)) {
isAuthenticated = true;
in.close();
fileIn.close();
Log.i(LOG, "Successfully read key and ip to the file.");
cont = false;
}
}
else{
in.close();
fileIn.close();
System.out.println("No users authenticated in file");
Log.i(LOG, "No users authenticated in file");
}
} catch (IOException | ClassNotFoundException e) {
System.out.println("An error occurred. ");
e.printStackTrace();
cont= false;
}
}
return isAuthenticated;
}
//used to set map key and IP of new users , after challenge response //TODO: call this somewhere
public static void setAuthenticatedUser( String connectedPeerIP,String connectedPeerKey) {
AuthenticatedUser user= new AuthenticatedUser(connectedPeerIP,connectedPeerKey);
try{
FileOutputStream fileOut =
new FileOutputStream("/data/data/com.example.testaware/authenticatedUsers.txt",true);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(user);
out.close();
fileOut.close();
Log.i(LOG, "Successfully wrote key and ipaddr to the file.");
} catch (IOException i) {
System.out.println("An error occurred.");
i.printStackTrace();
}
}
}
```