0

Assume these:

ObjectOutputStream out =
    new ObjectOutputStream(new BufferedOutputStream(remote.getOutputStream()));
ObjectInputStream in =
    new ObjectInputStream(new BufferedInputStream(remote.getInputStream()));

After sending the "initiate file send" message and size and successfully reading the string and long number on the other end (you can assume I already handled them) I want to be able to write a whole file without having to instance a new OutputStream. What I've done so far is:

InputStream buf = new BufferedInputStream(new FileInputStream(file));
out.writeUnshared(new PeerProtocol(PeerProtocol.Type.FILE, file.getName(), file.length()));
int sum = 0, len;
byte[] buffer = new byte[remote.getSendBufferSize()];
out.flush();
out.reset();
while ((len = buf.read(buffer)) > 0) {
    out.write(buffer, 0, len);
    sum += len;
    sendFileDialogController.updatePercentage((double) sum / file.length() * 100);
}
// System.out.println("done sending");
//out.flush(); didnt work neither
sendFileDialogController.updatePercentage(100);
buf.close();

Problem is I'm always missing the last few bytes so the file is corrupt and can't be used. How to fix this?

Muli Yulzary
  • 2,559
  • 3
  • 21
  • 39

0 Answers0