Search code examples
javaobjectinputstreamobjectoutputstream

Object sent differs from object received


I'm currently working on a file transfer program. However, I'm facing a major problem. I wondered at first why when I received files on one end, it would only create an empty file. I later found out that for some reason, the object that I sent differed from the object I received. I sent a Message object:

public class Message implements Serializable {


    private static final long serialVersionUID = 566740029496308507L;
    private HashMap<MType, Object> map = new HashMap<MType, Object>();

    public Message(MType id, Object value) {
        map.put(id, value);
    }
/* methods */

}

Using the internal HashMap, I stored values such as file bytes.

        BufferedInputStream is = new BufferedInputStream(new FileInputStream(src));
        Message msg = new Message(MType.FILE_OPEN, true);
        com.transmit(msg);
        byte[] buf = new byte[Utility.bufferSize];
        msg = new Message(MType.FILE_NAME, src.getName());
        msg.setValue(MType.FILE_SIZE, Files.size(src.toPath()));
        com.transmit(msg);
        for (int count = is.read(buf); count > 0; count = is.read(buf)) {

            msg.setValue(MType.FILE_BYTE, buf);
            msg.setValue(MType.FILE_COUNT, count);
            com.transmit(msg);
            System.out.println("File part sent");

        }
        System.out.println("File sending complete");
        msg = new Message(MType.FILE_NAME, src.getName());
        msg.setValue(MType.FILE_SIZE, Files.size(src.toPath()));
        msg.setValue(MType.FILE_CLOSE, true);
        is.close();
        com.transmit(msg);

Where the transmit function directly writes the Message object. However, on the receiving end< I noticed a discrepancy between the Message sent and the Message received. On the receiving end, the Messages are sent but none of their internal maps ever contain the FILE_BYTE and FILE_COUNT values. It's as if in the loop, it's only sending the value of 'msg' before the loop, not accounting for the addition of new values. I know this because I made a toString method for the Message and this is how it looks like:

Sent message:

============================================
USERNAME {
  Bob
}
FILE_OPEN {
  true
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_SIZE {
  27252
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_COUNT {
  8192
}
FILE_BYTE {
  [B@5cea3875
}
FILE_SIZE {
  27252
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_COUNT {
  8192
}
FILE_BYTE {
  [B@5cea3875
}
FILE_SIZE {
  27252
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_COUNT {
  8192
}
FILE_BYTE {
  [B@5cea3875
}
FILE_SIZE {
  27252
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_COUNT {
  2676
}
FILE_BYTE {
  [B@5cea3875
}
FILE_SIZE {
  27252
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_CLOSE {
  true
}
FILE_SIZE {
  27252
}

============================================

Received Message:

============================================
USERNAME {
  Bob
}
FILE_OPEN {
  true
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_SIZE {
  27252
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_SIZE {
  27252
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_SIZE {
  27252
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_SIZE {
  27252
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_SIZE {
  27252
}

============================================

============================================
FILE_NAME {
  logo.jpg
}
USERNAME {
  Bob
}
FILE_SIZE {
  27252
}
FILE_CLOSE {
  true
}

============================================

If there's any other code I should be posting, please let me know. Thanks :)


Solution

  • You need to use ObjectOutputStream.writeUnshared(), or ObjectOutputStream.reset() before each writeObject(), if you want to retransmit the same object with changes. Otherwise only a handle to the original object is sent.

    Or create a new object per send.