cannot instantiate ObjectInputStream

manish411

New Member
I am making an android app in which I want to transfer file from my server pc to my android phone. The server reads the file , encapsulates it into a bytearray and then writes it as an object of a class to the ObjectOutputStream.

This is the class whose object is to be send
B4X:
import java.io.Serializable;
public class FileClass implements Serializable{
public byte file[];
public FileClass(byte file[])
{
  this.file = file;   
}
}
this is the function for sending the object
B4X:
void sendvideoobject(String f)throws Exception
   {
      String filepath = f;
        
       File file = new File(filepath);
       byte[] mybytearray  = new byte[(int)file.length()];
       FileInputStream fin = new FileInputStream(filepath);
       fin.read(mybytearray);
       FileClass myfile = new FileClass(mybytearray);
       ObjectOutputStream ob = new ObjectOutputStream(out);
       ob.flush();
       ob.writeObject(myfile);
      ob.flush();
      System.out.println("Finished sending file");
}

All the streams are initialized in a staticsocket class as static objects at the time of connection in a separtate thread.
B4X:
package com.android;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.net.Socket;

import android.util.Log;

public class staticsocket{
public static Socket socket;
public static OutputStream o;
public static String servername;
public static DataOutputStream dout;
public static DataInputStream din;
public static ObjectInputStream obin;
public static ObjectOutputStream obout;   
public static InputStream i;
public static BufferedInputStream bi;
public static BufferedOutputStream bo;
public static void setsocket(InputStream in,OutputStream out)

   {
      try{
      i = in;
      o = out;
   
    
     dout = new DataOutputStream(o);
      din = new DataInputStream(i);
      bi = new BufferedInputStream(i);
      bo = new BufferedOutputStream(o); 
      System.out.println("Finished intializing streams");
        
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
      }

}
This is the download function which is implemented at the client side at a separate thread running.
B4X:
   public void run() {
      // TODO Auto-generated method stub
       try
        {
          byte buffer[] = new byte[1024];
       String filepath =null;      
   
     System.out.println("started downloading");
     ObjectOutputStream obout = new ObjectOutputStream(staticsocket.o);
     obout.flush();
     System.out.println("Initialized objectoutputstream");
  

//THIS IS WHERE THE EXECUTION JUST HANGS
     ObjectInputStream obin = new ObjectInputStream(staticsocket.i);
        System.out.println("Initialized objecinputstream");
    FileClass file = (FileClass)obin.readObject();
          FileOutputStream of = new FileOutputStream("/mnt/sdcard/Raul.mp4");
     BufferedOutputStream bo = new BufferedOutputStream(of);
     bo.write(file.file);
           System.out.println("Finished receiving file"); 

   }
catch(Exception e)
{}
   }
}

The execution hangs just after Initialized objectoutputStream.
I have read that objectoutputstream on the other side has to be initialized before. But still after initializing the objectoutputstream on the server and flushing it the application on the client android is not able to intialize the objectinputstream. However the same algorithms works if I run the client application as a simple java code.
 
Last edited:
Top