CopyPastor

Detecting plagiarism made easy.

Score: 1.8936854976122497; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2016-08-16
by Riyaz Parasara

Original Post

Original - Posted on 2012-04-29
by user207421



            
Present in both answers; Present only in the new answer; Present only in the old answer;

You are reading the socket until read() returns -1. This is the end-of-stream condition (EOS). EOS happens when the peer closes the connection. Not when it finishes writing one file.
You need to send the file size ahead of each file. You're already doing a similar thing with the file count. Then make sure you read exactly that many bytes for that file:

And This Is Sample Code

String filename = dis.readUTF(); long fileSize = dis.readLong(); FileOutputStream fos = new FileOutputStream(filename); while (fileSize > 0 && (n = dis.read(buf, 0, (int)Math.min(buf.length, fileSize)) != -1) { fos.write(buf,0,n); fileSize -= n; } fos.close();


You can enclose all this in a loop that terminates when readUTF() throws EOFException. Conversely of course you have to call writeUTF(filename) and writeLong(filesize) at the sender, before sending the data.
You are reading the socket until `read()` returns -1. This is the end-of-stream condition (EOS). EOS happens when the peer closes the connection. Not when it finishes writing one file.
You need to send the file size ahead of each file. You're already doing a similar thing with the file count. Then make sure you read exactly that many bytes for that file:
String filename = dis.readUTF(); long fileSize = dis.readLong(); FileOutputStream fos = new FileOutputStream(filename); while (fileSize > 0 && (n = dis.read(buf, 0, (int)Math.min(buf.length, fileSize))) != -1) { fos.write(buf,0,n); fileSize -= n; } fos.close();
You can enclose all this in a loop that terminates when `readUTF()` throws `EOFException`. Conversely of course you have to call `writeUTF(filename)` and `writeLong(filesize)` at the sender, before sending the data.


        
Present in both answers; Present only in the new answer; Present only in the old answer;