What character encoding does writeObject ObjectOutputStream use?

I read that Java uses internal UTF-16 encoding. that is, I understand that if I have: String var = "जनमत"; then “जनमत” will be internally encoded in UTF-16. So, if I upload this variable to some file, for example, below:

fileOut = new FileOutputStream("output.xyz");
out = new ObjectOutputStream(fileOut);
out.writeObject(var);

will there be an encoding of the string "जनमत" in the file "output.xyz" in UTF-16? Also, later, if I want to read from the file "output.xyz" via ObjectInputStream, can I get a view of the UTF-16 variable?

Thank.

+3
source share
3 answers

, ... "जनमत" "output.xyz" UTF-16?

, ObjectOutputStream . , ObjectInputStream. ( - IIRC , - , , XML JSON - .)

, "output.xyz" ObjectInputStream, UTF-16 ?

ObjectInputStream, . java.lang.String, ( ), UTF-16, , getBytes() ( , ).


. , , ; , JVM, .

+4

: UTF-16, - UCS-2; 2 ( 2 , .. 4 ).

ObjectOutputStream -, UTF-8, UTF-8, 2- , UTF-8 (- ), 0.

, , " , String, String", . JDK .

writeUTF() , , . "writeObject()" , .

+1

, ObjectOutputStream.writeString() UTF "" UTF "" UTF, "long", javadoc

The long UTF format is identical to the standard UTF, except that it uses an 8 byte header (instead of the standard 2 bytes) to transmit the UTF encoding length.

I got this from code ...

private void writeString(String str, boolean unshared) throws IOException {
    handles.assign(unshared ? null : str);
    long utflen = bout.getUTFLength(str);
    if (utflen <= 0xFFFF) {
        bout.writeByte(TC_STRING);
        bout.writeUTF(str, utflen);
    } else {
        bout.writeByte(TC_LONGSTRING);
        bout.writeLongUTF(str, utflen);
    }
}

and writeObject(Object obj)they do check

if (obj instanceof String) {
    writeString((String) obj, unshared);
}
0
source

Source: https://habr.com/ru/post/1779229/


All Articles