title: data transfer not working using ObjectOutputStream.writeObject()
body: <p>I created a android to pc client-server app, and throughout my app's other features i use ObjectOutputStream.writeObject(AbstractPacket object) to send controls and data b/w my apps and they all works fine, now when i tried to send a file(eg:a small jpg/png file), after sending the file bytes to the server side and recreating the original jpg file, the file is showing that its corrupted and is not displaying, but its size is exact same as sent from the client android app.
  here's some code,</p>
  
  <p>client side:(file to bytearray)</p>
  
  <pre><code>  byte [] mybytearray  = new byte [(int)myFile.length()];
  
  FileInputStream fis = new FileInputStream(myFile);
  
  BufferedInputStream bis = new BufferedInputStream(fis);
  
  bis.read(mybytearray, 0, mybytearray.length);
  
  String filename= myFile.getName();
  
  FileTransferPacket packet = new FileTransferPacket(mybytearray,mybytearray.length,filename);
  TCPconnection.sendPacket(packet);
  </code></pre>
  
  <p>FileTransferPacket extends the AbstractPacket:</p>
  
  <pre><code>public class FileTransferPacket extends AbstractPacket implements Serializable {
  
  
  byte[] bytes;
  long size;
  String filename;
  
  public FileTransferPacket(byte[] bytes,long size,String filename) {
  super(Protocol.Command.FILE_TRANSFER);
  this.bytes = bytes;
  this.size = size;
  
  this.filename = filename;
  }
  
  public byte[] getBytes() {
  return bytes;
  }
  
  public long getSize() {return size; }
  
  public String getFilename() {return filename; }
  }
  </code></pre>
  
  <p>Here's how i send the packet object to the server:</p>
  
  <pre><code>public void sendPacket(AbstractPacket packet) throws IOException {
      connectionOutput.writeObject(packet);
      connectionOutput.flush();
      connectionOutput.reset();
  
    }
  </code></pre>
  
  <p>At the server side I read the objects like:</p>
  
  <p>AbstractPacket packet = (AbstractPacket) connectionInput.readObject();</p>
  
  <p>receiver.handleReceiveData(packet, connection);</p>
  
  <pre><code>public synchronized void handleReceiveData(AbstractPacket packet, TcpConnection connection) {
  
  String filename=packet.getFilename();
  long size= packet.getSize();
  
  byte [] mybytearray  = new byte [(int)size];
  
  byte[] myByteArray=packet.getBytes();
  
  String userHomeFolder = System.getProperty("user.home");
  
  userHomeFolder+="\\Desktop\\"+filename;
  
  try {
  FileOutputStream fos = new FileOutputStream(userHomeFolder);
  
  BufferedOutputStream bos = new BufferedOutputStream(fos);
  
  bos.write(mybytearray, 0 , (int)size);
  
  bos.flush();
  fos.close();
  bos.close();
  
  } catch (IOException e) {
  e.printStackTrace();
  }
  }
  </code></pre>
  
  <p>Can somebody tell me what's actually wrong here, also please suggest how to fix them.</p>
  
  <p>After some searching, i did found that maybe i can fix if i used normal OutputStreams to send only the file's bytearray to server, it could work:</p>
  
  <p>outputStream.write(mybytearray, 0, mybytearray.length);</p>
  
  <p>But in my whole app i used ObjectoutputStream, so how can i include both types of streams on a single socket, also on server side how can i distinguish the incoming data as an Object/bytesbuffer.</p>