The difference resides not in the OutputStreams
but in the write methods you are using: they differ. In your first example you use dp.write()
and in your second example you use dp.writeChar()
.
Change dp.writeChar()
to dp.write()
in your second example and the results will be as expected.
java.io.DataOutputStream.writeChar(int)
method is implemented as follows:
public final void writeChar(int v) throws IOException {
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
incCount(2);
}
See soource code of writeChar(int)
while java.io.DataOutputStream.write(int)
has following implementation:
public synchronized void write(int b) throws IOException {
out.write(b);
incCount(1);
}
See soource code of write(int)