Issue
I have confirmed via a packet sniffer that all data coming from the client is being totally sent, and that the data being received by the server is complete as well, however there is a slight problem.
Here is a basic representation of relevant parts of the client thread within the server.
this.data_in = new DataInputStream(sock.getInputStream());
this.in = new InputStreamReader(this.data_in);
The server will read in authentication data using the BufferedReader, and once authentication is complete it will read data from the socket, like so. The variable ‘fos’ is a FileOutputStream that the DataInputStream data is being written to.
int read = 0;
byte[] buffer = new byte[8192];
while((read = data_in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
bytes_received += read;
}
This all seems correct in concept, however, in practice the first 1450 bytes of the file read in are missing. I have tried using a new DataInputStream(sock.getInputStream()) in case the position in the stream was weird but still no luck. Thanks for any help!
Update 1
So I could use BufferedReader functionality I made a method to read in string lines as I was using them before. See below code.
int input;
boolean run = true;
while((input = in.read()) != -1 && run) {
run = true;
char[] chars = Character.toChars(input);
for(char c : chars) {
if(c == '\n') {
run = false;
break;
} else if(c != '\r') {
sb.append(c);
}
}
}
As great of a solution it seemed, there is still 1450 bytes of information missing. Maybe I’m going in the wrong direction here?
To me it looks like the
BufferedReaderis the problem – according to http://ulibgcj.sourceforge.net/javadoc/java/io/BufferedReader.html is has default buffer size of 8192… whatever this default buffer size is in your case, theBufferedReaderwill advance thedata_inin steps of that default buffer size… so even if you don’t consume themdata_inis usually on a position beyond the last bytes you consumed during authentication.