2

I am new and I only know a little of english. Well my problem is that I have to do a Multithreading server and a Client class to communicate a lot of clients to one server. I am saving my clients in a hashmap and after adding to the hashmap I am using a method to write into a file (HashMap is serializable and I am using singleton pattern to make the hashmap global). The problem is when I try to read from my file, then Java throws me an EOFException and I don't know why.

This is my code:

import java.io.*;
import java.util.*;

public class MapaClients implements java.io.Serializable  {

private static MapaClients mapa= new MapaClients();

private Map<String,Client> clients= new HashMap<String,Client>();

private MapaClients(){}

public static MapaClients getInstance(){
    return mapa;
}

public void afegirMapa(String correu, Client client){
    clients.put(correu, client);
}

public Client existeix(String correu){
    if(clients.containsKey(correu)){
        return(clients.get(correu));
    }else{
        return(null);
    }
}

public void serializableGuardar(){
    try {
         FileOutputStream fileOut =new FileOutputStream("client.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(mapa);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in client.ser");
      } catch (IOException i) {
         i.printStackTrace();
      }
}

public void desserializableCarregar(){
    ObjectInputStream in;
      try {
         FileInputStream fileIn = new FileInputStream("client.ser");
         in = new ObjectInputStream(fileIn);
         boolean check=true;
         while (check) {
            try{
                mapa = (MapaClients) in.readObject();
            } catch(EOFException ex){
                in.close();
                check=false;
            }
         }
         in.close();
         fileIn.close();
      } catch (IOException i) {
         i.printStackTrace();
      } catch (ClassNotFoundException c) {
         c.printStackTrace();
      }
}

public void actualizar(Client client){
    clients.replace(client.getCorreu(), client);
}
}

The problem is here:

in = new ObjectInputStream(fileIn);

This is the exception:

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Proves.MapaClients.desserializableCarregar(MapaClients.java:46)

I hope u can help me and sorry for my english :)

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Besides the answer and because you said you are new to coding, I hope you don’t mind some code comments: Remove java.io from ‘implements java.io.Serializable’ as you are importing java.io.*. You can just write ‘return null’. Use proper code formatting, whichever IDE you are using, see if it has code reformatting, I use IntelliJ as my IDE. Move ObjectInputStream to its implementation (inside the try..catch block). Remove Boolean check=true and just use ‘break;’ for false. – Christian Nov 19 '17 at 21:39

1 Answers1

0

You are trying to initialize the ObjectInputStream from an empty file.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • The first time I run the server I know is empty but when I used the server and client it should to be not empty. Is public void serializableGuardar() correct?? Thank you for your help. – Joan Jara Bosch Nov 19 '17 at 21:40
  • The serialization code looks OK, so either there was an exception running it or you haven't run it yet. Check the file on disk. If empty, remove. – user207421 Nov 19 '17 at 21:42
  • Okai, sorry. As I said I am new and I don't know a lot of Stackoverflow. Thanks again I will try to run the server and add some users to see if the file is still empty. – Joan Jara Bosch Nov 19 '17 at 21:45
  • Well another question is how did it get to be zero length? You need to consider what happens the first time this system is run. It should cope with the file not existing, but it shouldn't have to cope with it being zero length, as that can't happen in normal operation. – user207421 Nov 19 '17 at 21:50
  • Thanks a lot for helping me. You are rigth and I changed my code using an If to see if tt's empty or not and if it's not empty then I read the file. Now I have 0 exceptions or errors.I am so gretefully. Thanks EJP :) – Joan Jara Bosch Nov 19 '17 at 22:52