【发布时间】:2014-08-16 12:13:38
【问题描述】:
我正在编写网络 java 代码,但我似乎无法理解 ObjectInputStream 解释字节所需的先决条件。 以下是部分代码:
InputStream is = /* creation of the stream */
ObjectInputStream in = new ObjectInputStream(is);
System.out.println(in.readInt()); // the exception is thrown here
异常和堆栈跟踪:
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)
发送代码:
OutputStream os = /* creation of output stream */
out = new ObjectOutputStream(os);
out.writeInt(1);
out.flush();
现在有趣的部分是,当我将“in.readInt()”替换为手动读取“is”时,当我发送得到的字节时: -84 -19 0 5 119 4 0 0 0 1 我用谷歌搜索了序列化协议,这似乎意味着: “-84 -19” -> 序列化协议幻数 “0 5” -> 版本 "119" -> 数据类型 (TC_BLOCKDATA) "0 0 0 1" -> 我的整数 = 1
所以,无效的类型代码“77040000”是“119 4 0 0”部分的十六进制。
此时,我不知道在哪里搜索,ObjectInputStream似乎无法理解协议。
输入流是自定义的,这里是它的部分代码:
@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;
}
【问题讨论】:
-
您的第一个 read() 方法复制了 DataInputStream.readInt()。第二个是多余的。第三个同上。这段代码并不像你想象的那么神奇。
标签: java serialization