java StreamCorruptedException: invalid type code: 77040000, what the ObjectInputStream expects?

被刻印的时光 ゝ 提交于 2021-02-17 05:27:10

问题


i'm working on a network java code, and i can't seem to understand what are the prerequisites an ObjectInputStream needs to interpret bytes. Here is a part of the code :

InputStream is = /* creation of the stream */
ObjectInputStream in = new ObjectInputStream(is);
System.out.println(in.readInt()); // the exception is thrown here

exception and stack trace :

java.io.StreamCorruptedException: invalid type code: 77040000
    at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.refill(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.read(Unknown Source)
    at java.io.DataInputStream.readInt(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readInt(Unknown Source)
    at java.io.ObjectInputStream.readInt(Unknown Source)

sending code :

OutputStream os = /* creation of output stream */
out = new ObjectOutputStream(os);
out.writeInt(1);
out.flush();

now the interesting part, when i replace "in.readInt()" with a manual reading of "is", when i pring the bytes i got : -84 -19 0 5 119 4 0 0 0 1 i googled serialization protocols and it seems to mean : "-84 -19" -> serialization protocol magic numbers "0 5" -> version "119" -> type of data (TC_BLOCKDATA) "0 0 0 1" -> my integer = 1

so, the invalid type code "77040000" is the hexadecimal for the "119 4 0 0" part.

at this point, i don't know where to search, the ObjectInputStream seems to not be able to understand the protocol.

the input stream is custom, here is part of its code:

@Override
public int read() throws IOException {
    byte[] bytes = new byte[4];
    if(read(bytes, 0, 4) != 4)
        return -1;
    else
        return bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF);
}

@Override
public int read(byte[] b) throws IOException {
    return read(b, 0, available());
}

@Override
public int read(byte[] b, int off, int len) throws IOException{
    int i;
    for(i = 0; i < len; i++){
        if(!isReady())
            return i == 0 ? -1 : i;
        b[i+off] = data[offset++];
    }
    return i;
}

@Override
public int available() throws IOException {
    if(isReady())
        return length - offset;
    else
        return -1;
}

回答1:


Your read() method is broken. It is supposed to return a single byte. You should read the javadoc for InputStream thoroughly if you intend to write your own implementation.




回答2:


Yes you are right, i was reading the ObjectInputStream$BlockDataInputStream.readBlockHeader implementation when i realized that it uses read() to get a byte. The fact read() returns an integer made me implement it to read 4 bytes... I should have read more thorougly the javadoc of InputStream.

Thanks for your help !



来源:https://stackoverflow.com/questions/25340032/java-streamcorruptedexception-invalid-type-code-77040000-what-the-objectinput

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!