I have written the following code which writes 4000 bytes of 0s to a file test.txt
. Then, I read the same file in chunks of 1000 bytes at a time.
FileOutputStream output = new FileOutputStream("test.txt");
ObjectOutputStream stream = new ObjectOutputStream(output);
byte[] bytes = new byte[4000];
stream.write(bytes);
stream.close();
FileInputStream input = new FileInputStream("test.txt");
ObjectInputStream s = new ObjectInputStream(input);
byte[] buffer = new byte[1000];
int read = s.read(buffer);
while (read > 0) {
System.out.println("Read " + read);
read = s.read(buffer);
}
s.close();
What I expect to happen is to read 1000 bytes four times.
Read 1000
Read 1000
Read 1000
Read 1000
However, what actually happens is that I seem to get "paused" (for a lack of a better word) every 1024 bytes.
Read 1000
Read 24
Read 1000
Read 24
Read 1000
Read 24
Read 928
If I try to read more than 1024 bytes, then I get capped at 1024 bytes. If I try to read less than 1024 bytes, I'm still required to pause at the 1024 byte mark.
Upon inspection of the output file test.txt
in hexadecimal, I noticed that there is a sequence of 5 non-zero bytes 7A 00 00 04 00
1029 bytes apart, despite the fact that I have written only 0s to the file. Here is the output from my hex editor. (Would be too long to fit in question.)
So my question is : Why are these five bytes appearing in my file when I have written entirely 0s? Do these 5 bytes have something to do with the pause that occurs every 1024 bytes? Why is this necessary?