5 Replies Latest reply: Apr 14, 2010 4:14 AM by 843790 RSS

    Java invalid stream header Problem using Streams

    843790
      Hi all, im writen a client-server app, and now i´m facing a problem that I dont know how to solve:

      This is the client:
      try
                   {
      
                       Socket socket = new Socket(ip, port);
      
      
                       ObjectOutputStream ooos = new ObjectOutputStream(socket
                               .getOutputStream());
                       SendMessage message = new SendMessage();
                       
                       message.numDoc = value.numDoc;
                       message.docFreq = value.docFreq;
      
                       message.queryTerms = query;
                       message.startIndex = startIndex;
                       message.count = count;
                       message.multiple = false;
                       message.ips = null;
                       message.ports = null;
                      
                       message.value = true;
                       message.docFreq = value.docFreq;
                       message.numDoc = value.numDoc;
                       ooos.writeObject(message);
                       
      
                       ObjectInputStream ois = new ObjectInputStream(socket
                               .getInputStream());
                       ComConstants mensajeRecibido;
                       Object mensajeAux;
                       String mensa = null;
      
                       byte[] by = null;
      
                       do
                       {
      
                           mensajeAux = ois.readObject();
      
                           if (mensajeAux instanceof ComConstants)
                           {
                             
      
                               System.out.println("Thread by Thread has Search Results");
      
                               String test;
                               
                               ByteArrayOutputStream testo = new ByteArrayOutputStream();
                               
                               mensajeRecibido = (ComConstants) mensajeAux;
                               
                               byte[] wag;
                                 
                               testo.write(
                                       mensajeRecibido.fileContent, 0,
                                       mensajeRecibido.okBytes);
                               
                               wag = testo.toByteArray();
                               
                               
                               if (by == null) {
                                    
                                    by = wag;
                                    
                               }
                               else {
                                    
                                    int size = wag.length;
                                    
                                      System.arraycopy(wag, 0, by, 0, size);
                               }
                                  
                               
                           } else
                           {
                               
                               System.err.println("Mensaje no esperado "
                                       + mensajeAux.getClass().getName());
                               break;
                           }
                       } while (!mensajeRecibido.lastMessage);
      
                       
                       
                       
                       //ByteArrayInputStream bs = new ByteArrayInputStream(by.toByteArray()); // bytes es el byte[]
                       ByteArrayInputStream bs = new ByteArrayInputStream(by);
                       ObjectInputStream is = new ObjectInputStream(bs);
                       QueryWithResult[] unObjetoSerializable = (QueryWithResult[])is.readObject();
                       is.close();
                       
                       //AQUI TOCARIA METER EL QUICKSORT
                       
                      XmlConverter xce = new XmlConverter(unObjetoSerializable, startIndex, count);
                              String serializedd = xce.runConverter();
      
                       
                       
                         tempFinal = serializedd;
                       
                       ois.close();
                       socket.close();
      
                   } catch (Exception e)
                   {
                       e.printStackTrace();
                   }
                   
                   i++;
      
                         }
      And this is the sender:
      try
              {
                   
                   QueryWithResult[] outputLine;
                   
                   Operations op = new Operations();
                   
                  boolean enviadoUltimo=false;
      
                  ComConstants mensaje = new ComConstants();
                  mensaje.queryTerms = query;
                 
                  outputLine = op.processInput(query, value);
                  
                  //String c = new String();
                     //c = outputLine.toString();
                     //StringBuffer swa = sw.getBuffer();
                  
                  ByteArrayOutputStream bs= new ByteArrayOutputStream();
                  
                  ObjectOutputStream os = new ObjectOutputStream (bs);
                  os.writeObject(outputLine);
                  os.close();
                  
                  byte[] mybytearray =  bs.toByteArray();
      
                         ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mybytearray); 
                 
                         BufferedInputStream bis = new BufferedInputStream(byteArrayInputStream);
                  
                  int readed = bis.read(mensaje.fileContent,0,4000);
                  
              
                  while (readed > -1)
                  {
                      
                     
                      mensaje.okBytes = readed;
                      
      
                      if (readed < ComConstants.MAX_LENGTH)
                      {
                          mensaje.lastMessage = true;
                          enviadoUltimo=true;
                      }
                      else
                          mensaje.lastMessage = false;
                      
                      oos.writeObject(mensaje);
      
             
                      if (mensaje.lastMessage)
                          break;
                      
                      mensaje = new ComConstants();
                      mensaje.queryTerms = query;
                      
                      readed = bis.read(mensaje.fileContent);
                  }
                  
                  if (enviadoUltimo==false)
                  {
                      mensaje.lastMessage=true;
                      mensaje.okBytes=0;
                      oos.writeObject(mensaje);
                  }
                 
                  oos.close();
              } catch (Exception e)
              {
                  e.printStackTrace();
              }
          }
      And this is the error log:
       Thread by Thread has Search Results
          java.io.StreamCorruptedException: invalid stream header: 20646520
           at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
           at java.io.ObjectInputStream.<init>(Unknown Source)
           at org.tockit.comunication.ServerThread.enviaFicheroMultiple(ServerThread.java:747)
           at org.tockit.comunication.ServerThread.run(ServerThread.java:129)
           at java.lang.Thread.run(Unknown Source)
      
      Where at `org.tockit.comunication.ServerThread.enviaFicheroMultiple(ServerThread.java:747)` is this line `ObjectInputStream is = new ObjectInputStream(bs);` on the 1st code just after `while (!mensajeRecibido.lastMessage);`
      I think that the problem its caused on one of the OutputStreams, which is a byte array not generated by and ObjectOutputStream...
      Any ideas on how to solve this problem? Thanks.