I'm writing some marshaling/unmarshaling routines for a class project and am a bit perplexed about Java's default behavior in this case. Here are my "naive" subroutines for writing and reading strings to and from byte streams:
protected static void write(DataOutputStream dout, String str)
throws IOException{
dout.writeInt(str.length());
dout.writeChars(str);
}
protected static String readString(DataInputStream din)
throws IOException{
int strLength = 2*din.readInt(); // b/c there are two bytes per char
byte[] stringHolder = new byte[strLength];
din.read(stringHolder);
return new String(stringHolder);
}
Unfortunately, this simply doesn't work; the characters are written in UTF-16 format by default, but String(byte[]) seems to assume that each byte will contain a character, and since ASCII characters all start with a 0 byte in UTF-16, the constructor appears to just give up and return an empty string. The solution is to change readString to specify that it must use UTF-16 encoding:
protected static String readString(DataInputStream din)
throws IOException{
int strLength = 2*din.readInt();
byte[] stringHolder = new byte[strLength];
din.read(stringHolder);
return new String(stringHolder, "UTF-16");
}
My question is, why is this necessary? Since Java uses UTF-16 for strings by default, why wouldn't it assume that UTF-16 is being used when reading chars from bytes? Or, alternatively, why wouldn't it just encode the chars as bytes in the first place by default? In short, why don't the default behaviors of the writeChars() method and the String(byte[]) constructor parallel each other?
DataOutputStream.writeUTF()andDataInputStream.readUTF()? - Boris the Spider