3

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();
        }

    }

}

```
Raggen96
  • 49
  • 3
  • 1
    You open your file, read one object, and then close it, so naturally you only read the first object. – tgdavies Apr 14 '21 at 09:26
  • My "limited" search seems to suggest that appending objects to a file is not supported, or not supported easily. You would need to have ALL the contents read, update and re-written as an atomic workflow. There are also a number of issues with your reading code – MadProgrammer Apr 14 '21 at 09:30
  • After some experimentation and reading of posts like [this one](https://stackoverflow.com/questions/30013292/how-do-i-write-multiple-objects-to-the-serializable-file-and-read-them-when-the), it would seem that in order to store multiple objects into a file, you should be using a `List` of some kind. This will change your workflow some what, as you will need to maintain a `List` in memory and update it before it's written to the file – MadProgrammer Apr 14 '21 at 09:40
  • Thanks for the reply. I think I will try using a List, as @MadProgrammer suggested. I will update you, and see if it works. – Raggen96 Apr 14 '21 at 12:21

0 Answers0