title: java.io.IOException: Premature EOF while reading serialized object
body: <p>I am working on a homework which i have to send back and forth an object in serialized mode.I have a Client3.java file which sends an object to Server3.java.And Server3.java do the exact same thing.But when i try to read the object in Client3 he throws a <code>java.io.IOException: Premature EOF</code> error.From an extensive search i found that my program runs faster than the InputStream reading but i dont know how to fix this </p>
  
  <p>here my 2 files</p>
  
  <pre><code>package server3;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  import java.net.HttpURLConnection;
  import java.net.URL; 
  
  public class Client3 {
      public static void main(String[] args) throws Exception {
        try {
          URL url = new URL("http://localhost:3333");
          HttpURLConnection s = (HttpURLConnection) url.openConnection();
          s.setDoOutput(true);
          s.setDoInput(true);
          s.setRequestMethod("POST");
          s.setUseCaches(false);
          //here we send an serialized object
          Send obj = new Send();
          ObjectOutputStream objOut = new               
                  ObjectOutputStream(s.getOutputStream());
          objOut.writeObject(obj);
  
          /*********this is the chunk of code that makes the error*****************/
          //here we read the serialized object
          InputStream in = s.getInputStream();
          ObjectInputStream ios = new ObjectInputStream(in);
          SendResponse oin = (SendResponse) ios.readObject();
          String gabim=oin.getGabim();
          String gabimNr =oin.getGabimNr();
          System.out.println(gabim);
          System.out.println(gabimNr);
          ios.close();
          s.disconnect();
          /***************************************************************************/
          //catch all the possible errors 
        } catch (IOException ex) {
          System.err.println("Gabim ne klient"+ex);
          ex.printStackTrace();
        }
      }
  }
  
  class Send implements Serializable {
      // the object to be send to the server
      private static final long serialVersionUID = 1L;
      int id = 1;
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      public int getAmount() {
          return amount;
      }
      public void setAmount(int amount) {
          this.amount = amount;
      }
      public int getPaid() {
          return paid;
      }
      public void setPaid(int paid) {
          this.paid = paid;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      int amount = 2000;
      int paid = 800;
      String name = "Andi Domi";
  }
  </code></pre>
  
  <p>And here it is the Server3.java file</p>
  
  <pre><code>package server3;
  
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.OutputStream;
  import java.io.Serializable;
  import java.net.InetSocketAddress;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.ArrayList;
  import com.sun.net.httpserver.HttpExchange;
  import com.sun.net.httpserver.HttpHandler;
  import com.sun.net.httpserver.HttpServer;
  
  public class Server3 {
  
  public static void main(String[] args) throws Exception {
      //create the server
      HttpServer server = HttpServer.create(new InetSocketAddress(3333), 0);
      server.createContext("/", new MyHandler());
      server.setExecutor(null); // creates a default executor
      server.start();
  }
  static class MyHandler implements HttpHandler {
      public void handle(HttpExchange t) throws IOException {
  
          /*******we start to read the object send from the client here*******/ 
          //create the sream
          ObjectInputStream ios = new ObjectInputStream(t.getRequestBody());
          //variables we need to connect to the database
          final String url = "jdbc:mysql://localhost/httpServer";
          final String user = "root";
          final String password = "";
          try {
              //create the object to be read
              Send oin = (Send) ios.readObject();
              int id = oin.getId();
              String emri = oin.getName();
              int amount = oin.getAmount();
              int paid = oin.getPaid();
              //jdbc try
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = DriverManager.getConnection(url, user,password);           
              //insert values into the first table
              PreparedStatement s = con
                      .prepareStatement("INSERT INTO      person(ID,Name,Amount,Paid) VALUES (?,?,?,?)");
              s.setInt(1, id);
              s.setString(2, emri);
              s.setInt(3, amount);
              s.setInt(4, paid);
              s.executeUpdate();
              //read the values from a table and add them to an array list
              ResultSet rs = s.executeQuery("SELECT * from personat ORDER BY ID");
              ArrayList&lt;Personat&gt; perList = new ArrayList&lt;Personat&gt;();
              if (rs != null) {
                  while (rs.next()) {
                      Personat per = new Personat();
                      per.setID(rs.getInt("ID"));
                      per.setName(rs.getString("Name"));
                      per.setAmount(rs.getInt("Amount"));
                      perList.add(per);
                  }
              }
              //send the serialized object
              String gabim="Ska asnje gabim";
              String gabimNr="1";
              t.sendResponseHeaders(200,0);
              OutputStream os = t.getResponseBody();
              SendResponse obj = new SendResponse(gabim,gabimNr,perList);
              ObjectOutputStream objOut = new ObjectOutputStream(os);
              objOut.writeObject(obj);
              objOut.close();
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
              //ketu cojme nje object me keto te dhena
              String gabim=("Dicka nuk shkoi mire me marrjen e objektit");
              String gabimNr="3";
              rrayList&lt;Personat&gt; perList = null;
              t.sendResponseHeaders(200,0);
              OutputStream os = t.getResponseBody();
              SendResponse obj = new SendResponse(gabim,gabimNr,perList);
              ObjectOutputStream objOut = new ObjectOutputStream(os);
              objOut.writeObject(obj);
              objOut.close();
          } catch (SQLException e) {
              //ketu cojme nje objekt me keto te dhena
              e.printStackTrace();
              String gabim=("Dicka nuk shkoi mire ne lidhje me databasin");
              String gabimNr="4";
              ArrayList&lt;Personat&gt; perList = null;
              t.sendResponseHeaders(200,0);
              OutputStream os = t.getResponseBody();
              SendResponse obj = new SendResponse(gabim,gabimNr,perList);
              ObjectOutputStream objOut = new ObjectOutputStream(os);
              objOut.writeObject(obj);
              objOut.close();
          }
      }
  }
  }
  
  class Personat {
  int ID;
  String Name;
  int Amount;
  public int getID() {
      return ID;
  }
  public void setID(int iD) {
      ID = iD;
  }
  public String getName() {
      return Name;
  }
  public void setName(String name) {
      Name = name;
  }
  public int getAmount() {
      return Amount;
  }
  public void setAmount(int amount) {
      Amount = amount;
  }
  }
  
  class SendResponse implements Serializable {
  private static final long serialVersionUID = 1L;
  
  String gabim;
  String gabimNr;
  ArrayList&lt;Personat&gt; personList;
  public SendResponse(String gabim, String gabimNr,
          ArrayList&lt;Personat&gt; personList) {
      super();
      this.gabim = gabim;
      this.gabimNr = gabimNr;
      this.personList = personList;
  }
  public String getGabim() {
      return gabim;
  }
  public void setGabim(String gabim) {
      this.gabim = gabim;
  }
  public String getGabimNr() {
      return gabimNr;
  }
  public void setGabimNr(String gabimNr) {
      this.gabimNr = gabimNr;
  }
  public ArrayList&lt;Personat&gt; getPersonList() {
      return personList;
  }
  public void setPersonList(ArrayList&lt;Personat&gt; personList) {
      this.personList = personList;
  }
  } 
  </code></pre>
  
  <p>after i run it from the <code>ex.printStackTrace();</code> in the Client3 file i get this :</p>
  
  <pre><code>java.io.IOException: Premature EOF
  java.io.IOException: Premature EOF
  at sun.net.www.http.ChunkedInputStream.readAheadBlocking(Unknown Source)
  at sun.net.www.http.ChunkedInputStream.readAhead(Unknown Source)
  at sun.net.www.http.ChunkedInputStream.read(Unknown Source)
  at java.io.FilterInputStream.read(Unknown Source)
  at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
  at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
  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.&lt;init&gt;(Unknown Source)
  at server3.Client3.main(Client3.java:29)
  </code></pre>
  
  <p>i dont know hot to read the file faster or make my program slower ,do someone have any idea ?</p>